一、用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.www58058.com/37198