标签:循环
1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。
#!/bin/bash
until who | grep "^\<hacker\>" > /var/log/login.log && echo "you must logout" | write jack
do
sleep 3s
done
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash
declare -i r=$(($RANDOM%10))
declare -i n=10000000000000000
until [ "$n" -eq "$r" ] && echo "you are so clear"
do
read -p "you can guess a number[0-10]:" n
if [ "$n" -gt "$r" ]
then
echo "you guess great"
elif
[ "$n" -lt "$r" ]
then
echo "you guess less"
fi
done
3、编写脚本,求100以内所有正整数之和
#!/bin/bash
declare -i i=1
declare -i sum=0
until [ $i -eq 101 ] ; do
sum=$[$sum+$i]
i=$[$i+1]
done
echo "sum1..100:$sum"
4、编写脚本,通过ping命令探测172.16.250.1-254范围内的所有主机的在线状态,统计在线主机和离线主机各多少。
#!/bin/bash
declare -i i=1
declare -i y=0
declare -i n=0
until [ $i -eq 255 ];do
ping -c 1 -W 1 10.1.252.$i &> /dev/null && y=$[$y+1] || n=$[$n+1]
i=$[$i+1]
done
echo "ping up:$y"
echo "ping down:$n"
5、编写脚本,打印九九乘法表
#!/bin/bash
i=1
until [ $i -ge 10 ]; do
j=1
until [ $j -gt $i ];do
echo -ne ""$j"x"$i"=$[$i*$j]\t"
j=$[$j+1]
done
i=$[$i+1]
echo
done
6、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大者和最小者
until做法:
#!/bin/bash
MAX=$RANDOM MIN=$MAX i=1
until [ $i -ge 11 ] ; do
n=$RANDOM
i=$[$i+1]
[ $MAX -ge $n ] && MAX=$MAX || MAX=$n
[ $MIN -le $n ] && MIN=$MIN || MIN=$n
echo $n
done
echo "MAX=$MAX"
echo "MIN=$MIN"
while做法:
#!/bin/bash
i=1 a=$RANDOM
echo $a
b=$a
while [ $i -le 9 ];do
n=$RANDOM
echo $n
i=$[$i+1]
[ $n -gt $a ] && a=$n || a=$a
[ $n -lt $b ] && b=$n || b=$b
done
echo "MAX $a"
echo "MIN $b"
7、编写脚本,实现打印国际象棋棋盘
until做法:
#/bin/bash
i=1
until [ $i -gt 9 ];do
j=1
until [ $j -gt 9 ];do
[ $[$[$i+$j]%2] -eq 0 ] && echo -en "\033[41m \033[0m" || echo -en "\033[46m \033[0m"
j=$[$j+1]
done
i=$[$i+1]
echo
done
for做法:
#/bin/bash
for i in {1..8}
do
for j in {1..8}
do
if [ $j -lt 8 ] ; then
[ "$[$[$i+$j]%2]" -eq 0 ] && echo -en "\033[41m \033[0m" || echo -en "\033[45m \033[0m"
else
[ "$[$[$i+$j]%2]" -eq 0 ] && echo -e "\033[41m \033[0m" || echo -e "\033[45m \033[0m"
j=j++
fi
done
i=i++
done
8、打印等腰三角形
#!/bin/bash
read -p "number:" n
for j in `seq $n`;do
for a in `seq $[$n-$j]` ; do
echo -n " "
done
b=$[$j*2-1]
for m in `seq $b`;do
echo -n "#"
done
echo
done
本文出自 “zebra930” 博客,请务必保留此出处http://zebra930.blog.51cto.com/11736340/1839717
标签:循环
原文地址:http://zebra930.blog.51cto.com/11736340/1839717