1、while练习
1、编写脚本,求100以内所有正奇数之和
#!/bin/bash
i=1
sum=0
while [ $i -lt 100 ];do
if [ $[i%2] -ne 0 ];then
let sum+=i
fi
let i++
done
echo jishusum=$sum
2、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少
#!/bin/bash
read -p "please input nework(eg:192.168.0.0): " network
netid=`echo $network|cut -d. -f1-3`
hostid=1
up=0
down=0
while [ $hostid -le 254 ];do
if ping -c1 -w1 $netid.$hostid &>/dev/null;then
echo "the $netid.$hostid is up"
let up++
else
echo "the $netid.$hostid is down"
let down++
fi
let hostid++
done
echo the up is $up
echo the down is $down
3、编写脚本,打印九九乘法表
#!/bin/bash
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
let k=i*j
echo -en "$i*$j=$k\t"
let j++
done
echo
let i++
done
[root@redhat7 app]#./while9\*9.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
4、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数
字,并显示其中的最大值和最小值
#!/bin/bash
>/random.txt
i=1
while [ $i -le 10 ];do
echo $RANDOM |tee -a /app/random.txt
let i++
done
echo "max is `cat /app/random.txt|sort -n|tail -n1`"
echo "min is `cat /app/random.txt|sort -n|head -n1`"
5、编写脚本,实现打印国际象棋棋盘
#!/bin/bash
i=1
while [ $i -le 8 ];do
j=1
while [ $j -le 8 ];do
let k=i+j
if [ $[k%2] -eq 0 ];then
echo -en "\033[41m \033[0m"
else
echo -en "\033[43m \033[0m"
fi
let j++
done
echo
let i++
done
6、后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机执行命令:
echo $RANDOM|md5sum|cut –c1-10
后的结果,请破解这些字符串对应的RANDOM值
#!/bin/bash
#
read -p "please input passwd: " passwd
i=0
while [ $i -le 32767 ];do
num=`echo $i |md5sum|cut -c1-10`
if [ $num == $passwd ];then
echo "$passwd:$i"
break
fi
let i++
done
2、until练习
1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并退出脚本
#!/bin/bash
>/app/login.log
until false;do
if who |grep "^hacker\>" &>/dev/null;then
who|grep "^hacker\>">/app/login.log
break
fi
sleep 3
done
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash
random=`echo $[RANDOM%10]`
read -p "I guess the num is: " num
until [ $num -eq $random ];do
[ $num -gt $random ]&&echo "you guess big"
[ $num -lt $random ]&&echo "you guess small"
read -p "I guess the num is: " num
random=`echo $[RANDOM%10]`
done
echo "you guess right"
3、用文件名做为参数,统计所有参数文件的总行数
#!/bin/bash
[ -a $1 ]||{ echo "the file is not exist,please input again";exit 10; }
[ $# -eq 0 ]&& echo "you must input a filename"&& exit 100
until [ $# -eq 0 ];do
sum=0
n=`wc -l<$1`&>/dev/null
let sum+=n
shift
done
echo sum=$sum
4、用二个以上的数字为参数,显示其中的最大值和最小值
#!/bin/bash
>/app/num.log
if [ $# -lt 2 ];then
echo "the parameter must be more than one"
exit 2
fi
[[ "$1" =~ ^-?[0-9]+$ ]]|| { echo "the parameter must be integer" ;exit 100; }
until [ $# -eq 0 ];do
echo $1>> /app/num.log
[[ "$1" =~ ^-?[0-9]+$ ]]|| { echo "the parameter must be integer" ;exit 20; }
shift
done
echo "max=`cat /app/num.log |sort -n|tail -n1`"
echo "min=`cat /app/num.log|sort -n|head -n1`"