昨天為大家介紹了getopt 命令的使用方法,今天南昌網(wǎng)絡(luò)公司-百恒網(wǎng)絡(luò)小編將為大家介紹一種更高級(jí) getopts命令的用法,供大家參考,學(xué)習(xí)。 getopts命令(注意是復(fù)數(shù))內(nèi)建于bash shell。它跟近親getopt看起來很像,但多了一些擴(kuò)展功能。
與getopt不同,前者將命令行上選項(xiàng)和參數(shù)處理后只生成一個(gè)輸出,而getopts命令能夠和已有的shell參數(shù)變量配合默契。
每次調(diào)用它時(shí),它一次只處理命令行上檢測(cè)到的一個(gè)參數(shù)。處理完所有的參數(shù)后,它會(huì)退出并返回一個(gè)大于0的退出狀態(tài)碼。這讓它非常適合用解析命令行所有參數(shù)的循環(huán)中。
getopts命令的格式如下:
getopts optstring variable
optstring值類似于getopt命令中的那個(gè)。有效的選項(xiàng)字母都會(huì)列在optstring中,如果選項(xiàng)字母要求有個(gè)參數(shù)值,就加一個(gè)冒號(hào)。要去掉錯(cuò)誤消息的話,可以在optstring之前加一個(gè)冒號(hào)。getopts命令將當(dāng)前參數(shù)保存在命令行中定義的variable中。
getopts命令會(huì)用到兩個(gè)環(huán)境變量。如果選項(xiàng)需要跟一個(gè)參數(shù)值,OPTARG環(huán)境變量就會(huì)保存這個(gè)值。OPTIND環(huán)境變量保存了參數(shù)列表中g(shù)etopts正在處理的參數(shù)位置。這樣你就能在處理完選項(xiàng)之后繼續(xù)處理其他命令行參數(shù)了。
下面百恒網(wǎng)絡(luò)小編為大家舉個(gè)使用getopts命令的例子,大家可以簡(jiǎn)單了解一下。
$ cat test19.sh
#!/bin/bash
# simple demonstration of the getopts command
#
echo
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option" ;;
*) echo "Unknown option: $opt";;
esac
done
$
$ ./test19.sh -ab test1 -c
Found the -a option
Found the -b option, with value test1
Found the -c option
$
while語句定義了getopts命令,指明了要查找哪些命令行選項(xiàng),以及每次迭代中存儲(chǔ)它們 的變量名(opt)。
你會(huì)注意到在本例中case語句的用法有些不同。getopts命令解析命令行選項(xiàng)時(shí)會(huì)移除開頭的單破折線,所以在case定義中不用單破折線。
getopts命令有幾個(gè)好用的功能。對(duì)新手來說,可以在參數(shù)值中包含空格。
$ ./test19.sh -b "test1 test2" -a
Found the -b option, with value test1 test2
Found the -a option
$
另一個(gè)好用的功能是將選項(xiàng)字母和參數(shù)值放在一起使用,而不用加空格。
$ ./test19.sh -abtest1
Found the -a option
Found the -b option, with value test1
$
getopts命令能夠從-b選項(xiàng)中正確解析出test1值。除此之外,getopts還能夠?qū)⒚钚猩险业降乃形炊x的選項(xiàng)統(tǒng)一輸出成問號(hào)。
$ ./test19.sh -d
Unknown option: ?
$
$ ./test19.sh -acde
Found the -a option
Found the -c option
Unknown option: ?
Unknown option: ?
$
optstring中未定義的選項(xiàng)字母會(huì)以問號(hào)形式發(fā)送給代碼。
getopts命令知道何時(shí)停止處理選項(xiàng),并將參數(shù)留給你處理。在getopts處理每個(gè)選項(xiàng)時(shí),它會(huì)將OPTIND環(huán)境變量值增一。在getopts完成處理時(shí),你可以使用shift命令和OPTIND值來移動(dòng)參數(shù)。
$ cat test20.sh
#!/bin/bash
# Processing options & parameters with getopts
#
echo
while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG" ;;
c) echo "Found the -c option" ;;
d) echo "Found the -d option" ;;
*) echo "Unknown option: $opt" ;;
esac
done
#
shift $[ $OPTIND - 1 ]
#
echo
count=1
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + 1 ]
done
#
$
$ ./test20.sh -a -b test1 -d test2 test3 test4
Found the -a option
Found the -b option, with value test1
Found the -d option
Parameter 1: test2
Parameter 2: test3
Parameter 3: test4
$
現(xiàn)在你就擁有了一個(gè)能在所有shell腳本中使用的全功能命令行選項(xiàng)和參數(shù)處理工具。
關(guān)于更高級(jí)的 getopts命令的使用方法,南昌網(wǎng)絡(luò)公司-百恒網(wǎng)絡(luò)就為大家介紹到這里了,如果還有哪些不太明白的地方,可倒回去再看一遍,或者來電咨詢我們。此外,如有需要網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)等方面的服務(wù)的朋友,歡迎來電和我們聯(lián)系,百恒將隨時(shí)為您效勞!