linux學習之shell指令碼 ------- 指令碼參數傳遞

來源:互聯網
上載者:User

標籤:shell指令碼參數傳遞   shift命令   getopts命令   

[本文是自己學習所做筆記,歡迎轉載,但請註明出處:http://blog.csdn.net/jesson20121020]

今天再來看一下如何向shell指令碼傳遞參數,需要掌握兩個命令,一個是 shift命令,另一個是getopts。
指令碼參數傳遞  shift命令

   用法:

    shift n 每次將參數位置向左位移n位

   假如我們要實現統計多個檔案的總行數,就可以用到這個shift命令了,如下:

opt2.sh

#!/bin/bash#op2  static files total lines;staticlines(){   echo "static:`basename $0` filenames"   exit}totalline=0if [ $# -lt 2 ]then    staticlinesfiwhile [ $# -ne 0 ]do    line=`cat $1 | wc -l`    echo "$1:${line}"    totalline=$[ $totalline+$line ]    shiftdoneecho "-------------------------------------"echo "totalline:${totalline}"
   給予可執行許可權,執行:

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx opt2.sh [email protected]:~/develop/worksapce/shell_workspace$ ./opt2.sh static:opt2.sh filenames[email protected]:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txtstatic:opt2.sh filenames[email protected]:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txt name.txt lsout.txt:18name.txt:4-------------------------------------totalline:22[email protected]:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txt name.txt while_test1.sh lsout.txt:18name.txt:4while_test1.sh:6-------------------------------------totalline:28
   通過shift 命令,我們可以很容易地實現統計log等資訊。

  getopts命令

   該命令可以獲得多個命令列參數。

   還是一個指令碼來分析getopts的用法

optsget.sh

#!/bin/bash#optsgetALL=falseHELP=falseFILE=falseVERBOSE=falsewhile getopts ahfvc: OPTIONdo     case $OPTION in     a)         ALL=true         echo "ALL is $ALL"         ;;     h)         HELP=true         echo "HELP is $HELP"         ;;     f)         FILE=true         echo "FILE is $FILE"         ;;     v)          VERBOSE=true         echo "VERBOSE is $VERBOSE"         ;;     c)         c=$OPTARG         echo "c valuse is $c"         ;;     \?)         echo "`basename $0` -[a h f v] -[c value]"         ;;      esacdone
   給予可執行許可權,執行結果如下:

[email protected]:~/develop/worksapce/shell_workspace$ ./optsget.sh -aALL is true[email protected]:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -fALL is trueFILE is true[email protected]:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f -hALL is trueFILE is trueHELP is true[email protected]:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f -h -vALL is trueFILE is trueHELP is trueVERBOSE is true[email protected]:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f -cALL is trueFILE is true./optsget.sh: 選項需要一個參數 -- coptsget.sh -[a h f v] -[c value][email protected]:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f -c jessonALL is trueFILE is truec valuse is jesson
  不難看出,可以通過getopts命令取得多個參數,而且還可以為每個參數指定值,在需要指定值的參數後加:即可。


linux學習之shell指令碼 ------- 指令碼參數傳遞

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.