分享几个linux shell脚本,包括判断登录用户、判断是否继续执行、隐藏输入、屏蔽显示等shell脚本
1、判断登录用户
[devtac@test_1 shell]$ vi check_user.sh
#! /bin/sh echo "You are logged in as `whoami`"; if [ `whoami` != devtac ]; then echo "Must be logged in as devtac to run this script." exit fi echo "Running script at `date`"
1.2、运行结果
[devtac@test_1 shell]$ chmod a+x check_user.sh
[devtac@test_1 shell]$ ./check_user.sh You are logged in as devtac Running script at 2014年 12月 09日 星期二 13:35:17 CST
2、判断是否继续执行
[devtac@test_1 shell]$ vi do_continue.sh
#! /bin/sh doContinue=n echo "Do you really want to continue? (y/n)" read doContinue if [ "$doContinue" != y ]; then echo "Quitting..." exit fi echo "OK... we will continue." 2.2 运行结果
[devtac@test_1 shell]$ ./do_continue.sh
Do you really want to continue? (y/n) y OK... we will continue. [devtac@test_1 shell]$ ./do_continue.sh Do you really want to continue? (y/n) n Quitting... [devtac@test_1 shell]$
3、隐藏输入
[devtac@test_1 shell]$ vi hide_input.sh echo "$pw was entered"
3.2 结果
./hide_input.sh
Enter the database system password:123qweasd was entered [devtac@test_1 shell]$
3.3 解析
3.3.1 man 手册定义
DESCRIPTION
[devtac@test_1 shell]$ stty -a
本例中使用的参数:
[-]echo
echo input characters
屏蔽显示:
测试方法:
stty -echo;read;stty echo;read
简述: (责任编辑:IT) |