順序執行
從上到下逐條執行命令,執行完所有命令及退出。
選擇執行
(1)&&,||
&&:代表and,&&左側為真右側則執行
||:代表or,||左側為假右側則執
(2)if語句
代碼有一個分支;條件滿足時才會執行;兩個或以上的分支;只會執行其中一個滿足條件的分支;
單分支的if語句
if 測試條件;then
代碼分支
fi
雙分支的if語句:
if 測試條件;then
條件為真時執行的分支
else
條件為假時執行的分支
fi
示例:通過參數傳遞一個用戶名給腳本,此用戶不存在,則添加之
#!/bin/bash # # [ $# -eq 0 ] && echo "enter username" && exit 1 if id $1 &> /dev/null;then echo "the is username exists" exit 2 else useradd $1 && echo "useradd finish" fi
多分支的if語句
if condition1;then
條件1為真分支;
elif condition2;then
條件2為真分支
elif condition3;then
條件3為真分支
…
elif conditionN;then
條件n為真分支
else
所有條件均不滿足時的分組
fi
注意:即便多個條件可能同時都能滿足,分支只會執行其中一個,首先測試為“真”的那個;
示例:腳本參數傳遞一個文件路徑給腳本,判斷此文件的類型;
#!/bin/bash # # if [ $# -lt 1 ];then echo "enter one filepath" exit 1 fi if [ -L $1 ];then echo "symbolic link" elif [ -d $1 ];then echo "Directory" elif [ -b $1 ];then echo "block device file" elif [ -c $1 ];then echo "character special file" elif [ -f $1 ];then echo "Common file" elif [ -p $1 ];then echo "pipe" elif [ -S $1 ];then echo "Socket file" else echo "Alien" fi
注意:-L判斷軟連接需要寫在判斷首行,直接在命令行鍵入 file /var/* 就可以查看文件類型,file命令本身就是查看文件類型的
(3)case語句
case varname in
pattern1)
suite1
;;
pattern2)
suite2
;;
…
patternn)
suitenn
;
*)
other_suiten
;;
esac
;;雙分號結尾,單分號表示結束,case是一直比較??蓡为毘尚谢蛘咴谧詈笠恍忻钭詈?/p>
case中各pattern可以使用模式
a|b:a或b
*:匹配任意長度的任意字符
?:匹配任意單個字符
[-]:范圍匹配
[a-z])
[0-9])
示例;讓用戶輸入一個選項,根據其選項給出對應的信息
#!/bin/bash # # cat <<EOF cpu) display cpu information mem) display memory iniformation disk) dispaly disks iniformetion quit) quit ================================ EOF read -p "enter your option:" option while [ $option != "cpu" -a $option != "mem" -a $option != "disk" -a $option != "quit" ];do echo "enter cpu mem disk quit" read -p "enter your option again:" option done case $option in cpu) lscpu ;; mem) free -m ;; disk) fdisk -l /dev/[hs]d[a-z] ;; *) echo "quit" exit 1 ;; esac
循環執行
將一段代碼重復執行0,1,多次;
進入條件:只有在條件滿足時才進入循環
退出條件:每個循環都應該有退出條件,以有機會退出循環;
for循環
兩種格式:
(1)遍歷列表
(2)控制變量
for循環格式
==================================
for 變量名字 in list; do
循環體
done
==================================
遍歷列表:
進入條件:只要列表有可用元素,即可進入循環;
退出條件;列表中的原素遍歷完成
list的生成方式
(1)直接給出
#!/bin/bash # # for username in user22 user23 user24 ;do useradd $username done
(2)整數列表
#!/bin/bash # # for filename in 1 2 3 4 5;do touch $filename done
(a){start..end}
#!/bin/bash # # declare -i sum=0 for i in {1..100};do let sum=sum+i done echo "$sum"
(b)seq[start [incremtal]]last
#!/bin/bash # # for filename in `seq 10`;do touch $filename done
(3)返回列表的命令
(4)glob
(5)變量引用
#!/bin/bash # # # username=`cut -d: -f1 /etc/passwd` for userid in $username;do id $userid done
#!/bin/bash # # declare -i sum=0 id=`cut -d: -f3 /etc/passwd` for uid in $id;do let sum=sum+uid done echo $sum
while循環
進入條件:condition測試為"真"
退出條件:condition測試為"假"
while循環格式
=========================================
while condition;do
循環體
循環控制變量修正表達式
done
=========================================
示例
#!/bin/bash # # declare -i i=0 declare -i sum=0 while [ $i -le 100 ];do let sum=sum+i let i++ done echo $sum
until循環
進入條件:condition測試為"假"
退出條件:condition測試為"真"
until循環格式
=============================================
until condition;do
循環體
循環控制變量修正表達式
done
=============================================
示例
#!/bin/bash # # declare -i i=0 declare -i sum=0 until [ $i -gt 100 ];do let sum=sum+i let i++ done echo $sum
break:提前跳出循環
while condition1;do
cmd1
…
if condition1;then
break
fi
cmdn
…
done
示例
#!/bin/bash # # declare -i i=0 declare -i sum=0 while [ $i -le 100 ];do let sum=sum+i let i++ if [ $i -eq 50 ];then break fi done echo $sum
continue:提前結束本輪循環,而直接進入下一輪循環判斷;
while condition1;do
cmd1
…
if condition1;then
continue
fi
cmdn
…
done
#!/bin/bash # # declare -i i=0 declare -i sum=0 while [ $i -le 100 ];do let sum=sum+i let i++ if [ $i -eq 50 ];then continue fi sum1=$[sum+i] done echo $sum echo $sum1
特殊執行順序
創建死循環:
============================================================
while true;do
循環體
done
退出方式;
某個測試條件滿足時,讓循環體執行break命令
============================================================
示例
#!/bin/bash # # while true;do if who |grep '^hacker\>' &> /dev/null ;then break fi done echo "hacker is up" echo out now |write 'hacker'
sleep:命令
sleep – delay for a specified amount of time
延遲指令時間
sleep NUMBER[SUFFIX]…
while循環的特殊用法(遍歷文件的行)
====================================================================================
while read variable;do
循環體
done < /path/from/sumfile
依次讀取/path/from/somfile文件中的每一行,且將其賦值給variable變量;
====================================================================================
示例:找出id號為偶數的用戶,顯示其用戶名,ID和默認shell
#!/bin/bash # # while read line;do username=`echo $line |cut -d: -f1` userid=`echo $line |cut -d: -f3` usershell=`echo $line |cut -d: -f7` if [ $[$userid%2] -eq 0 ];then echo "${username} ${userid} ${usershell}" fi done </etc/passwd
for循環的特殊用法
==========================================================================================
for((控制變量初始化;條件判斷表達式;控制變量的修正語句));;do
循環體
done
控制變量初始化;僅在循環代碼開始運行時執行一次;
控制變量的修正語句;每輪循環結束后會先進行控制變量修正運算,而后再做條件判斷;
==========================================================================================
示例;打印九九乘法表(逆序)
#!/bin/bash # # for ((i=1;i<=9;i++));do for ((j=i;j<=9;j++));do echo -n "$i*$j=$[i*j] " done echo done
打印九九乘法表(順序)
#!/bin/bash # # for ((i=1;i<=9;i++));do for ((j=1;j<=i;j++));do echo -n "$j*$i=$[i*j] " done echo done
完
原創文章,作者:M20-1馬星,如若轉載,請注明出處:http://www.www58058.com/38331