溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何理解bash shell命令行選項與修傳入?yún)?shù)處理

發(fā)布時間:2021-10-08 10:56:56 來源:億速云 閱讀:172 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“如何理解bash shell命令行選項與修傳入?yún)?shù)處理”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何理解bash shell命令行選項與修傳入?yún)?shù)處理”吧!

在編寫shell程序時經(jīng)常需要處理命令行參數(shù),本文描述在bash下的命令行處理方式。
選項與參數(shù):
如下命令行:
 

代碼如下:

./test.sh -f config.conf -v --prefix=/home


-f為選項,它需要一個參數(shù),即config.conf, -v 也是一個選項,但它不需要參數(shù)。
--prefix我們稱之為一個長選項,即選項本身多于一個字符,它也需要一個參數(shù),用等號連接,當(dāng)然等號不是必須的,/home可以直接寫在--prefix后面,即--prefix/home,更多的限制后面具體會講到。
在bash中,可以用以下三種方式來處理命令行參數(shù),每種方式都有自己的應(yīng)用場景。
* 手工處理方式
* getopts
* getopt
依次討論這三種處理方式。
1,手工處理方式
在手工處理方式中,首先要知道幾個變量,還是以上面的命令行為例:

代碼如下:


*    $0 : ./test.sh,即命令本身,相當(dāng)于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 會打?。?br/>-f config.conf -n 10    #這是"$*"的輸出
-f   #以下為$@的輸出
config.conf
-n
10
所以,手工處理的方式即對這些變量的處理。因為手工處理高度依賴于你在命令行上所傳參數(shù)的位置,所以一般都只用來處理較簡單的參數(shù)。
(腳本學(xué)堂 www.kemok4.com 編輯整理)
例如:
./test.sh 10
而很少使用./test -n 10這種帶選項的方式。 典型用法為:

代碼如下:

#!/bin/bash
if [ x$1 != x ]
then
    #...有參數(shù)
else
then
    #...沒有參數(shù)
fi


為什么要使用 x$1 != x 這種方式來比較呢?想像一下這種方式比較:
if [ -n $1 ]  #$1不為空
但如果用戶不傳參數(shù)的時候,$1為空,這時 就會變成 [ -n ] ,所以需要加一個輔助字符串來進(jìn)行比較。
手工處理方式能滿足大多數(shù)的簡單需求,配合shift使用也能構(gòu)造出強(qiáng)大的功能,但在要處理復(fù)雜選項的時候建議用下面的兩種方法。

2. getopts/getopt
處理命令行參數(shù)是一個相似而又復(fù)雜的事情,為此,c提供了getopt/getopt_long等函數(shù),
c++的boost提供了options庫,在shell中,處理此事的是getopts和getopt.
getopts和getopt功能相似但又不完全相同,其中g(shù)etopt是獨立的可執(zhí)行文件,而getopts是由bash內(nèi)置的。
先來看看參數(shù)傳遞的典型用法:

代碼如下:

    * ./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"

             ?)  #當(dāng)有不認(rèn)識的選項的時候arg為?
            echo "unkonw argument"
        exit 1

        esac
done


現(xiàn)在就可以使用:
./test.sh -a arg -b -c

./test.sh -a arg -bc
來加載了。
應(yīng)該說絕大多數(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當(dāng)作選項來解析,這時就可以使用
# 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

比如使用
./test -a  -b arg arg1 -c
你可以看到,命令行中多了個arg1參數(shù),在經(jīng)過getopt和set之后,命令行會變?yōu)椋?br/>-a -b arg -c -- arg1
$1指向-a,$2指向-b,$3指向arg,$4指向-c,$5指向--,而多出的arg1則被放到了最后。

到此,相信大家對“如何理解bash shell命令行選項與修傳入?yún)?shù)處理”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI