SHELL腳本之for、while循環經典例題
阿新 • • 發佈:2019-01-13
one let 登錄 dev 判斷 etc 所有 abcdefg tlist 1、創建用戶的腳本:(標準版本)
[ ! -f "$1" ] && echo "$1 file is not exit ! " && exit
STUDEN_FILE = $ 1
for USER in
if id ${USER} &> /dev/null;then
echo " 用戶: ${USER}已存在,創建失敗 "
else
useradd ${USER}
echo ${USER} | passwd --stdin ${USER} &> /dev/null
echo "用戶:${USER}創建成功"
fi
done
註:/dev/urandom 本文件可生成隨機字符
2、提示請輸入網址如172.20.119 判斷輸入的網段中主機在線狀態:
NET=172.20.119
START=1
END=254
for HOSTID in
{
if ping -c1 -w1 ${NET}.${HOSTID} &> /dev/null;then
echo ${NET}.${HOSTID} is up
echo ${NET}.${HOSTID} >> hostlist。txt
else
echo ${NET}.${HOSTID} is down
fi
}&
done
wait
3、判斷/var/目錄下所有文件的類型
for i in $(find /var) ;do
if [ -b $i ];then
echo "$i是塊設備"
elif [ -c $i ];then
echo "$i是字符設備"
elif [ -f $i ];then
echo "$i是普通文件"
elif [ -h $i ];then
echo "$i是符號鏈接文件"
elif [ -p $i ];then
echo "$i是管道文件"
elif [ -s $i ];then
echo "$i是 套接文件"
elif [ -d $i ];then
echo "$i是目錄文件"
else
echo “文件或目錄不存在”
fi
done
exit 0
4、目錄下分別有多個以K開頭和以S開頭的文件;分別讀取每個文件, 以K開頭的輸出為文件加stop,以S開頭的輸出為文件名加start。
for i in /etc/rc.d/rc3.d/k;do
echo $i stop
done
for i in /etc/rc.d/rc3.d/s;do
echo $i start
done
5、提示輸入正整數n的值,計算1+2+…+n的總和
read -p "please input a positive integer:" n
i=1
sum=0
for i in
let sum+=i
done
echo "sum is $sum"
6、計算100以內所有能被3整除的整數之和
for n in {1..100};do
if [ $[ $n%3 ] -eq 0 ];then
let sum+=n
fi
done
echo "sum is $sum”
7、打印九九乘法表
for i in {1..9};do
for j in
result = $[ $j$i ]
echo -e "${j}x${i}=$result\t\c"
done
echo
done
8、在/testdir目錄下創建10個html文件,文件名格式為數字N(從1到10)加隨機8個字 母,如: 1AbCdeFgH.html
if [ ! -d /testdir ];then
mkdir /testdir
fi
a=1
while [ $a -le 10 ];do
touch /testdir/$a$(tr -cd [:alnum:] < /dev/random |head -c 8).html
a=$(echo $a+1|bc)
done
9、打印等腰三角形
read -p "請輸入需要生成的等腰三角形的行數:" zongline
for curline in $(seq 1 $zongline);do
spacenum=$[$zongline-$curline]
anum=$[2$curline-1]
for kongge in $(seq 1 $spacenum);do
echo -e "" "\c"
done
for a in $(seq 1 $anum);do
echo -e "a\c"
done
echo
done
10、求100以內所有正奇數之和
sum=0
n=1
while [ $n -le 100 ]
do
sum=$((sum+n))
n=$((n+2))
done
echo $sum
11、提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機 在線狀態,並統計在線和離線主機各多少
read -p "please input nework(eg:192.168.0.0): " network
netid=
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
12、打印九九乘法表
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
let k=ij
echo -en "$i$j=$k\t"
let j++
done
echo
let i++
done
13、利用變量RANDOM生成10個隨機數字,輸出這個10數字,並顯 示其中的最大值和最小值
x=0
while [ $x -le 9 ];do
if [ $x -le 9 ];then
echo $RANDOM |tee -a /tmp/suiji.txt
else
break
fi
let x++
done
echo "max num is : $(sort -n /tmp/suiji.txt |tail -1)"
echo "min num is : $(sort -n /tmp/suiji.txt |head -1)"
14、實現打印國際象棋棋盤
for i in {1..8};do
for j in {1..8};do
if [ $[$i%2] -eq 1 ];then
if [ $[$j%2] -eq 1 ];then
echo -en "\033[47;37m \033[0m"
else
echo -en "\033[40;37m \033[0m"
fi
else
if [ $[$j%2] -eq 1 ];then
echo -en "\033[40;37m \033[0m"
else
echo -en "\033[47;37m \033[0m"
fi
fi
done
echo
done
15、後續六個字符串: efbaf275cd、 4be9c40b8b、 44b2395c46、 f8c8873ce0、 b902c16c8b、 ad865d2f63是通過對隨機數變量RANDOM隨機 執行命令: echo $RANDOM|md5sum|cut –c1-10 後的結果,請破解這些 字符串對應的RANDOM值
ps1=efbaf275cd
ps2=4be9c40b8b
ps3=44b2395c46
ps4=f8c8873ce0
ps5=b902c16c8b
ps6=ad865d2f63
echo ‘num ‘ ‘random‘
time=1
for i in {0..32767}
do
if [ $time -gt 6 ];then
break
fi
ps=$(echo $i | md5sum | cut -c1-10)
for j in $ps1 $ps2 $ps3 $ps4 $ps5 $ps6
do
if [ $ps == $j ];then
echo $i $ps
let time++
fi
done
done
16、每隔3秒鐘到系統上獲取已經登錄的用戶的信息;如果發現用戶hacker登錄, 則將登錄時間和主機記錄於日誌/var/log/login.log中,並退出腳本
until false;do
if who |grep "^hacker\>" &> /dev/null;then
who|grep "^hacker\>" > /var/log/login.log
break
fi
sleep 3
done
17、隨機生成10以內的數字,實現猜字遊戲,提示比較大或小,相等則退出
suiji=$[$RANDOM%10]
time=1
while [ $time -le 3 ];do
read -p "請輸入0-9的任意整數:" shuru
if [ $shuru -eq $suiji ];then
echo "猜中了"
break
elif [ $shuru -gt $suiji ];then
echo "請小一點"
echo "您還有$[3-$time]次機會"
let time++
elif [ $shuru -lt $suiji ];then
echo "請大一點"
echo "您還有$[3-$time]次機會"
let time++
fi
done
[ ! -f "$1" ] && echo "$1 file is not exit ! " && exit
STUDEN_FILE = $ 1
for USER in
cat $STUDEN_FILE
;doif id ${USER} &> /dev/null;then
echo " 用戶: ${USER}已存在,創建失敗 "
else
useradd ${USER}
echo ${USER} | passwd --stdin ${USER} &> /dev/null
fi
done
註:/dev/urandom 本文件可生成隨機字符
2、提示請輸入網址如172.20.119 判斷輸入的網段中主機在線狀態:
NET=172.20.119
START=1
END=254
for HOSTID in
seq $START $END
;do{
if ping -c1 -w1 ${NET}.${HOSTID} &> /dev/null;then
echo ${NET}.${HOSTID} is up
echo ${NET}.${HOSTID} >> hostlist。txt
echo ${NET}.${HOSTID} is down
fi
}&
done
wait
3、判斷/var/目錄下所有文件的類型
for i in $(find /var) ;do
if [ -b $i ];then
echo "$i是塊設備"
elif [ -c $i ];then
echo "$i是字符設備"
elif [ -f $i ];then
echo "$i是普通文件"
elif [ -h $i ];then
echo "$i是符號鏈接文件"
elif [ -p $i ];then
elif [ -s $i ];then
echo "$i是 套接文件"
elif [ -d $i ];then
echo "$i是目錄文件"
else
echo “文件或目錄不存在”
fi
done
exit 0
4、目錄下分別有多個以K開頭和以S開頭的文件;分別讀取每個文件, 以K開頭的輸出為文件加stop,以S開頭的輸出為文件名加start。
for i in /etc/rc.d/rc3.d/k;do
echo $i stop
done
for i in /etc/rc.d/rc3.d/s;do
echo $i start
done
5、提示輸入正整數n的值,計算1+2+…+n的總和
read -p "please input a positive integer:" n
i=1
sum=0
for i in
seq 1 $n
;dolet sum+=i
done
echo "sum is $sum"
6、計算100以內所有能被3整除的整數之和
for n in {1..100};do
if [ $[ $n%3 ] -eq 0 ];then
let sum+=n
fi
done
echo "sum is $sum”
7、打印九九乘法表
for i in {1..9};do
for j in
seq 1 $i
;doresult = $[ $j$i ]
echo -e "${j}x${i}=$result\t\c"
done
echo
done
8、在/testdir目錄下創建10個html文件,文件名格式為數字N(從1到10)加隨機8個字 母,如: 1AbCdeFgH.html
if [ ! -d /testdir ];then
mkdir /testdir
fi
a=1
while [ $a -le 10 ];do
touch /testdir/$a$(tr -cd [:alnum:] < /dev/random |head -c 8).html
a=$(echo $a+1|bc)
done
9、打印等腰三角形
read -p "請輸入需要生成的等腰三角形的行數:" zongline
for curline in $(seq 1 $zongline);do
spacenum=$[$zongline-$curline]
anum=$[2$curline-1]
for kongge in $(seq 1 $spacenum);do
echo -e "" "\c"
done
for a in $(seq 1 $anum);do
echo -e "a\c"
done
echo
done
10、求100以內所有正奇數之和
sum=0
n=1
while [ $n -le 100 ]
do
sum=$((sum+n))
n=$((n+2))
done
echo $sum
11、提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機 在線狀態,並統計在線和離線主機各多少
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
12、打印九九乘法表
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
let k=ij
echo -en "$i$j=$k\t"
let j++
done
echo
let i++
done
13、利用變量RANDOM生成10個隨機數字,輸出這個10數字,並顯 示其中的最大值和最小值
x=0
while [ $x -le 9 ];do
if [ $x -le 9 ];then
echo $RANDOM |tee -a /tmp/suiji.txt
else
break
fi
let x++
done
echo "max num is : $(sort -n /tmp/suiji.txt |tail -1)"
echo "min num is : $(sort -n /tmp/suiji.txt |head -1)"
14、實現打印國際象棋棋盤
for i in {1..8};do
for j in {1..8};do
if [ $[$i%2] -eq 1 ];then
if [ $[$j%2] -eq 1 ];then
echo -en "\033[47;37m \033[0m"
else
echo -en "\033[40;37m \033[0m"
fi
else
if [ $[$j%2] -eq 1 ];then
echo -en "\033[40;37m \033[0m"
else
echo -en "\033[47;37m \033[0m"
fi
fi
done
echo
done
15、後續六個字符串: efbaf275cd、 4be9c40b8b、 44b2395c46、 f8c8873ce0、 b902c16c8b、 ad865d2f63是通過對隨機數變量RANDOM隨機 執行命令: echo $RANDOM|md5sum|cut –c1-10 後的結果,請破解這些 字符串對應的RANDOM值
ps1=efbaf275cd
ps2=4be9c40b8b
ps3=44b2395c46
ps4=f8c8873ce0
ps5=b902c16c8b
ps6=ad865d2f63
echo ‘num ‘ ‘random‘
time=1
for i in {0..32767}
do
if [ $time -gt 6 ];then
break
fi
ps=$(echo $i | md5sum | cut -c1-10)
for j in $ps1 $ps2 $ps3 $ps4 $ps5 $ps6
do
if [ $ps == $j ];then
echo $i $ps
let time++
fi
done
done
16、每隔3秒鐘到系統上獲取已經登錄的用戶的信息;如果發現用戶hacker登錄, 則將登錄時間和主機記錄於日誌/var/log/login.log中,並退出腳本
until false;do
if who |grep "^hacker\>" &> /dev/null;then
who|grep "^hacker\>" > /var/log/login.log
break
fi
sleep 3
done
17、隨機生成10以內的數字,實現猜字遊戲,提示比較大或小,相等則退出
suiji=$[$RANDOM%10]
time=1
while [ $time -le 3 ];do
read -p "請輸入0-9的任意整數:" shuru
if [ $shuru -eq $suiji ];then
echo "猜中了"
break
elif [ $shuru -gt $suiji ];then
echo "請小一點"
echo "您還有$[3-$time]次機會"
let time++
elif [ $shuru -lt $suiji ];then
echo "請大一點"
echo "您還有$[3-$time]次機會"
let time++
fi
done
SHELL腳本之for、while循環經典例題