1、for循環
for 變量名 in 列表;do
循環體
done
執行機制:依次將列表中元素賦值給“變量名”;每次賦值后即執一次循環體;直到列表中元素耗盡循環結束
列表生成方式:
(1) 直接給出列表
(2) 整數列表:
(a){start..end}
(b) $(seq [start [step]] end)
(3) 返回列表的命令
$(COMMAND)或者`COMMAND`
(4) 使用glob,如:/root/bin/*.sh
(5) 變量引用;
$@, $*
2、while循環
while CONDITION; do
循環體
done
CONDITION(循環控制條件);進入循環之前,先做一次判斷;每一次循環之后會再次做判斷;條件為“true”,則執行一次循環;直到條件測試狀態為“false”終止循環,因此CONDTION 一般應該有循環控制變量,而此變量的值會在循環體中不斷地被修正
進入條件:CONDITION為true
退出條件:CONDITION為false
1、 until循環
until CONDITION; do
循環體
done
進入條件:CONDITION為false
退出條件:CONDITION為true
2、 循環控制語句continue
用于循環體中
continue [N]:最內層為第1層,提前結束第N層的本輪循環,而直接進入下一輪判斷
示例1:添加用戶時,若某用戶如user2存在,則結束當次if-fi,進入下一個用戶user3的if-fi循環
#!/bin/bash
for username in user{1..10};do
if grep "^$username\>" /etc/passwd;then
continue
else
useradd $username
echo $username|passwd –stdin $username
fi
done
示例2:#!/bin/bash
i=0
until false;do
echo $i;let i++;sleep 1;[ $i -eq 10 ] && continue;echo xx
done
輸出的10后面沒有xx,緊接著就輸出11xx、12xx等等
由于sleep命令,例如0單獨一次輸出,而后是xx1一塊輸出,接著是xx2一塊輸出等等
3、 循環控制語句break
用于循環體中
break [N]:提前結束第N 層整層循環,最內層為第1層
示例1:當輸出9時,break所在的整層until循環結束,但是until外的echo xxxhhh命令仍然會執行,所有最后輸出1-9和xxxhhh
#!/bin/bash
i=1
until false ;do
[ $i -eq 10 ] && break
echo $i
let i++
done
echo xxxhhh
4、 創建無限循環
while true; do
循環體
done
until false; do
循環體
done
練習題:
1、 判斷/var/目錄下所有文件的類型
第一種方法:
#!/bin/bash
for file in `ls -A /var`;do
if [ -h /var/$file ];then
echo "the file is link"
elif [ -d /var/$file ];then
echo "the file is directory"
elif [ -f /var/$file ];then
echo "the file is commen"
else
echo "the file is other"
fi
done
第二種方法:
腳本filetype.sh
#!/bin/bash
read -p "please input the file path:" a
[ ! -e $a ] && echo "the file not exist" && exit
if [ -h $a ];then
echo "this file is link"
elif [ -d $a ];then
echo "this file is directory"
elif [ -f $a ];then
echo "this file is commen"
else
echo "this file is other"
fi
腳本filevartype1.sh
#!/bin/bash
for file in `ls -A /var`;do
echo /var/$file |./filetype.sh
done
注意其中調用腳本時應寫處調用腳本的絕對路徑或者相對路徑
2、 添加10個用戶user1-user10 ,密碼同用戶名
#!/bin/bash
for username in user{1..10};do
if grep "^$username\>" /etc/passwd;then
continue
else
useradd $username;echo $username|passwd –stdin $username
fi
done
3、/etc/rc.d/rc3.d 目錄下分別有多個以K 開頭和以S 開頭的文件;分別讀取每個文件,以K開頭的文件輸出為文件加stop,以S 開頭的文件輸出為文件名加start
“K34filename stop”
“S66filename start”
#!/bin/bash
for file in `ls -A /etc/rc.d/rc3.d`;do
if echo $file |grep "^K" >>/dev/dull;then
echo "$file stop"
elif echo $file|grep "^S" >>/dev/dull;then
echo "$file start"
else
echo "$file other"
fi
done
4、 寫 一個腳本,提示輸入正整數n 的值,計算1+2+3+…n的總和
#!/bin/bash
sum=0
read -p "please input the positive integer:" a
for i in `seq 1 $a`;do
sum=$[sum+i]
done
echo "the total is $sum"
5、 寫一個腳本,提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機在線狀態
#!/bin/bash
read -p "please input the ip:" a
b=`echo $a|grep -o "^.*\."`
sumtrue=0
sumfalse=0
for ip in {230..255};do
ping -c1 -W1 $b$ip &>/dev/dull && let sumtrue++ || let sumfalse++
done
echo "the ip is on:$sumtrue"
echo "the ip is off:$sumfalse"
6、打印九九乘法表
#1/bin/bash
b=0
for i in {1..9};do
let b++
for j in `seq 1 $b`;do
echo -en "$j*$i=$[j*i] \t" 內層循環不換行,但是在每次循環輸出后面插入tab
done
echo 外層循環一次換一次行
done
1 、求100以內所有正整數之和
#!/bin/bash
i=1
sum=0
while [ $i -le 100 ];do
sum=$[sum+i]
let i++
done
echo "the total is $sum"
2 、通過ping 命令探測10.1.252.230-254 范圍內的所有主機的在線狀態,統計在線主機和離線主機各多少
#!/bin/bash
b=`echo "10.1.252.0"|grep -o "^.*\."`
sumtrue=0
sumfalse=0
id=230
while [ $id -le 254 ];do
ping -c1 -W1 $b$ip &>/dev/dull && let sumtrue++ || let sumfalse++
let id++
done
echo "the ip is on:$sumtrue"
echo "the ip is off:$sumfalse"
3 、打印九九乘法表
#!/bin/bash
b=1
i=1
while [ $i -le 9 ];do
j=1 注意j的初始值應該賦予此處,每次開始執行內循環時,j的值都從1開始
while [ $j -le $b ];do
echo -en "$j*$i=$[j*i] \t"
let j++
done
let b++
let i++
echo
done
4 、利用變量RANDOM生成10 個隨機數字,輸出這個10 數字,并顯示其中的最大者和最小者
#!/bin/bash
i=1
maxa=$RANDOM
mina=$maxa
echo $maxa
while [ $i -le 9 ];do
mid=`echo $RANDOM`
echo $mid
if [ $mid -ge $maxa ];then
maxa=$mid
elif [ $mid -le $mina ];then
mina=$mid
fi
let i++
done
echo "the max is $maxa"
echo "the min is $mina"
5、打印國際象棋棋盤
#!/bin/bash
i=1
while [ $i -le 8 ];do
j=1
while [ $j -le 8 ];do
a=$[i%2]
b=$[j%2]
if [ $a -gt $b -o $a -lt $b ];then
echo -en "\033[40m \033[0m"
let j++
elif [ $a -eq $b ];then
echo -en "\033[47m \033[0m"
let j++
fi
done
let i++
echo
done
1、 求100 以內所有正整數之和
#!/bin/bash
i=1
sum=0
until [ $i -gt 100 ];do
sum=$[sum+i]
let i++
done
echo "the total is $sum"
2、 通過ping 命令探測10.1.252.230-254 范圍內的所有主機的在線狀態,統計在線主機和離線主機各多少
#!/bin/bash
b=`echo "10.1.252.0"|grep -o "^.*\."`
sumtrue=0
sumfalse=0
id=199
until [ $id -gt 254 ];do
ping -c1 -W1 $b$ip &>/dev/dull && let sumtrue++ || let sumfalse++
let id++
done
echo "the ip is on:$sumtrue"
echo "the ip is off:$sumfalse"
3、 打印九九乘法表
#!/bin/bash
b=1
i=1
until [ $i -gt 9 ];do
j=1
until [ $j -gt $b ];do
echo -en "$j*$i=$[j*i] \t"
let j++
done
let b++
let i++
echo
done
4、 利用變量RANDOM 生成10 個隨機數字,輸出這個10 數字,并顯示其中的最大者和最小者
#!/bin/bash
maxa=$RANDOM
mina=$maxa
echo $maxai
until [ $i -ge 10 ];do
mid=`echo $RANDOM`
echo $mid
if [ $mid -ge $maxa ];then
maxa=$mid
elif [ $mid -le $mina ];then
mina=$mid
fi
let i++
done
echo "the max is $maxa"
echo "the min is $mina"
5、打印國際象棋棋盤
#!/bin/bash
i=1
until [ $i -gt 8 ];do
j=1
until [ $j -gt 8 ];do
a=$[i%2]
b=$[j%2]
if [ $a -gt $b -o $a -lt $b ];then
echo -en "\033[40m \033[0m"
let j++
elif [ $a -eq $b ];then
echo -en "\033[47m \033[0m"
let j++
fi
done
let i++
echo
done
原創文章,作者:18612763863,如若轉載,請注明出處:http://www.www58058.com/37063