shelll脚本,常见的脚本题目。

[root@localhost wyb]# cat 2quan.sh 
#!/bin/bash

#写一个脚本,先要求输入用户名,然后让他输入一个数字,输的如果是数字给输出yes,不是数字,输出no
#然后如果输入三次以上不是数字锁定此用户,就是不能让他上了
#要求次数累加,即使关了脚本再重启,也没用,依旧锁定,次数到了3就锁定

read_user()
{

read -p "Please Input a Username:" username

}
read_user
while [ -z $username ]
do
    read_user
done

[ ! -f $username.log ] && touch $username.log


judge_user()
{
   key=`cat $username.log|wc -l`
   [ $key -ge 3 ] && echo "This $username is lock" && exit
}

judge_user
echo 'input `quit`to quit'
while :
do
  judge_user
  read -p "Pleas input a number:" num
   [ -z $num ] && continue
   [[ "$num" = "quit" ]] && break
   
   expr $num + 1 &>/dev/null
  [ $? -ne 0 ] && echo error  >> $username.log && echo no|| echo yes

done
[root@localhost wyb]# bash 2quan.sh 
Please Input a Username:cheng
input `quit`to quit
Pleas input a number:eee
no
Pleas input a number:123
yes
Pleas input a number:123
yes
Pleas input a number:ddd
no
Pleas input a number:aaa
no
This cheng is lock
[root@localhost wyb]# bash 2quan.sh 
Please Input a Username:cheng
This cheng is lock
[root@localhost wyb]# bash 2quan.sh 
Please Input a Username:jing
input `quit`to quit
Pleas input a number:123
yes
Pleas input a number:bbb
no
Pleas input a number:ddd
no
Pleas input a number:aaa
no
This jing is lock
[root@localhost wyb]# bash 2quan.sh 
Please Input a Username:quit
input `quit`to quit
Pleas input a number:quit
[root@localhost wyb]# cat jing.log 
error
error
error
[root@localhost wyb]# cat cheng.log 
error
error
error
[root@localhost wyb]# 

 增加了一些需求

#写一个脚本,先要求输入用户名,然后让他输入一个数字,输的如果是数字给输出yes,不是数字,输出no
#然后如果输入三次以上不是数字锁定此用户,就是不能让他上了
#要求次数累加,即使关了脚本再重启,也没用,依旧锁定,次数到了3就锁定
#如果用户被锁,则提示用户问他是不是解锁
#解锁的话需要输入5次数字,有一次错误都不能解锁,如果输入5次都没错,那么用户解锁
#增加注册与登录。如果登录的用户没有注册,则提示注册,如果登录用户已注册,则可以继续操作

[root@localhost wyb]# cat 12quan.sh #!/bin/bash read_user() { read -p "Please Input username:" username } flag=0 read_user while [ -z $username ] do read_user done user_reg() { echo ----------------create a user-------------- read -p "Please Input A username:" name1 [ -z $name1 ] && echo "You Do no Input" && exit 1 read -p "Please Input A username:" name2 [ -z $name2 ] && echo "You Do no Input" && exit 2 [[ "$name1" = "$name2" ]] && echo $name1 >> user.list echo "Your user ($name1) has registered!" && exit 0 } #注册了flag2不变是0可以执行程序 flag2=0 [ ! -f ${username}.log ] && touch ${username}.log [ ! -f user.list ] && touch user.list user_exist=`cat user.list|grep -c "<$username>"` [ $user_exist -eq 0 ] && read -p "You username do not exist.create it(yes/on)?" create_user && flag2=1 #[ "$create_user" = "no" ] && exit [ $flag2 -eq 1 ] && [[ "$create_user" = "yes" ]] && user_reg [ $flag2 -eq 1 ] && exit unlock_user() { for i in `seq 5 -1 1` do read -p "Please a number(remain $i counts):" unlock_num expr $unlock_num + 1 &>/dev/null [ $? -ne 0 ] && echo "Input error,unlock stop" && exit 1 done echo "( $username ) has been unlocked" > ${username}.log } judge_user() { key=`cat ${username}.log|wc -l` [ $key -ge 3 ] && echo "Your username ( $username ) is locked." && flag=1 [ $flag -eq 1 ] && read -p "Do you wanna to unlock(yes/no)" unlock_key [ $flag -eq 1 ] && [[ "$unlock_key" = "yes" ]] && unlock_user && exit [ $flag -eq 1 ] && [[ "$unlock_key" != "yes" ]] && exit } judge_user echo 'Input `quit` to quit' while : do judge_user read -p "Please Input a number:" number [ -z $number ] && continue [[ "$number" = "quit" ]] && break expr $number + 1 &>/dev/null [ $? -ne 0 ] && echo "error" >> ${username}.log && echo no || echo yes done [root@localhost wyb]# bash 12quan.sh Please Input username:aaa You username do not exist.create it(yes/on)?yes ----------------create a user-------------- Please Input A username:aaa Please Input A username:aaa Your user (aaa) has registered! [root@localhost wyb]# bash 12quan.sh Please Input username:aaa Input `quit` to quit Please Input a number:1 yes Please Input a number:1 yes Please Input a number:no no Please Input a number:no no Please Input a number:no no Your username ( aaa ) is locked. Do you wanna to unlock(yes/no)yes Please a number(remain 5 counts):1 Please a number(remain 4 counts):1 Please a number(remain 3 counts):1 Please a number(remain 2 counts):1 Please a number(remain 1 counts):11 ( aaa ) has been unlocked [root@localhost wyb]# bash 12quan.sh Please Input username:aaa Input `quit` to quit Please Input a number: [root@localhost wyb]#
原文地址:https://www.cnblogs.com/wangyuebo/p/5836088.html