break與continue
與其他編程相同里的概念相同,代碼示例如下:
# break示例:
while true
do
echo -n "輸入 1 到 5 之間的數(shù)字:"
read aNum
case $aNum in
1|2|3|4|5) echo "你輸入的數(shù)字為 $aNum!"
;;
*) echo "你輸入的數(shù)字不是 1 到 5 之間的! 游戲結(jié)束"
break
;;
esac
done
# 輸出結(jié)果
輸入 1 到 5 之間的數(shù)字:3
你輸入的數(shù)字為 3!
輸入 1 到 5 之間的數(shù)字:7
你輸入的數(shù)字不是 1 到 5 之間的! 游戲結(jié)束
# continue示例
while true
do
echo -n "輸入 1 到 5 之間的數(shù)字:"
read aNum
case $aNum in
1|2|3|4|5) echo "你輸入的數(shù)字為 $aNum!"
;;
*) echo "你輸入的數(shù)字不是 1 到 5 之間的! 游戲結(jié)束"
continue
echo "游戲結(jié)束"
;;
esac
done
# 輸出結(jié)果,無(wú)法結(jié)束
輸入 1 到 5 之間的數(shù)字:3
你輸入的數(shù)字為 3!
輸入 1 到 5 之間的數(shù)字:7
你輸入的數(shù)字不是 1 到 5 之間的! 游戲結(jié)束
輸入 1 到 5 之間的數(shù)字:
select
選擇一個(gè)列表中的一個(gè)值,item列表值可以分別單獨(dú)寫(xiě)出來(lái),也可以是一個(gè)列表
語(yǔ)法格式如下:
select var in item1 item2..itemN
do
command
done
代碼示例如下:
list=(a b)
PS3="Please select the value:"
select var in ${list[*]};do
break
done
echo "your select is $var"
# 輸出結(jié)果
1) a
2) b
Please select the value:1
your select is a
1.9 函數(shù)
函數(shù)定義
shell中函數(shù)的定義格式如下, 其中return的返回值在[0-225] 之間,如果不加return將以最后一條命令運(yùn)行結(jié)果,作為返回值.
[ function ] funname()
{
action;
[return int;]
}
示例代碼:
# 完整格式定義函數(shù)
function test1() {
echo "hello world1"
return 0
}
# 簡(jiǎn)寫(xiě)的函數(shù)定義
test2() {
echo "hello world2"
}
# 函數(shù)調(diào)用
test1
# 獲取上一條指令的返回值,緊接著test1之后就是代表獲取test1函數(shù)的返回值
echo $?
test2
# 輸出結(jié)果
hello world1
0
hello world2
函數(shù)參數(shù)
在Shell中,調(diào)用函數(shù)時(shí)可以向其傳遞參數(shù)。在函數(shù)體內(nèi)部,通過(guò) $$
n 的形式來(lái)獲取參數(shù)的值,例如,
-
Linux
+關(guān)注
關(guān)注
87文章
11350瀏覽量
210462 -
文本編輯器
+關(guān)注
關(guān)注
0文章
28瀏覽量
8139 -
javascript
+關(guān)注
關(guān)注
0文章
525瀏覽量
53952 -
Shell腳本
+關(guān)注
關(guān)注
0文章
36瀏覽量
8029
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
Shell基礎(chǔ)知識(shí)(上)
Shell腳本編程實(shí)驗(yàn)
嵌入式和物聯(lián)網(wǎng)的shell腳本學(xué)習(xí)指南之shell腳本入門(mén)免費(fèi)下載
![嵌入式和物聯(lián)網(wǎng)的<b class='flag-5'>shell</b><b class='flag-5'>腳本</b>學(xué)習(xí)指南之<b class='flag-5'>shell</b><b class='flag-5'>腳本</b>入門(mén)免費(fèi)下載](https://file.elecfans.com/web1/M00/63/76/o4YBAFuXkYCALlMqAAHAKzTEIaQ501.png)
shell腳本最簡(jiǎn)明的教程
![<b class='flag-5'>shell</b><b class='flag-5'>腳本</b>最簡(jiǎn)明的教程](https://file.elecfans.com/web1/M00/68/8E/pIYBAFvFWqCAWecrAAARSOanVz0569.png)
Linux中shell腳本執(zhí)的4種方式
![Linux中<b class='flag-5'>shell</b><b class='flag-5'>腳本</b>執(zhí)的<b class='flag-5'>4</b>種方式](https://file.elecfans.com/web1/M00/BC/E5/o4YBAF7Ie0uAV-14AAApnLOBhTg151.jpg)
109個(gè)實(shí)用shell腳本分享
Linux開(kāi)發(fā)_Makefile規(guī)則與Shell腳本語(yǔ)言
shell腳本基礎(chǔ)知識(shí)匯總1
shell腳本基礎(chǔ)知識(shí)匯總2
shell腳本基礎(chǔ)知識(shí)匯總3
shell腳本基礎(chǔ)知識(shí)
Linux Shell腳本經(jīng)典案例分享
![Linux <b class='flag-5'>Shell</b><b class='flag-5'>腳本</b>經(jīng)典案例分享](https://file1.elecfans.com/web2/M00/89/DD/wKgZomSL-7eAcRBVAAA0YQQcU5I277.png)
評(píng)論