Shell应用编程之开机密码欢迎welcome


描述:若用户希望锁定对终端的访问,但不想退出并再次登录,则可以编写一个Shell脚本程序实现。

当调用该脚本时,知道用户输入正确的密码才能退出。

其中:

  • ctrl + u  删除整行
  • ctrl + b 删除前一个字符
  • 粗体显示字符

shell程源序:

  1. trap " " 2 3 4 
  2. stty -echo 
  3. #tput bel  
  4. stty kill \^u 
  5. stty erase \^b 
  6. tput bold 
  7. #tput blink  
  8. tput bel 
  9. #tput dim  
  10. if [ $# -gt 0 ] 
  11. then   
  12.   MESG="$@" 
  13. else 
  14.   MESG="This system is locked" 
  15. fi 
  16.  
  17. tput clear 
  18. tput cup 5 10; echo "Enter your passwd>\c" 
  19. read pword_1 
  20. tput clear 
  21. tput cup 10 20;echo "$MESG" 
  22. pword_2= 
  23. until [ "$pword_1" = "$pword_2" ] 
  24. do 
  25.   tput rev 
  26.   read pword_2 
  27. done 
  28. stty echo 
  29. tput clear 
  30. exit 0 

分析:通过trap捕捉信号,设置中断信号、退出信号均已屏蔽,即ctrl + c,del ,Break键均不管用。

相关内容