亚洲欧美精品沙发,日韩在线精品视频,亚洲Av每日更新在线观看,亚洲国产另类一区在线5

<pre id="hdphd"></pre>

  • <div id="hdphd"><small id="hdphd"></small></div>
      學習啦 > 學習電腦 > 操作系統(tǒng) > Linux教程 >

      linux shell命令行選項與參數(shù)有哪些怎么用

      時間: 加城1195 分享

        Linux繼承了Unix以網(wǎng)絡為核心的設計思想,是一個性能穩(wěn)定的多用戶網(wǎng)絡操作系統(tǒng)。在bash中,可以用以下三種方式來處理命令行參數(shù),每種方式都有自己的應用場景本文介紹了linux shell中使用命令行選項與命令行參數(shù)的方法

        方法步驟

        1,直接手工處理位置參數(shù)

        必須要要知道幾個變量,

        復制代碼 代碼如下:

        * $0 :即命令本身,相當于c/c++中的argv[0]

        * $1 :第一個參數(shù).

        * $2, $3, $4 ... :第2、3、4個參數(shù),依次類推。

        * $# 參數(shù)的個數(shù),不包括命令本身

        * $@ :參數(shù)本身的列表,也不包括命令本身

        * $* :和$@相同,但"$*" 和 "$@"(加引號)并不同,"$*"將所有的參數(shù)解釋成一個字符串,而"$@"是一個參數(shù)數(shù)組。

        手工處理方式能滿足多數(shù)的簡單需求,配合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內(nèi)置命令)

        處理命令行參數(shù)是一個相似而又復雜的事情,為此,c提供了getopt/getopt_long等函數(shù),c++的boost提供了options庫,在shell中,處理此事的是getopts和getopt。

        getopts/getopt的區(qū)別,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有兩個參數(shù),第一個參數(shù)是一個字符串,包括字符和“:”,每一個字符都是一個有效的選項,如果字符后面帶有“:”,表示這個字符有自己的參數(shù)。getopts從命令中獲取這些參數(shù),并且刪去了“-”,并將其賦值在第二個參數(shù)中,如果帶有自己參數(shù),這個參數(shù)賦值在“optarg”中。提供getopts的shell內(nèi)置了optarg這個變變,getopts修改了這個變量。

        這里變量$optarg存儲相應選項的參數(shù),而$optind總是存儲原始$*中下一個要處理的元素位置。

        while getopts ":a:bc" opt #第一個冒號表示忽略錯誤;字符后面的冒號表示該選項必須有自己的參數(shù)

        例子,(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))的處理,$*中就只保留了除去選項內(nèi)容的參數(shù),可以在其后進行正常的shell編程處理了。

        echo $0

        echo $*

        執(zhí)行命令:

        復制代碼 代碼如下:

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

        復制代碼 代碼如下:

        #!/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

        運行命令:

        復制代碼 代碼如下:

        ./getopt.sh --b-long abc -a -c33 remain

        option b, argument `abc'

        option a

        option c, argument `33'

        remaining arguments:

        --> `remain'

        補充:Linux基本命令

        1.ls命令:

        格式::ls [選項] [目錄或文件]

        功能:對于目錄,列出該目錄下的所有子目錄與文件;對于文件,列出文件名以及其他信息。

        常用選項:

        -a :列出目錄下的所有文件,包括以 . 開頭的隱含文件。

        -d :將目錄像文件一樣顯示,而不是顯示其他文件。

        -i :輸出文件的i節(jié)點的索引信息。

        -k :以k字節(jié)的形式表示文件的大小。

        -l :列出文件的詳細信息。

        -n :用數(shù)字的UID,GID代替名稱。

        -F : 在每個文件名后面附上一個字符以說明該文件的類型,“*”表示可執(zhí)行的普通文 件;“/”表示目錄;“@”表示符號鏈接;“l”表示FIFOS;“=”表示套接字。

        2.cd命令

        格式:cd [目錄名稱]

        常用選項:

        cd .. 返回上一級目錄。

        cd ../.. 將當前目錄向上移動兩級。

        cd - 返回最近訪問目錄。

        3.pwd命令

        格式: pwd

        功能:顯示出當前工作目錄的絕對路徑。

        相關閱讀:Linux主要特性

        完全兼容POSIX1.0標準

        這使得可以在Linux下通過相應的模擬器運行常見的DOS、Windows的程序。這為用戶從Windows轉到Linux奠定了基礎。許多用戶在考慮使用Linux時,就想到以前在Windows下常見的程序是否能正常運行,這一點就消除了他們的疑慮。

        多用戶、多任務

        Linux支持多用戶,各個用戶對于自己的文件設備有自己特殊的權利,保證了各用戶之間互不影響。多任務則是現(xiàn)在電腦最主要的一個特點,Linux可以使多個程序同時并獨立地運行。

        良好的界面

        Linux同時具有字符界面和圖形界面。在字符界面用戶可以通過鍵盤輸入相應的指令來進行操作。它同時也提供了類似Windows圖形界面的X-Window系統(tǒng),用戶可以使用鼠標對其進行操作。在X-Window環(huán)境中就和在Windows中相似,可以說是一個Linux版的Windows。

        支持多種平臺

        Linux可以運行在多種硬件平臺上,如具有x86、680x0、SPARC、Alpha等處理器的平臺。此外Linux還是一種嵌入式操作系統(tǒng),可以運行在掌上電腦、機頂盒或游戲機上。2001年1月份發(fā)布的Linux 2.4版內(nèi)核已經(jīng)能夠完全支持Intel 64位芯片架構。同時Linux也支持多處理器技術。多個處理器同時工作,使系統(tǒng)性能大大提高。


      linux shell命令行選項與參數(shù)相關文章:

      1.linux chsh命令參數(shù)及用法詳解

      2.linux shell cd命令

      3.linux shell腳本執(zhí)行命令

      4.關于Linux中10個你不知道的命令補齊技巧有哪些

      5.xshell操作linux系統(tǒng)的常用命令

      4019638