欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

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

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

if condition
then
    command1
    command2
    ...
    commandN
fi

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

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

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

末尾的fi就是if倒過來拼寫,后面還會遇到類似的。

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語句經(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)文章

最新評論