佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > linux shell命令行選項與參數(shù)用法詳解

linux shell命令行選項與參數(shù)用法詳解

熱門標(biāo)簽:網(wǎng)站排名優(yōu)化 百度競價排名 鐵路電話系統(tǒng) 地方門戶網(wǎng)站 呼叫中心市場需求 AI電銷 Linux服務(wù)器 服務(wù)外包
問題描述:在linux shell中如何處理tail -n 10 access.log這樣的命令行選項?
在bash中,可以用以下三種方式來處理命令行參數(shù),每種方式都有自己的應(yīng)用場景。
1,直接處理,依次對$1,$2,...,$n進行解析,分別手工處理;
2,getopts來處理,單個字符選項的情況(如:-n 10 -f file.txt等選項);
3,getopt,可以處理單個字符選項,也可以處理長選項long-option(如:--prefix=/home等)。
總結(jié):小腳本手工處理即可,getopts能處理絕大多數(shù)的情況,getopt較復(fù)雜、功能也更強大。
1,直接手工處理位置參數(shù)
必須要要知道幾個變量,
復(fù)制代碼 代碼如下:

   *    $0 :即命令本身,相當(dāng)于c/c++中的argv[0]
    *    $1 :第一個參數(shù).
    *    $2, $3, $4 ... :第2、3、4個參數(shù),依次類推。
    *    $#  參數(shù)的個數(shù),不包括命令本身
    *    $@ :參數(shù)本身的列表,也不包括命令本身
    *    $* :和$@相同,但"$*" 和 "$@"(加引號)并不同,"$*"將所有的參數(shù)解釋成一個字符串,而"$@"是一個參數(shù)數(shù)組。

手工處理方式能滿足多數(shù)的簡單需求,配合shift使用也能構(gòu)造出強大的功能,但處理復(fù)雜選項時建議用下面的兩種方法。
例子,(getargs.sh):

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

#!/bin/bash
if [ $# -lt 1 ]; then
    echo "error.. need args"
    exit 1
fi
echo "commond is $0"
echo "args are:"
for arg in "$@"
do
    echo $arg
done


運行命令:

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

./getargs.sh 11 22 cc
commond is ./getargs.sh
args are:
11
22
cc

2,getopts (shell內(nèi)置命令)
處理命令行參數(shù)是一個相似而又復(fù)雜的事情,為此,c提供了getopt/getopt_long等函數(shù),c++的boost提供了options庫,在shell中,處理此事的是getopts和getopt。
getopts/getopt的區(qū)別,getopt是個外部binary文件,而getopts是shell builtin。

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

[root@jbxue ~]$ type getopt
getopt is /usr/bin/getopt
[root@jbxue ~]$ type getopts
getopts is a shell builtin

getopts不能直接處理長的選項(如:--prefix=/home等)
關(guān)于getopts的使用方法,可以man bash  搜索getopts
getopts有兩個參數(shù),第一個參數(shù)是一個字符串,包括字符和“:”,每一個字符都是一個有效的選項,如果字符后面帶有“:”,表示這個字符有自己的參數(shù)。getopts從命令中獲取這些參數(shù),并且刪去了“-”,并將其賦值在第二個參數(shù)中,如果帶有自己參數(shù),這個參數(shù)賦值在“optarg”中。提供getopts的shell內(nèi)置了optarg這個變變,getopts修改了這個變量。
這里變量$optarg存儲相應(yīng)選項的參數(shù),而$optind總是存儲原始$*中下一個要處理的元素位置。
while getopts ":a:bc" opt  #第一個冒號表示忽略錯誤;字符后面的冒號表示該選項必須有自己的參數(shù)
例子,(getopts.sh):

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

echo $*
while getopts ":a:bc" opt
do
        case $opt in
                a ) echo $optarg
                    echo $optind;;
                b ) echo "b $optind";;
                c ) echo "c $optind";;
                ? ) echo "error"
                    exit 1;;
        esac
done
echo $optind
shift $(($optind - 1))
#通過shift $(($optind - 1))的處理,$*中就只保留了除去選項內(nèi)容的參數(shù),可以在其后進行正常的shell編程處理了。
echo $0
echo $*


執(zhí)行命令:

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

./getopts.sh -a 11 -b -c
-a 11 -b -c
11
3
b 4
c 5
5
./getopts.sh

3,getopt(一個外部工具)
具體用用法可以 man getopt
#-o表示短選項,兩個冒號表示該選項有一個可選參數(shù),可選參數(shù)必須緊貼選項,如-carg 而不能是-c arg
#--long表示長選項
例子,(getopt.sh):

復(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表示短選項,兩個冒號表示該選項有一個可選參數(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

運行命令:
復(fù)制代碼 代碼如下:

./getopt.sh --b-long abc -a -c33 remain
option b, argument `abc'
option a
option c, argument `33'
remaining arguments:
--> `remain'

以上就是有關(guān)linux shell命令行選項與參數(shù)用法的詳細內(nèi)容,希望對大家有所幫助。
您可能感興趣的文章:
  • linux命令詳解之useradd命令使用方法
  • Linux top命令的用法詳細詳解
  • linux之cut命令的用法
  • linux系統(tǒng)sudo命令詳解
  • Linux 入門常用命令 password — 修改密碼,改變用戶
  • Python執(zhí)行Linux系統(tǒng)命令的4種方法
  • linux shell腳本學(xué)習(xí)xargs命令使用詳解
  • Linux中rz命令和sz命令使用詳解大全
  • Linux ls命令參數(shù)詳解
  • Linux du命令查看文件夾大小并按降序排列
  • Linux常見基本命令與用法大全

標(biāo)簽:蘭州 湘潭 湖南 衡水 仙桃 黃山 銅川 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux shell命令行選項與參數(shù)用法詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    西藏| 藁城市| 云阳县| 延吉市| 苍山县| 屯昌县| 英吉沙县| 澎湖县| 开江县| 普宁市| 大丰市| 新疆| 当阳市| 封丘县| 兰州市| 通许县| 邹城市| 正宁县| 扶沟县| 旺苍县| 南江县| 柯坪县| 寿阳县| 土默特右旗| 洛宁县| 农安县| 乡宁县| 巍山| 泸定县| 颍上县| 星子县| 安国市| 晴隆县| 衡阳县| 和田市| 荆州市| 临西县| 乐陵市| 南雄市| 温泉县| 图片|