一、用until实现下列脚本
1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。
#!/bin/bash #author:jackCui #description:Find out if the system has a hacker user login per 3s until false ;do whether=0 user=`who|grep "hacker"|cut -f1 -d " "` if [ -n "$user" ];then until [ $whether -ne 0 ] ;do who | grep "hacker">>/var/log/login.log echo "hacker logout right now!"| write hacker whether=1 sleep 3 done else sleep 3 fi done
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
[root@centos7 testdir]# cat guess_BorS.sh #!/bin/bash #author:jackcui random=$[$RANDOM%10] read -p "Input you number: " guess until [ $random -eq $guess ];do if [ $random -gt $guess ];then echo "The number you guess is too small" else echo "The number you guess is too big" fi read -p "Input you number: " guess done echo "YOU WIN THE GAME!"
3、编写脚本,求100以内所有正整数之和
[root@centos7 testdir]# cat until100.sh #!/bin/bash #author:jackcui #description:use until solve sum 1 to N i=0 alpha=0 sum=0 while [ $alpha -eq 0 ];do //如果输入错误则循环输入 read -p "Input the number:" N alpha=`echo $N |grep "\<[[:digit:]]\+\>"|wc -l` if [ $alpha -eq 0 ];then echo "input error,input must digit!" fi done until [ "$((N+1))" -eq "$i" ];do //N+1保证能保证最后一个数字也能加上 ((sum+=i++)) done echo "The sum 1 to $N is $sum"
4、编写脚本,通过ping命令探测172.16.250.1-254范围内的所有主机的在线状态,统计在线主机和离线主机各多少。
#!/bin/bash # i=1 until [ $i -gt 254 ]; do if ping -W1 -c1 "172.16.20.$i" &> /dev/null;then echo "172.16.20.$i is online!!!!" let online++ else echo "172.16.20.$i is not online!!!!" let unline++ fi let i++ done echo "online=$online" echo "unline=$unline"
5、编写脚本,打印九九乘法表
#!/bin/bash i=1;j=1;sum=0 until [ $i -gt 9 ];do until [ $j -gt $i ];do ((sum=j*i)) echo -ne "$j*$i=$sum\t" ((j++)) done echo "" ((j=1)) ((i++)) done
6、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大者和最小者
[root@centos7 testdir]# cat maxmin.sh #!/bin/bash #author:jackcui temp=$[$RANDOM%20];max=$temp;min=$temp;i=0 until [ $i -eq 10 ];do temp=$[$RANDOM%20] if [ $temp -ge $max ];then max=$temp elif [ $temp -lt $min ];then min=$temp fi ((i++)) done echo "The maximum value is : $max,minimum value is : $min"
7、编写脚本,实现打印国际象棋棋盘
[root@centos7 testdir]# cat untilchess.sh #!/bin/bash #author:jackcui i=1;j=1 until [ $i -eq 9 ];do until [ $j -eq 9 ];do if [ $[(i+j)%2] -eq 0 ];then echo -en "\e[45m \e[0m" ((j++)) else echo -en "\e[47m \e[0m" ((j++)) fi done echo "";((i++));((j=1)) done
8、打印等腰三角形
#!/bin/bash #author:jackcui read -p "Input the line you want print: " line nline=1 i=0;j=0 until [ $nline -eq $[line+1] ];do until [ $[line-nline-i ] -eq 0 ];do echo -n " " ((i++)) done i=0 until [ $[ 2*nline-1-$j ] -eq 0 ];do echo -ne "\e[35;5m*\e[0m" ((j++)) done j=0 ((nline++)) echo "" done
原创文章,作者:jack_cui,如若转载,请注明出处:http://www.178linux.com/37198