亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Shell腳本if else語(yǔ)句小結(jié)

 更新時(shí)間:2014年07月19日 10:48:02   投稿:junjie  
這篇文章主要介紹了Shell腳本if else語(yǔ)句小結(jié),總結(jié)了Shell腳本中的if控制語(yǔ)句和其它語(yǔ)言的不同,常見的3種寫法等,需要的朋友可以參考下

和Java、PHP等語(yǔ)言不一樣,sh的流程控制不可為空,如:

復(fù)制代碼 代碼如下:

<?php
if (isset($_GET["q"])) {
    search(q);
}
else {
    //do nothing
}
?>

在sh/bash里可不能這么寫,如果else分支沒有語(yǔ)句執(zhí)行,就不要寫這個(gè)else,就像這樣:

復(fù)制代碼 代碼如下:

if condition
then
    command1
    command2
    ...
    commandN
fi

當(dāng)然,也可以寫成一行(適用于終端命令提示符),像這樣:

復(fù)制代碼 代碼如下:

if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

末尾的fi就是if倒過(guò)來(lái)拼寫,后面還會(huì)遇到類似的。

if else格式

復(fù)制代碼 代碼如下:

if condition
then
    command1
    command2
    ...
    commandN
else
    command
fi

if else-if else格式

復(fù)制代碼 代碼如下:

if condition1
then
    command1
elif condition2
    command2
else
    commandN
fi

if else語(yǔ)句經(jīng)常與test命令結(jié)合使用,如下所示:

復(fù)制代碼 代碼如下:

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
    echo 'The two numbers are equal!'
else
    echo 'The two numbers are not equal!'
fi

輸出:
The two numbers are equal!

相關(guān)文章

最新評(píng)論