自制Linux终端锁屏工具

准备工作
| 项目 | 详情 |
| 操作系统 | ElementaryOS虚拟机 + XShell远程登录工具。 |
| Shell语言 | 默认的Bash Shell。 |
| 其他小工具 | fortune:系统随机从语库中选出一句英文成语。 cowsay:在终端界面上显示奶牛语句框,配合管道连接fortune效果完美。 |
代码实现
#!/bin/bash scriptname: locktty writed by: Marksinoberg description: just for protecting our message when we leave away. And we can set the password every time. reset; clear # 清除屏幕 info="Please input the password you will use later!" cowsay $info read mypassword echo "Screen will locked in 7 seconds!" sleep 7 clear
#!/bin/bash
scriptname: locktty
writed by: javalee
script start...
reset; clear # 清除屏幕
info="Please input the password you will use later!"
cowsay $info
read mypassword
echo "Screen will locked in 7 seconds!"
sleep 7
clear
trapper () { # 建立个函数
trap ' ' 2 3 20 # 忽略CTRL+C CTRL+\ CTRL+Z信号
}
while : # 进入死循环
do
trapper # 调用函数
printf "
\t\t\tPlease enter unlock code:" | cowsay
stty -echo # 屏蔽输入的字符
read input
case $input in
$mypassword)
printf "\t\t Hello $USER,Today is $(date +%T)
"
stty echo
break ;; # 输入正确,跳出循环回到命令行
*) echo "Do not check my files, please! See as follows:"
sleep 3
clear
continue ;; # 否则,继续循环
esac
done
运行演示
程序运行开始:
mark@mark:~/temp/myscripts$ ./lockscreen.sh / Please input the password you \ \ will use later! / Screen will locked in 7 seconds!
由于静态文本无法展示动态效果,直接看解锁界面,当我们输入不正确的密码时,系统会提示输入错误,并给出幽默的“警告”。
\ Please enter unlock code: / Do not check my files,please! See as follows: / Q: Why is it that the more accuracy you \ | demand from an interpolation | | function, the more expensive it becomes | | to compute? A: That's the Law of Spline | \ Demand. /
当我们输入正确的密码时:
/ \ \ Please enter unlock code: / Hello mark, Today is 06:35:05
代码非常简单,仅使用了shell脚本语法的几个小命令,希望这个脚本能抛砖引玉,打开你的思路,做出更好的锁屏小脚本!
相关问题与解答
问题1:如何安装和配置必要的小工具?

答:需要安装的小工具有fortune和cowsay,可以通过以下命令进行安装:
sudo apt-get install fortune cowsay
安装完成后,即可正常使用这两个小工具。fortune用于随机生成英文成语,cowsay用于在终端界面上显示一个奶牛的语句框,通过管道将fortune的输出传递给cowsay,可以实现完美的效果。
问题2:如何在脚本中设置自动锁屏时间?
答:在脚本中,使用sleep命令来设置锁屏时间。
echo "Screen will locked in 7 seconds!" sleep 7
上述代码表示屏幕将在7秒后锁定,可以根据需要调整sleep命令后的数值,以设置不同的锁屏时间。