自制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 "\n\n\n\n\n\n\n\n\t\t\tPlease enter unlock code:" | cowsay
stty -echo #屏蔽输入的字符
read input
case $input in
$mypassword)
printf "\t\t Hello $USER,Today is $(date +%T)\n"
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! / -------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || 123 Screen will locked in 7 seconds!
由于静态文本难以呈现执行过程中的动态交互,此处直接展示解锁界面。当输入密码不正确时,系统会提示错误信息,并附带一条幽默的“警告”。
___________________________ / \ \ Please enter unlock code: / --------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || 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. / ----------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
若密码输入正确,界面将显示如下内容:
___________________________ / \ \ Please enter unlock code: / --------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || Hello mark,Today is 06:35:05
总结
该脚本逻辑简洁,仅涉及Shell语法中的基础命令。期望此示例能抛砖引玉,激发读者思路,开发出更优秀的锁屏小工具!