主頁 > 知識庫 > Shell腳本if else語句小結(jié)

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

熱門標(biāo)簽:百度地圖的地圖標(biāo)注 佛山高德地圖標(biāo)注中心 旅游地圖標(biāo)注大全 東莞電銷機(jī)器人價格一覽表 陜西電銷卡外呼系統(tǒng)怎么安裝 地圖標(biāo)注超出范圍怎么辦 杭州機(jī)器人外呼系統(tǒng) excel地址地圖標(biāo)注 百度地圖標(biāo)注圖標(biāo)更換

和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

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

復(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!

您可能感興趣的文章:
  • 基于shell的if和else詳解
  • Windows Powershell IF-ELSEIF-ELSE 語句
  • linux shell中 if else以及大于、小于、等于邏輯表達(dá)式介紹
  • 詳解Shell if else語句的具體使用方法

標(biāo)簽:延邊 青島 隨州 朝陽 通遼 南充 西藏 雅安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Shell腳本if else語句小結(jié)》,本文關(guān)鍵詞  Shell,腳本,else,語句,小結(jié),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Shell腳本if else語句小結(jié)》相關(guān)的同類信息!
  • 本頁收集關(guān)于Shell腳本if else語句小結(jié)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章