鳥哥的Linux私房菜讀書筆記--迴圈loop
1、while do done ,until do done(不定迴圈)
while [ condition ] #中間括號內的狀態就是判斷式
do #迴圈的開始
程式段落
done #迴圈的結束
當condition條件成立時,就終止迴圈,否則就持續進行迴圈的程式段
[[email protected]
temp]$ sh yes_to_stop.sh un please input yes/YES to stop this program;un please input yes/YES to stop this program;yes ok you input right #!/bin/bash #program # repeat question until user input correct answer #history # 20181011 lile first release PATH=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:/usr/local/sbin:~/temp export PATH while [ "${yn}" != "yes" -a "${yn}" != "YES" ] do read -p "please input yes/YES to stop this program;" yn done echo "ok you input right"
如果將while變成until
[[email protected] temp]$ sh yes_to_stop_until.sh yes
ok you input right
[[email protected] temp]$ sh yes_to_stop_until.sh no
ok you input right
#!/bin/bash
#program
# repeat question until user input correct answer
#history
# 20181011 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:/usr/local/sbin:~/temp
export PATH
until [ "${yn}" != "yes" -o "${yn}" != "YES" ]
do
read -p "please input yes/YES to stop this program;" yn
done
echo "ok you input right"
我們可以看出,不管輸入什麼,until都跳出迴圈
例:設計計算1+2+3+4+5+6……+100
#!/bin/bash
#program
# use loop to calculate "1 to 100' result
#history
# 20181011 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
s=0 #加總的數值變數 sum
i=0 #累加數值
while [ ${i} != "100" ]
do
i=$(($i+2))
s=$(($s+$i))
done
echo "total is $s"
2、for …in…do……done (固定迴圈)
相對於while與until的迴圈方式必須要符合某個條件的狀態,for語法則需要知道迴圈的次數,其語法結構如下:
for var in con1 con2 con3…… #var變數在迴圈時第一次的迴圈內容是con1,第二次迴圈內容是com2,以此類推
do
程式段
done
[[email protected] temp]$ vi for_animal.sh
#!/bin/bash
#program
# usring for ... loop to print 3 animals
#hisroty
# 20181012 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export=PATH
for animal in dog cat pig xiaolei
do
echo -e "there are ${animal} "
done
[[email protected] temp]$ sh for_animal.sh
there are dog
there are cat
there are pig
there are xiaolei
由於系統上面的各種賬號都是寫在/etc/passwd內的第一個欄位,,我們需要透過管線命令cut捉出單純的賬號名稱後,以id分別檢查使用者的識別符號和特殊引數。程式碼如下
如果我想利用 ping來判斷網路狀態,來進行網路狀態的實際偵測中,我要偵測的網域是本機所在的192.168.122.1~192.168.122
.100,指令如下:
#!/bin/bash
#program
# use ping command to check the network's pc state
#history
# 20181011 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
network="192.168.122" #定義網域的前一部分
for sitenu in $(seq 1 100) #seq為sequence(連續)的縮寫
do
ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1
#以下是進行迴圈判斷顯示正確的啟動(UP)還是錯誤的沒有連通(DOWN)
if [ "${result}" == 0 ];then
echo "server ${network}.${sitenu} is UP"
else
echo "server ${network}.${sitenu} is DOWN"
fi
done
除了使用$(seq 1 100) 以外,還可以直接使用bash的內建機制{1..100}來取代連續輸入的意思,中間以兩個小數點來表示連續出現的意思
例:使用者輸入某個目錄檔名,找出某目錄內的檔名的許可權。
#!/bin/bash
#program
# try do calculate 1+...+$(your input)
#history
# 20181012 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "please input a number ,it will count fot 1+...+" num
s=0 i=0
for((i=0;i<=${num};i++))
do
s=$((${s}+${i}))
done
echo -e "1+...+${num}=${s}"
3、for……do……done的數值處理
for((初始值;限制值;執行步階))
do
程式段
done
初始值:某個變數在迴圈當中的起始值,直接以類似i=1設定好
限制值:當變數的值在這個範圍內,就執行迴圈,例如i<=100
執行步階:每做一次迴圈時,變數的變化量。例如i=i+1,也可以使用i++
#!/bin/bash
#program
# try do calculate 1+...+$(your input)
#history
# 20181012 lile first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "please input a number ,it will count fot 1+...+" num
s=0 i=0
for((i=0;i<=${num};i++))
do
s=$((${s}+${i}))
done
echo -e "1+...+${num}=${s}"
4、搭配隨機數與陣列的實驗
例 :假設你所在的團隊,經常為吃午飯的選擇問題搞昏,,我們是否可以使用指令碼搭配隨機數來確定午餐。要達成這個任務,首先我們需要將全部的店家輸入到一組陣列當中,在透過隨機數的處理,去取得可能得數值,在將搭配到該數值的店家顯示出來。
#!/bin/bash
#/program
# try to chioce what us may eat
#history
# 20181012 LILE first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
eat[0]="humburger"
eat[1]="fried chicken"
eat[2]="handy"
eat[3]="freid bread stick"
eat[4]="instant noodles"
eatnum=5 #需要輸入可選擇餐廳數
check=$((${RANDOM}*${eatnum}/32767+1))
echo -e "we will eat ${eat[${check}]}"
每次顯示三個選擇
#!/bin/bash
#/program
# try to chioce what us may eat
#history
# 20181012 LILE first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
eat[0]="humburger"
eat[1]="fried chicken"
eat[2]="handy"
eat[3]="freid bread stick"
eat[4]="instant noodles"
eatnum=5
eated=0
while [ "${eated}" -lt 3 ];do
check=$((${RANDOM}*${eatnum}/32767+1)) #產生隨機選擇
mycheck=0
if [ "${eated}" -ge 1 ];then #如果eated>1,執行以下程式
for i in $(seq 1 ${eated}) #i從1增加到eated
do
if [ ${eatedcon[${i}]} == ${check} ];then #如果已經產生了的選擇
mycheck=1
mycheck=1
fi
done
fi
if [ ${mycheck} == 0 ];then #如果mycheck=0,顯示隨機選擇的結果
echo "your may eat ${eat[${check}]}"
eated=$((${eated}+1))
eatedcon[${eated}]=${check} #為了避免產生相同的選擇
fi
done