1嫉到、while練習(xí)
1、編寫腳本月洛,求100以內(nèi)所有正奇數(shù)之和
#!/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何恶、編寫腳本,提示請(qǐng)輸入網(wǎng)絡(luò)地址嚼黔,如192.168.0.0细层,判斷輸入的網(wǎng)段中主機(jī)在線狀態(tài)惜辑,并統(tǒng)計(jì)在線和離線主機(jī)各多少
#!/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個(gè)隨機(jī)數(shù)字捧搞,輸出這個(gè)10數(shù)
字抵卫,并顯示其中的最大值和最小值
#!/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、編寫腳本胎撇,實(shí)現(xiàn)打印國際象棋棋盤
#!/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介粘、后續(xù)六個(gè)字符串:efbaf275cd、4be9c40b8b晚树、44b2395c46姻采、f8c8873ce0、b902c16c8b题涨、ad865d2f63是通過對(duì)隨機(jī)數(shù)變量RANDOM隨機(jī)執(zhí)行命令:
echo $RANDOM|md5sum|cut –c1-10
后的結(jié)果偎谁,請(qǐng)破解這些字符串對(duì)應(yīng)的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練習(xí)
1纲堵、每隔3秒鐘到系統(tǒng)上獲取已經(jīng)登錄的用戶的信息巡雨;如果發(fā)現(xiàn)用戶hacker登錄,則將登錄時(shí)間和主機(jī)記錄于日志/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席函、隨機(jī)生成10以內(nèi)的數(shù)字铐望,實(shí)現(xiàn)猜字游戲,提示比較大或小茂附,相等則退出
#!/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正蛙、用文件名做為參數(shù),統(tǒng)計(jì)所有參數(shù)文件的總行數(shù)
#!/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营曼、用二個(gè)以上的數(shù)字為參數(shù)乒验,顯示其中的最大值和最小值
#!/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`"