濮阳杆衣贸易有限公司

主頁 > 知識庫 > bash shell命令行選項與修傳入?yún)?shù)處理

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

熱門標簽:地圖標注員都是年輕人 打電話機器人接我是他的秘書 江蘇云電銷機器人公司 客服外呼系統(tǒng)怎么樣 華鋒e路航港口地圖標注 揭陽智能電話機器人推薦 河南信譽好的不封卡電話外呼系統(tǒng) 如果做線上地圖標注 百度地圖標注錯了有責任嗎


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

復制代碼 代碼如下:

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

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

*    $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 會打?。?BR>-f config.conf -n 10    #這是"$*"的輸出
-f   #以下為$@的輸出
config.conf
-n
10
所以,手工處理的方式即對這些變量的處理。因為手工處理高度依賴于你在命令行上所傳參數(shù)的位置,所以一般都只用來處理較簡單的參數(shù)。
(腳本學堂 www.jb51.net 編輯整理)
例如:
./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 ] ,所以需要加一個輔助字符串來進行比較。
手工處理方式能滿足大多數(shù)的簡單需求,配合shift使用也能構造出強大的功能,但在要處理復雜選項的時候建議用下面的兩種方法。

2. getopts/getopt
處理命令行參數(shù)是一個相似而又復雜的事情,為此,c提供了getopt/getopt_long等函數(shù),
c++的boost提供了options庫,在shell中,處理此事的是getopts和getopt.
getopts和getopt功能相似但又不完全相同,其中getopt是獨立的可執(zhí)行文件,而getopts是由bash內置的。
先來看看參數(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"

             ?)  #當有不認識的選項的時候arg為?
            echo "unkonw argument"
        exit 1

        esac
done

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

./test.sh -a arg -bc
來加載了。
應該說絕大多數(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

比如使用
./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則被放到了最后。
3,總結
一般小腳本手工處理也就夠了,getopts能處理絕大多數(shù)的情況,getopt較復雜,功能也更強大。

您可能感興趣的文章:
  • bash批量修改文件名稱的方法小結(增加,去除,修改后綴)
  • bash 編程中循環(huán)語句用法
  • BASH 學習筆記小結
  • Bash Shell字符串操作小結
  • Shell 編程:Bash空格的那點事
  • linux bash字符串處理大全
  • Bash中尖括號的更多使用方法

標簽:許昌 馬鞍山 赤峰 邵陽 金昌 淘寶邀評 婁底 巴彥淖爾

巨人網(wǎng)絡通訊聲明:本文標題《bash shell命令行選項與修傳入?yún)?shù)處理》,本文關鍵詞  bash,shell,命令行,選項,與,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《bash shell命令行選項與修傳入?yún)?shù)處理》相關的同類信息!
  • 本頁收集關于bash shell命令行選項與修傳入?yún)?shù)處理的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    临潭县| 光山县| 通山县| 新昌县| 孟津县| 临颍县| 黄大仙区| 林口县| 江都市| 特克斯县| 新郑市| 葵青区| 松江区| 沅江市| 镇沅| 西和县| 西乌珠穆沁旗| 汉中市| 浏阳市| 卢龙县| 临沭县| 漾濞| 东乌| 米泉市| 元氏县| 九江县| 淮安市| 舞阳县| 大连市| 饶平县| 沐川县| 石台县| 博湖县| 成安县| 蕲春县| 浮山县| 永兴县| 马关县| 嘉荫县| 炎陵县| 精河县|