第四章 shell的控制流程

來源:互聯網
上載者:User

第四章 控制流程結構

1,if語句
if 條件1;

then
命令1
elif 條件2;
then
命令2
else
命令3
fi

if 條件
then 命令
fi
***************************************************************
#!/bin/bash
if [ "10" -lt "12" ]
then
echo "Yes,10 is less than 12"
else
echo "No!!"
fi

***************************************************************

條件的各種形式

expression
!expression 取否
expression1 -a expression2 相當於&&
expression1 -o expression2 ||
-n string 長度非空
-z string ch  為零
string = string
string !=string
integer1 -eq integer2
integer1 -ge integer2 相當於>
integer1 -le integer2 相當於<=
integer1 -lt integer2 相當於<
integer1 -ne integer2 相當於!=
file1 -ef file2 file1和file2有相同的裝置和節點數
file1 -nt file2 file1比fiel2要新
file1 -ot file2 file1要比file2舊
-b file file存在,並且是塊裝置
-c file file存在,並且是字元裝置
-d file file存在,並且是目錄
-e file 存在
-f file 存在 規則檔案
-g file
-G file
-k file
-L file 串連檔案
-O 有真實的使用者
-p file
-r file 可讀
-w file 可寫
-x file 可執行
***************************************************************
#!/bin/bash
echo -n "Enter your name:"
read NAME
if [ "$NAME" = “" ];then
echo "You do not enter anything"
fi
***************************************************************
***************************************************************
#!/bin/bash
if cp myfile.bak myfile; then
echo "good copy"
else echo "`basename $0`:erro could not copy" >&2
fi
***************************************************************
***************************************************************
#!/bin/bash
echo -n "Enter your name:"
read NAME
if [ -z $NAME ] || [ "$NAME" = "" ];then
echo "You did not enter a name."
elif [ "$NAME" = "chinalab" ];then
echo "Hello root"
elif [ "$NAME" = "chinalab" ];then
echo "Hello chinalab"
else
echo "You are not root chinalab , but hi ,$NAME"
fi

注意:等號兩邊需要留有空格。

***************************************************************

2,case語句
多選擇語句,可以用一個值與一個模式的匹配

case 值 in
模式1)
命令1;;
模式2)
命令2;;
esac

case取值後面必須為單詞in,每一個模式以右括弧結束。取值可以為變數或者常數。匹配發現取值符合某一模式後,期間所有命令開始執行直到;;結束。
***************************************************************
#!/bin/bash
echo -n "Enter a number from 1 to 3:"
read ANS
case $ANS in
1)
echo "You selected 1"
;;
2)
echo "You selected 2"
;;
3)
echo "You selected 3"
;;
*)
echo "`basename $0`:This is not between 1 and 3">&2
exit;
;;
esac
***************************************************************

3,for 迴圈

for 變數名 in 列表
do
命令1
命令2
done
當變數值在列表裡,for迴圈即執行一次所有命令,使用變數名訪問列表中的取值。命令可為任何有效shell命令和語句。in列表法是可選的,如果不用它,for迴圈使用命令列的位置參數。

***************************************************************
#!/bin/bash
for loop in 1 2 3 4 5
do 
echo $loop
done
***************************************************************
#!/bin/bash
for loop in "orange red bue grey"
do 
echo $loop
done
***************************************************************
4,until迴圈

until 條件
do
命令1
命令2
...
done

注意:條件為任意測試條件,測試發生在迴圈末尾,因此迴圈至少執行一次。
***************************************************************
#!/bin/bash
part="/backup"
LOOK_OUT=`df |grep "$part" | awk '{print $5}' | sed 's/%//g'`
echo $LOOK_OUT
until [ "$LOOK_OUT" -gt "90" ]
do 
echo "Filesystem /backup is nearly full" |mail root
LOOK_OUT=`df |grep "$part" | awk '{print $5}' | sed 's/%//g'`
sleep 3600
done 
***************************************************************
5,while迴圈

while 命令
do
命令1
命令2
...
done
***************************************************************
#!/bin/bash
echo "按住<ctrl>+D 退出輸入"
while echo -n "輸入你最喜歡的電影:";read FILM
do
echo "Yeah,${FILM}是一部好電影!"
done 
***************************************************************
#!/bin/bash
while read LINE
do
echo $LINE
done <reads.txt
***************************************************************

6,break 和continue

break [n] 退出迴圈,n表示要退出迴圈的層數
continue 跳過迴圈步

***************************************************************
#!/bin/bash
while : /*:說明永遠為真*/
do
echo -n "Enter any number [1...5]:"
read ANS
case $ANS in 
1|2|3|4|5)
echo "You enter a number between 1 and 5."
;;
*)
echo "Wrong number,Bye"
break
;;
esac
done
***************************************************************
#!/bin/bash
echo "Please Input a number between 1 to 5"
while :
do
read ANS
case $ANS in
1|2|3|4|5)
echo "The number you input is:$ANS"
;;
*)
echo "Wrong number,but ,will you continue?"
read Is_continue
case $Is_continue in
y|Y|yes|Yes)
continue
;;
*)
break;
;;
esac
esac
done
***************************************************************

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.