麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > Linux Shell > 正文

bash shell命令行選項(xiàng)與修傳入?yún)?shù)處理

2020-07-27 19:17:39
字體:
供稿:網(wǎng)友


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

復(fù)制代碼 代碼如下:

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

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

*    $0 : ./test.sh,即命令本身,相當(dāng)于c/c++中的argv[0]
*    $1 : -f,第一個(gè)參數(shù).
*    $2 : config.conf
*    $3, $4 ... :類推。
*    $#  參數(shù)的個(gè)數(shù),不包括命令本身,上例中$#為4.
*    $@ :參數(shù)本身的列表,也不包括命令本身,如上例為 -f config.conf -v --prefix=/home
*    $* :和$@相同,但"$*" 和 "$@"(加引號(hào))并不同,"$*"將所有的參數(shù)解釋成一個(gè)字符串,而"$@"是一個(gè)參數(shù)數(shù)組。

例子:

復(fù)制代碼 代碼如下:

#!/bin/bash
for arg in "$*"
do
   echo $arg
done
for arg in "$@"
do
 echo $arg
done

執(zhí)行./test.sh -f config.conf -n 10 會(huì)打印:
-f config.conf -n 10    #這是"$*"的輸出
-f   #以下為$@的輸出
config.conf
-n
10
所以,手工處理的方式即對(duì)這些變量的處理。因?yàn)槭止ぬ幚砀叨纫蕾囉谀阍诿钚猩纤鶄鲄?shù)的位置,所以一般都只用來處理較簡(jiǎn)單的參數(shù)。
(腳本學(xué)堂 www.companysz.com 編輯整理)
例如:
./test.sh 10
而很少使用./test -n 10這種帶選項(xiàng)的方式。 典型用法為:

復(fù)制代碼 代碼如下:

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

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

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

    * ./test.sh -a -b -c  : 短選項(xiàng),各選項(xiàng)不需參數(shù)
    * ./test.sh -abc   : 短選項(xiàng),和上一種方法的效果一樣,只是將所有的選項(xiàng)寫在一起。
    * ./test.sh -a args -b -c :短選項(xiàng),其中-a需要參數(shù),而-b -c不需參數(shù)。
    * ./test.sh --a-long=args --b-long :長(zhǎng)選項(xiàng)

先來看getopts,它不支持長(zhǎng)選項(xiàng)。
使用getopts非常簡(jiǎn)單:

復(fù)制代碼 代碼如下:

#test.sh
#!/bin/bash
while getopts "a:bc" arg #選項(xiàng)后面的冒號(hào)表示該選項(xiàng)需要參數(shù)
do
        case $arg in
             a)
                echo "a's arg:$optarg" #參數(shù)存在$optarg中

             b)
                echo "b"

             c)
                echo "c"

             ?)  #當(dāng)有不認(rèn)識(shí)的選項(xiàng)的時(shí)候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ù)就可以了,如果需要支持長(zhǎng)選項(xiàng)以及可選參數(shù),那么就需要使用getopt.
getopt自帶的一個(gè)例子:
復(fù)制代碼 代碼如下:

#!/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表示短選項(xiàng),兩個(gè)冒號(hào)表示該選項(xiàng)有一個(gè)可選參數(shù),可選參數(shù)必須緊貼選項(xiàng)
#如-carg 而不能是-c arg
#--long表示長(zhǎng)選項(xiàng)
#"$@"在上面解釋過
# -n:出錯(cuò)時(shí)的信息
# -- :舉一個(gè)例子比較好理解:
#我們要?jiǎng)?chuàng)建一個(gè)名字為 "-f"的目錄你會(huì)怎么辦?
# mkdir -f #不成功,因?yàn)?f會(huì)被mkdir當(dāng)作選項(xiàng)來解析,這時(shí)就可以使用
# mkdir -- -f 這樣-f就不會(huì)被作為選項(xiàng)。
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 會(huì)重新排列參數(shù)的順序,也就是改變$1,$2...$n的值,這些值在getopt中重新排列過了
eval set -- "$temp"
#經(jīng)過getopt的處理,下面處理具體選項(xiàng)。
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
你可以看到,命令行中多了個(gè)arg1參數(shù),在經(jīng)過getopt和set之后,命令行會(huì)變?yōu)椋?BR>-a -b arg -c -- arg1
$1指向-a,$2指向-b,$3指向arg,$4指向-c,$5指向--,而多出的arg1則被放到了最后。
3,總結(jié)
一般小腳本手工處理也就夠了,getopts能處理絕大多數(shù)的情況,getopt較復(fù)雜,功能也更強(qiáng)大。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久免费视频8 | 最新影院 | 91成人一区 | 日韩一级网站 | 免费a观看 | 久久久鲁| 久久一本日日摸夜夜添 | 模特三级在线观看 | 7777奇米成人四色影视 | 红杏网站永久免费视频入口 | 羞羞视频免费观看网站 | 爽毛片| 91九色视频在线播放 | www久久艹 | 黄网站进入 | 欧美成人精品一级 | 日本免费一区二区三区四区 | 亚洲精品午夜电影 | 亚洲成人激情av | 亚洲国产美女视频 | 综合国产一区 | www.理论片| 国产91丝袜在线播放0 | 黄色一级片在线免费观看 | 国产在线看一区 | 精国品产一区二区三区有限公司 | 免费亚洲视频在线观看 | 久久久久久久亚洲精品 | 成人毛片免费在线 | 国产精品视频一区二区三区综合 | 亚洲免费片| 美女毛片在线观看 | 成人污在线 | 羞羞视频免费网站日本动漫 | 免费毛片视频播放 | 久久精品99国产国产精 | 一级免费视频 | 久久线视频 | 91成人免费视频 | 国产精品一品二区三区四区18 | 性盈盈盈影院 |