shell编程--流程控制for,do-while,if-then,break,continue,case等
2.5 流程控制
2.5.1 if語法
1、語法格式
if?condition?
then?
????statements?
[elif?condition?
????then?statements.?..]?
[else?
????statements?]?
fi
?
2、示例
| #!/bin/bash read -p "please input your name:" NAME????? ###read命令用于從控制臺讀取輸入數據 printf '%s\n' $NAME if [ $NAME = root ]?????????????????????? ##注意if和后面的[]有間距 then ??? echo "hello ${NAME},welcome!" elif [ $NAME = toto ] then ??? echo "hello ${NAME},welcome!" else ??? echo "SB,get out here!" fi |
腳本內容截圖:
?
3、判斷條件
1/ 條件判斷基本語法
[ condition ] ??(注意condition前后要有空格)
#非空返回true,可使用$?驗證(0為true,>1為false)
[ itcast ]
#空返回false
[? ]
?
注意[? ]內部的=周邊的空格,有區別:
| [root@shizhan01 scripts]# if [ a = b ];then echo ok;else echo notok;fi notok [root@shizhan01 scripts]# if [ a=b ];then echo ok;else echo notok;fi ok |
?
短路(理解為三元運算符)
[ condition ] && echo OK || echonotok
???????? 條件滿足,執行&&后面的語句;條件不滿足,執行|| 后面的語句
?
2/ 條件判斷組合
注:[] 與[[ ]] 的區別:[[ ]] 中邏輯組合可以使用 &&? || 符號
而[] 里面邏輯組合可以用? -a?? -o
| [root@mini ~]# if [ a = b && b = c ]; then echo ok;else echo notok;fi -bash: [: missing `]' notok ? [root@mini ~]# if [ a = b -a b = b ]; then echo ok;else echo notok;fi notok [root@mini ~]# if [ a = b -o b = b ]; then echo ok;else echo notok;fi ok ? [root@mini ~]# if [[ a = b && b = b ]]; then echo ok;else echo notok;fi notok [root@mini ~]# if [[ a = b || b = b ]]; then echo ok;else echo notok;fi ok |
?
?
3/ 常用判斷運算符
字符串比較:=??? !=?????
-z 字符串長度是為0返回true
-n 字符串長度是不為0返回true
if [ 'aa' = 'bb' ]; then echo ok; else echonotok;fi
if [ -n "aa" ]; then echo ok;else echo notok;fi
if [ -z "" ]; then echo ok; elseecho notok;fi
?
整數比較:
-lt 小于
-le 小于等于
-eq 等于
-gt 大于
-ge 大于等于
-ne 不等于
?
?
文件判斷:
-d 是否為目錄
if [ -d /bin ]; then echo ok; else echonotok;fi
-f 是否為文件
if [ -f /bin/ls ]; then echo ok; elseecho notok;fi
-e 是否存在
if [ -e /bin/ls ]; then echo ok; elseecho notok;fi
?
2.5.2 while語法
1、方式一
while expression
do
command
…
done
?
2、方式二
i=1
while ((i<=3))
do
?echo $i
? leti++
done
?
while.sh腳本的內容如下:
| #!/bin/bash i=1 while((i<=3)) do ??? echo $i ??? let i++ done |
while2.sh的腳本內容如下:
| #!/bin/bash echo '按下 <CTRL-D> 退出' echo -n '輸入你最喜歡的電影名:' while read FILM do ??? echo "是的! $FILM 是一部好電影" done |
?
2.5.3 case語法
Shell case語句為多選擇語句。可以用case語句匹配一個值與一個模式,如果匹配成功,執行相匹配的命令。case語句格式如下:
| case 值 in 模式1) ??? command1 ??? command2 ??? ... ??? commandN ??? ;; 模式2) ??? command1 ??? command2 ??? ... ??? commandN ??? ;; esac |
case工作方式如上所示。取值后面必須為單詞in,每一模式必須以右括號結束。取值可以為變量或常量。匹配發現取值符合某一模式后,其間所有命令開始執行直至;;。
取值將檢測匹配的每一個模式。一旦模式匹配,則執行完匹配模式相應命令不再繼續其他模式。如果無一匹配模式,使用星號*捕獲該值,再執行后面的命令。
下面的腳本提示輸入1到4,與每一種模式進行匹配:
case.sh案例如下:
| #!/bin/bash echo '輸入 1 到 4 之間的數字:' echo '你輸入的數字為:' read aNum case $aNum in ??? 1) echo '你選擇了 1 ' ??? ;; ??? 2) echo '你選擇了 2' ??? ;; ??? 3) echo '你選擇了 3' ??? ;; ??? 4) echo '你選擇了 4' ??? ;; ??? *) echo '你沒有輸入 1 到 4之間的數字' ??? ;; esac |
運行結果:
| [root@hadoop test]# sh case.sh 輸入 1 到 4 之間的數字: 你輸入的數字為: 4 你選擇了 4 [root@hadoop test]# sh case.sh 輸入 1 到 4 之間的數字: 你輸入的數字為: 3 你選擇了 3 |
?
再如例子case2.sh
| #!/bin/bash ? case $1 in start) ??? echo "starting" ??? ;; stop) ??? echo "stoping" ??? ;; *) ??? echo "Usage:{start | stop}" esac |
運行結果:
| [root@hadoop test]# sh case2.sh start starting |
?
2.5.4 for語法
1、方式一
| #!/bin/bash ? for N in 1 2 3 do ??? echo $N done |
| 運行結果: [root@hadoop test]# sh for.sh 1 2 3 |
或
| #!/bin/bash ? for N in {1..5} do ??? echo $N done |
| 運行結果: [root@hadoop test]# sh for.sh 1 2 3 4 5 |
?
再如如下例子:
| [root@hadoop test]# ls a.sh????? b.sh????? case.sh????? for.sh????? hello.sh? login.log? while2.sh break.sh? case2.sh? continue.sh? fortest.sh? if.sh???? test.txt?? while.sh [root@hadoop test]# cat fortest.sh #!/bin/bash for shname in `ls *.sh` do ??? name=`echo "$shname" | awk -F. '{print $1}'` ??? echo $name done [root@hadoop test]# sh fortest.sh a break b case2 case continue for fortest hello if while2 while |
通過shell來統一放開端口
| 一簡單的字符串 枚舉遍歷法,利用for in格式對字符串按空格切份的功能 |
再如例子:
第一類:數字型循環
#!/bin/bashfor((i=1;i<=10;i++)); do echo $(expr $i \* 3 + 1); done #!/bin/bashfor i in $(seq 1 10) do echo $(expr $i \* 3 + 1); done #!/bin/bashfor i in {1..10} do echo $(expr $i \* 3 + 1); done #!/bin/bashawk 'BEGIN{for(i=1; i<=10; i++) print i}'?第二類:字符型循環
#!/bin/bashfor i in `ls`; do echo $i is file name\! ; done #!/bin/bashfor i in $* ; do echo $i is input chart\! ; #!/bin/bashfor i in f1 f2 f3 ; do echo $i is appoint ; done #!/bin/bashlist="rootfs usr data data2" for i in $list; do echo $i is appoint ; done第三類:路徑查找
#!/bin/bashfor file in /proc/*; do echo $file is file path \! ; done #!/bin/bashfor file in $(ls *.sh) do echo $file is file path \! ; done判斷文件是否存在
#shell判斷文件夾是否存在#如果文件夾不存在,創建文件夾 if [ ! -d "/Top" ]; thenmkdir -p /Topfi#shell判斷文件,目錄是否存在或者具有權限folder="/Top" file="/Top/test.txt"# -x 參數判斷 $folder 是否存在并且是否具有可執行權限 if [ ! -x "$folder"]; thenmkdir "$folder" fi# -d 參數判斷 $folder 是否存在 if [ ! -d "$folder"]; thenmkdir "$folder" fi# -f 參數判斷 $file 是否存在,加!表示不存在 if [ ! -f "$file" ]; thentouch "$file" fi# -n 判斷一個"變量"是否有值 if [ ! -n "$file" ]; thenecho "$file 變量為空!"exit 0 fi# 判斷兩個變量的字符串內容是否相同 if [ "$file1" = "$file2" ]; thenecho "$file1 equal $file2" elseecho "$file1 not equal $file2" fi代碼實現二:
#!/bin/sh # 說明:判斷文件是否存在myPath="/Top" myFile="/Top/access.log"# 這里的-x 參數判斷$myPath是否存在并且是否具有可執行權限 if [ ! -x "$myPath"]; thenmkdir "$myPath" fi # 這里的-d 參數判斷$myPath是否存在 if [ ! -d "$myPath"]; thenmkdir "$myPath" fi# 這里的-f參數判斷$myFile是否存在 if [ ! -f "$myFile" ]; thentouch "$myFile" fi# 其他參數還有-n,-n是判斷一個變量是否是否有值 if [ ! -n "$myVar" ]; thenecho "$myVar 變量為空!"exit 0 fi# 判斷$file字符串內容是否是“123123”相同 if [ "$file1" = "123123" ]; thenecho "$file1 equal $file2" elseecho "$file1 not equal $file2" fi?
2、方式二
for ((i = 0; i <= 5; i++))
do
???????? echo"welcome $i times"
done
或
for ((i = 0; i <= 5; i++)); do echo"welcome $i times"; done
?
定義for2.sh
| #!/bin/bash for ((i=0;i<=5;i++)) do ??? echo "welcome $i times" done |
| 運行結果如下: [root@hadoop test]# sh for2.sh welcome 0 times welcome 1 times welcome 2 times welcome 3 times welcome 4 times welcome 5 times |
?
2.5.5.跳出循環
在循環過程中,有時候需要在未達到循環結束條件時跳出循環,Shell使用兩個命令來實現該功能:break和continue。
break命令
break命令允許跳出所有循環(終止執行后面的所有循環)
下面的例子中,腳本進入死循環直到用戶輸入數字大于5。要跳出這個循環,返回到shell提示符下,需要使用break名。
| #!/bin/bash while : do ??? echo -n "輸入 1 到 5 之間的數字:" ??? read aNum ??? case $aNum in ??????? 1|2|3|4|5) echo "你輸入的數字為 $aNum!" ??????? ;; ??????? *) ??????????? echo "你輸入的數字不是 1 到 5 之間的!游戲結束" ??????? break ??? esac done |
執行以上的代碼,輸入結果為:
| [root@hadoop test]# sh break.sh 輸入 1 到 5 之間的數字:5 你輸入的數字為 5! 輸入 1 到 5 之間的數字:7 你輸入的數字不是 1 到 5 之間的!游戲結束 |
continue
continue命令與break命令類似,只有一點差別,它不會跳出所有循環,僅僅跳出當前循環。對上面的例子進行修改:
| #!/bin/bash while : do ??? echo -n "輸入 1 到 5 之間的數字:" ??? read aNum ??? case $aNum in ??????? 1|2|3|4|5) echo "你輸入的數字為$aNum!" ??????? ;; ??????? *) echo "你輸入的數字不是 1 到 5 之間的!" ???????????? continue ???????????? echo "游戲結束" ??????? ;; ??? esac done |
運行代碼,結果如下:
| [root@hadoop test]# sh continue.sh 輸入 1 到 5 之間的數字:7 你輸入的數字不是 1 到 5 之間的! 輸入 1 到 5 之間的數字:2 你輸入的數字為2! 輸入 1 到 5 之間的數字:2 你輸入的數字為2! 輸入 1 到 5 之間的數字:5 你輸入的數字為5! |
?
總結
以上是生活随笔為你收集整理的shell编程--流程控制for,do-while,if-then,break,continue,case等的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美原油开盘收盘时间
- 下一篇: 函数定义、函数参数、跨脚本调用函数