shell結(jié)構(gòu)化命令if-then-else語句
在if-then語句中,不管命令是否成功執(zhí)行,你都只有一種選擇。如果命令返回一個非0退出狀態(tài)碼,則bash shell會繼續(xù)執(zhí)行腳本中的下一條命令。在這種情況下,如果能夠執(zhí)行另一組命令就好了。這正是if-then-else語句的作用。
if-then-else語句在語句中提供了另外一組命令:
if command then command else command fi
當(dāng)if語句中的命令返回退出狀態(tài)碼0時,then部分中的命令會被執(zhí)行,這跟普通的if-then語句一樣。當(dāng)if語句中的命令返回非0退出狀態(tài)碼時,bash shell會執(zhí)行else部分中的命令。
Shell腳本首先判斷文件test1是否可讀,如果是,則輸出 is readable !的提示信息;否則不進(jìn)行任何動作。
[root@localhost 20190105]# vi test.sh filename=test1 if [ -r $filename ] //輸出test1可讀則輸出信息 then echo $filename' is readable !' fi [root@localhost 20190105]# sh test.sh test1 is readable !
Shell腳本會判斷number變量是否等于100,如果是,則輸出 The number is equal 100 !的提示;否則輸出 The number is not equal 100 !。
[root@localhost 20190105]# vi number.sh number=200 if [ $number -eq 100 ] //如果number等于100則輸出“The number is equal 100 !”提示 then echo 'The number is equal 100 !' else //否則輸出“The number is not equal 100 !”提示 echo 'The number is not equal 100 !' fi [root@localhost 20190105]# sh number.sh The number is not equal 100 !
Shell腳本首先判斷number變量是否小于10,如果是則輸出 The number < 10 !;否則,判斷number變量是否大于等于10且小于20。
如果是則輸出 10 =< The number < 20 !;否則,判斷 number變量是否大于等于20且小于30。
如果是,則輸出 20 =< The number < 30 !;否則,輸出 30 <= The number !。
[root@localhost 20190105]# vi number1.sh number=25 if [ $number -lt 10 ] //如果number小于10 then echo 'The number < 10 !' elif [ $number -ge 10 -a $number -lt 20 ] //如果number大于等于10且小于20 then echo '10 =< The number < 20 !' elif [ $number -ge 20 -a $number -lt 30 ] //如果number大于等于20且小于30 then echo '20 =< The number < 30 !' else //除上述3種情況以外的其他情況 echo '30 <= The number !' fi [root@localhost 20190105]# sh number1.sh 20 =< The number < 30 !
現(xiàn)在你可以復(fù)制并修改測試腳本,加入else部分:
$ cp test3.sh test4.sh $ $ nano test4.sh $ $ cat test4.sh #!/bin/bash # testing the else section # testuser=NoSuchUser # if grep $testuser /etc/passwd then echo "The scropt files in the home directory of $testuser are:" ls /home/$testuser/*.sh echo else echo "The user $testuser does not exist on this system." echo fi echo "We are outside the if statement" $ $ ./test4.sh The user NoSuchUser does not exist on this system. We are outside the if statement
這樣就友好多了。跟then部分一樣,else部分可以包含多條命令。fi語句說明else部分結(jié)束。
到此這篇關(guān)于shell結(jié)構(gòu)化命令if-then-else語句的文章就介紹到這了,更多相關(guān)shell if-then-else內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux shell中“.” 和 “./”執(zhí)行的區(qū)別詳解
這篇文章主要介紹了linux shell中“.” 和 “./”執(zhí)行的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05PowerShell實(shí)現(xiàn)簡單的grep功能
下面的PS腳本針對目錄和文件進(jìn)行了區(qū)分,借用Select-String命令,實(shí)現(xiàn)了內(nèi)容查找,并顯示查找到的文件和匹配內(nèi)容所在行號。感興趣的朋友一起看看吧2017-10-10Shell常見知識 方便想學(xué)習(xí)linux shell的彭玉
本文給大家介紹了一些Shell小知識,供參考學(xué)習(xí)2013-01-01shell腳本實(shí)現(xiàn)定時刪除文件或文件夾
本文主要介紹了shell腳本實(shí)現(xiàn)定時刪除文件或文件夾,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Shell腳本中調(diào)用、引用、包含另外一個腳本文件的兩種方法
這篇文章主要介紹了Shell腳本中調(diào)用、引用、包含另外一個腳本文件的兩種方法,本文介紹的兩種方法適合在當(dāng)前目錄下,需要的朋友可以參考下2014-12-12