标签:练习
1、每隔3秒钟到系统上获取已经登录的用户的信息;
#!/bin/bash until false do if w|grep "^hacker\b" &>/dev/null;then w|grep "^hacker\b" >>/var/log/login.log echo "please logout the system" else echo "the user is not onlin" fi sleep 3 done
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash num=`echo $[$RANDOM%10]` echo "$num" n=11 until [ $n -eq $num ];do read -p "please input a number[0-10]:" n if [ $n -gt $num -a $n -lt 10 ];then echo "偏大" elif [ $n -lt $num -a $n -lt 10 ];then echo "偏小" elif [ $n -eq $num ];then continue else echo "please input the number in 0-10" fi done echo "哈哈!猜对了"
3、编写脚本,求100以内所有正整数之和
#!/bin/bash i=0 sum=0 until [ $i -gt 99 ] do let i++ sum=$(($sum+$i)) done echo "the total num is $sum"
4、编写脚本,通过ping命令探测172.16.250.1-254范围内的所有主机的在线状态,统计在线主机和离线主机各多少。
#!/bin/bash read -p "please input the ip:" ip if echo "$ip" |egrep -o "\b(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]$)" &>/dev/null ;then ip1=`echo "$ip" |egrep -o "\b(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}\b"` i=1 num=0 until [ $i -gt 255 ];do if ping -c1 -w1 $ip1$i &>/dev/null;then let num++ echo "the $ip1$i is online " else echo "the $ip1$i is not online" fi let i++ done echo "the online number is $num,the not online number is $[254-num]" else echo "the ip is not hefa"&&$0 fi
5、编写脚本,打印九九乘法表
#!/bin/bash a=1 b=1 until [ $a -gt 9 ];do until [ $b -gt $a ];do num=$(($a*$b)) echo -ne $b*$a=$num" \t" let b++ done let a++ b=1 echo done
6、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大者和最小者
#!/bin/bash i=1 let min=max=$RANDOM until [ $i -gt 9 ] do random=$RANDOM echo -en "$random" "\t" if [ $random -ge $max ];then max=$random fi if [ $random -le $min ];then min=$random fi let i++ done echo echo "the max is $max,the min is $min"
7、编写脚本,实现打印国际象棋棋盘
#!/bin/bash a=1 b=1 until [ $a -gt 8 ] do until [ $b -gt 8 ] do if [ $[(($a+$b))%2] -gt 0 ];then echo -ne "\033[47m \033[0m" else echo -ne "\033[43m \033[0m" fi let b++ done echo let a++ b=1 done
8、打印等腰三角形
用while写
#!/bin/bash i=1 j=1 while [ $i -le 10 ] do while [ $i -le $[10-$j] ] do echo -n " " let j++ done j=1 while [ $j -le $[2*$i-1] ] do echo -n x let j++ done let i++ j=1 echo done
用for写
#!/bin/bash read -p "please input the long:" n for((i=1;i<=$n;i++));do for((j=1;j<=$[$n-i];j++));do echo -n " " done for ((j=1;j<=$[2*i-1];j++));do echo -n "x" done echo done
本文出自 “zhang1003995416” 博客,谢绝转载!
标签:练习
原文地址:http://1003995416.blog.51cto.com/10482168/1840108