目錄
- 前提
- 一.手工處理方式(已驗證)
- 二.getopts/getopt
- 三.總結
前提
在編寫shell程序時經(jīng)常需要處理命令行參數(shù)
選項與參數(shù):
如下命令行:
./test.sh -f config.conf -v --prefix=/home
-f為選項,它需要一個參數(shù),即config.conf,
-v 也是一個選項,但它不需要參數(shù)。
--prefix我們稱之為一個長選項,即選項本身多于一個字符,它也需要一個參數(shù),用等號連接,當然等號不是必須的,
/home可以直接寫在--prefix后面,即--prefix/home,
更多的限制后面具體會講到。
一.手工處理方式(已驗證)
在手工處理方式中,首先要知道幾個變量,還是以上面的命令行為例:
$0: ./test.sh,即命令本身,相當于c/c++中的argv[0]
$1: -f,第一個參數(shù).
$2: config.conf
$3, $4 ... :類推。
$#: 參數(shù)的個數(shù),不包括命令本身,上例中$#為4.
$@:參數(shù)本身的列表,也不包括命令本身,如上例為 -f config.conf -v --prefix=/home
$*:和$@相同,但"$*" 和 "$@"(加引號)并不同,"$*"將所有的參數(shù)解釋成一個字符串,而"$@"是一個參數(shù)數(shù)組。
例子
#!/bin/bash
for arg in "$*"
do
echo $arg
done
for arg in "$@"
do
echo $arg
done
執(zhí)行./test.sh -f config.conf -n 10 會打?。?/p>
# 這是"$*"的輸出
-f config.conf -n 10
#以下為$@的輸出
-f
config.conf
-n
10
所以,手工處理的方式即對這些變量的處理。因為手工處理高度依賴于你在命令行上所傳參數(shù)的位置,所以一般都只用來處理較簡單的參數(shù)。
例如:
而很少使用./test -n 10這種帶選項的方式。
典型用法為:
#!/bin/bash
if [ x$1 != x ]
then
#...有參數(shù)邏輯
else
then
#...沒有參數(shù)邏輯
fi
為什么要使用 x$1 != x 這種方式來比較呢?(x就是任意的一個字符,也可以是別的)
想像一下這種方式比較:
但如果用戶不傳參數(shù)的時候,$1為空,這時就會變成 [ -n ] ,所以需要加一個輔助字符串來進行比較。
手工處理方式能滿足大多數(shù)的簡單需求,配合shift使用也能構造出強大的功能,但在要處理復雜選項的時候建議用下面的兩種方法。
二.getopts/getopt
處理命令行參數(shù)是一個相似而又復雜的事情,為此,c提供了getopt/getopt_long等函數(shù),
c++的boost提供了options庫,在shell中,處理此事的是getopts和getopt.
getopts和getopt功能相似但又不完全相同,其中getopt是獨立的可執(zhí)行文件,而getopts是由bash內(nèi)置的。
- ./test.sh -a -b -c : 短選項,各選項不需參數(shù)
- ./test.sh -abc : 短選項,和上一種方法的效果一樣,只是將所有的選項寫在一起。
- ./test.sh -a args -b -c :短選項,其中-a需要參數(shù),而-b -c不需參數(shù)。
- ./test.sh --a-long=args --b-long :長選項
先來看getopts,它不支持長選項。
使用getopts非常簡單:
#test.sh
#!/bin/bash
while getopts "a:bc" arg #選項后面的冒號表示該選項需要參數(shù)
do
case $arg in
a)
echo "a's arg:$optarg" #參數(shù)存在$optarg中
b)
echo "b"
c)
echo "c"
?) #當有不認識的選項的時候arg為?
echo "unkonw argument"
exit 1
esac
done
現(xiàn)在就可以使用:
或
來加載了。
應該說絕大多數(shù)腳本使用該函數(shù)就可以了,如果需要支持長選項以及可選參數(shù),那么就需要使用getopt.
getopt自帶的一個例子:
#!/bin/bash
# a small example program for using the new getopt(1) program.
# this program will only work with bash(1)
# an similar program using the tcsh(1) script language can be found
# as parse.tcsh
# example input and output (from the bash prompt):
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\&;' -cmore -b " very long "
# option a
# option c, no argument
# option c, argument `more'
# option b, argument ` very long '
# remaining arguments:
# --> `par1'
# --> `another arg'
# --> `wow!*\&;'
# note that we use `"$@"' to let each command-line parameter expand to a
# separate word. the quotes around `$@' are essential!
# we need temp as the `eval set --' would nuke the return value of getopt.
#-o表示短選項,兩個冒號表示該選項有一個可選參數(shù),可選參數(shù)必須緊貼選項
#如-carg 而不能是-c arg
#--long表示長選項
#"$@"在上面解釋過
# -n:出錯時的信息
# -- :舉一個例子比較好理解:
#我們要創(chuàng)建一個名字為 "-f"的目錄你會怎么辦?
# mkdir -f #不成功,因為-f會被mkdir當作選項來解析,這時就可以使用
# mkdir -- -f 這樣-f就不會被作為選項。
temp=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "terminating..." >2 ; exit 1 ; fi
# note the quotes around `$temp': they are essential!
#set 會重新排列參數(shù)的順序,也就是改變$1,$2...$n的值,這些值在getopt中重新排列過了
eval set -- "$temp"
#經(jīng)過getopt的處理,下面處理具體選項。
while true ; do
case "$1" in
-a|--a-long) echo "option a" ; shift ;;
-b|--b-long) echo "option b, argument \`$2'" ; shift 2 ;;
-c|--c-long)
# c has an optional argument. as we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "option c, no argument"; shift 2 ;;
*) echo "option c, argument \`$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "internal error!" ; exit 1 ;;
esac
done
echo "remaining arguments:"
for arg do
echo '--> '"\`$arg'" ;
done
比如使用
你可以看到,命令行中多了個arg1參數(shù),在經(jīng)過getopt和set之后,命令行會變?yōu)椋?/p>
1指向−a,2指向-b,3指向arg,4指向-c,$5指向--,而多出的arg1則被放到了最后。
三.總結
一般小腳本手工處理也就夠了,getopts能處理絕大多數(shù)的情況,getopt較復雜,功能也更強大。
到此這篇關于shell腳本讀取命令行參數(shù)的實現(xiàn)的文章就介紹到這了,更多相關shell 讀取命令行參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Shell $0, $#, $*, $@, $?, $$和命令行參數(shù)的使用
- linux shell命令行選項與參數(shù)用法詳解
- linux shell命令行參數(shù)用法詳解
- shell腳本命令行參數(shù)簡介
- shell命令行參數(shù)用法簡介
- shell獲取命令行參數(shù)示例分享
- shell學習教程獲取命令行參數(shù)示例