Linux中命令選項及參數簡介

登錄Linux後,我們就可以在#或$符後面去輸入命令,有的時候命令後面還會跟着“選項”(英文options)或“參數”(英文arguments)。即Linux中命令格式爲:

command [options] [arguments] //中括號代表是可選的,即有些命令不需要選項也不需要參數,但有的命令在運行時需要多個選項或參數。

選項options:

選項是調整命令執行行爲的開關,即,選項不同決定了命令的顯示結果不同。

選項分爲長選項和短選項。

短選項:比如-h,-l,-s等。(-   後面接單個字母)

  l短選項都是使用‘-’引導,當有多個短選項時,各選項之間使用空格隔開。

  l有些命令的短選項可以組合,比如-l –h 可以組合爲–lh

  l有些命令的短選項可以不帶-,這通常叫作BSD風格的選項,比如ps aux

  l有些短選項需要帶選項本身的參數,比如-L 512M

長選項:比如--help,--list等。(--  後面接單詞)

  l長選面都是完整的單詞

  l長選項通常不能組合

  l如果需要參數,長選項的參數通常需要‘=’,比如--size=1G

參數arguments:

  參數是指命令的作用對象。

  如ls命令,不加參數的時候顯示是當前目錄,也可以加參數,如ls /dev, 則輸出結果是/dev目錄。

  以上簡要說明了選項及參數的區別,但具體Linux中哪條命令有哪些選項及參數,需要我們靠經驗積累或者查看Linux的幫助了。

 

總結:

  選項是限定結果的顯示結果  

    短選項(-  一個橫槓):只能修飾一個字符的選項,比如: ls -a,當然多個短選項可以合併,比如tar -cvf

    長選項(--  兩個橫槓):可以修飾單個字符,也可以修飾一個單詞,比如:(1) chkconfig --list   (2)chkconfig --add xxx  (3)service --status-all

  參數是傳遞到腳本中的真實的參數

 

 

第二種理解:

本文介紹了linux shell中使用命令行選項與命令行參數的方法,在bash中,可以用以下三種方式來處理命令

行參數,每種方式都有自己的應用場景
問題描述:在linux shell中如何處理tail -n 10 access.log這樣的命令行選項?
在bash中,可以用以下三種方式來處理命令行參數,每種方式都有自己的應用場景。
1,直接處理,依次對$1,$2,...,$n進行解析,分別手工處理;
2,getopts來處理,單個字符選項的情況(如:-n 10 -f file.txt等選項);
3,getopt,可以處理單個字符選項,也可以處理長選項long-option(如:--prefix=/home等)。
總結:小腳本手工處理即可,getopts能處理絕大多數的情況,getopt較複雜、功能也更強大。
1,直接手工處理位置參數
必須要要知道幾個變量,
代碼如下:

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

 

是一個參數數組。


手工處理方式能滿足多數的簡單需求,配合shift使用也能構造出強大的功能,但處理複雜選項時建議用下面

的兩種方法。
例子,(getargs.sh):
代碼如下:

複製代碼
#!/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  
複製代碼

 

 

 



運行命令:
代碼如下:


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

 



2,getopts (shell內置命令)
處理命令行參數是一個相似而又複雜的事情,爲此,c提供了getopt/getopt_long等函數,c++的boost提供了

options庫,在shell中,處理此事的是getopts和getopt。
getopts/getopt的區別,getopt是個外部binary文件,而getopts是shell builtin。

代碼如下:

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

 

 

getopts不能直接處理長的選項(如:--prefix=/home等)
關於getopts的使用方法,可以man bash  搜索getopts
getopts有兩個參數,第一個參數是一個字符串,包括字符和“:”,每一個字符都是一個有效的選項,如果

字符後面帶有“:”,表示這個字符有自己的參數。getopts從命令中獲取這些參數,並且刪去了“-”,並

將其賦值在第二個參數中,如果帶有自己參數,這個參數賦值在“optarg”中。提供getopts的shell內置了

optarg這個變變,getopts修改了這個變量。
這裏變量$optarg存儲相應選項的參數,而$optind總是存儲原始$*中下一個要處理的元素位置。
while getopts ":a:bc" opt  #第一個冒號表示忽略錯誤;字符後面的冒號表示該選項必須有自己的參數
例子,(getopts.sh):

代碼如下:

複製代碼
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))的處理,$*中就只保留了除去選項內容的參數,可以在其後進行正常的shell  
  
編程處理了。  
echo $0  
echo $*  
複製代碼

 


 

 





執行命令:

複製代碼 代碼如下:

 

複製代碼
./getopts.sh -a 11 -b -c  
-a 11 -b -c  
11  
3  
b 4  
c 5  
5  
./getopts.sh  
複製代碼

 

 


 

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

代碼如下:

複製代碼
#!/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表示短選項,兩個冒號表示該選項有一個可選參數,可選參數必須緊貼選項  
#如-carg 而不能是-c arg  
#--long表示長選項  
#"$@"在上面解釋過  
# -n:出錯時的信息  
# -- :舉一個例子比較好理解:  
#我們要創建一個名字爲 "-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 會重新排列參數的順序,也就是改變$1,$2...$n的值,這些值在getopt中重新排列過了  
eval set -- "$temp"  
#經過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  
複製代碼

 

 

 



運行命令:
代碼如下:

1
2
3
4
5
6
./getopt.sh --b-long abc -a -c33 remain 
option b, argument `abc' 
option a 
option c, argument `33' 
remaining arguments: 
--> `remain'

  

 

 

轉載自:https://www.cnblogs.com/qlqwjy/p/7787156.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章