欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

shell腳本讀取命令行參數(shù)的實(shí)現(xiàn)

 更新時(shí)間:2021年07月30日 16:53:29   作者:未月廿三  
本文主要介紹了shell腳本讀取命令行參數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前提

在編寫shell程序時(shí)經(jīng)常需要處理命令行參數(shù)

選項(xiàng)與參數(shù):

如下命令行:

./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ì)講到。

一.手工處理方式(已驗(yàn)證)

在手工處理方式中,首先要知道幾個(gè)變量,還是以上面的命令行為例:

$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ù)組。

例子

#!/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ì)打?。?/p>

# 這是"$*"的輸出
-f config.conf -n 10 

#以下為$@的輸出
-f  
config.conf
-n
10

所以,手工處理的方式即對(duì)這些變量的處理。因?yàn)槭止ぬ幚砀叨纫蕾囉谀阍诿钚猩纤鶄鲄?shù)的位置,所以一般都只用來(lái)處理較簡(jiǎn)單的參數(shù)。

例如:

./test.sh 10

而很少使用./test -n 10這種帶選項(xiàng)的方式。

典型用法為:

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

為什么要使用 x$1 != x 這種方式來(lái)比較呢?(x就是任意的一個(gè)字符,也可以是別的)
想像一下這種方式比較:

if [ -n $1 ]  #$1不為空

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

二.getopts/getopt

處理命令行參數(shù)是一個(gè)相似而又復(fù)雜的事情,為此,c提供了getopt/getopt_long等函數(shù),

c++的boost提供了options庫(kù),在shell中,處理此事的是getopts和getopt.

getopts和getopt功能相似但又不完全相同,其中g(shù)etopt是獨(dú)立的可執(zhí)行文件,而getopts是由bash內(nèi)置的。

  •  ./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)

先來(lái)看getopts,它不支持長(zhǎng)選項(xiàng)。

使用getopts非常簡(jiǎn)單:

#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

來(lái)加載了。

應(yīng)該說(shuō)絕大多數(shù)腳本使用該函數(shù)就可以了,如果需要支持長(zhǎng)選項(xiàng)以及可選參數(shù),那么就需要使用getopt.
getopt自帶的一個(gè)例子:

#!/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)
#"$@"在上面解釋過(guò)
# -n:出錯(cuò)時(shí)的信息
# -- :舉一個(gè)例子比較好理解:
#我們要?jiǎng)?chuàng)建一個(gè)名字為 "-f"的目錄你會(huì)怎么辦?
# mkdir -f #不成功,因?yàn)?f會(huì)被mkdir當(dāng)作選項(xiàng)來(lái)解析,這時(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中重新排列過(guò)了
eval set -- "$temp"
#經(jīng)過(guò)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)過(guò)getopt和set之后,命令行會(huì)變?yōu)椋?/p>

-a -b arg -c -- arg1

1指向−a,2指向-b,3指向arg,4指向-c,$5指向--,而多出的arg1則被放到了最后。

三.總結(jié)

一般小腳本手工處理也就夠了,getopts能處理絕大多數(shù)的情況,getopt較復(fù)雜,功能也更強(qiáng)大。

到此這篇關(guān)于shell腳本讀取命令行參數(shù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)shell 讀取命令行參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論