Linux系列-shell學習筆記(續一) 處理使用者輸入

來源:互聯網
上載者:User

標籤:linux   shell   指令碼   使用者輸入   

1.運行帶參數的程式

$0表示程式名,$1表示第一個參數,$2表示第二個參數,一次類推,直到第九個參數$9

# vi factorial

#!/bin/shf=1for((i=1;i<=$1;i++))do        f=$[ $f * $i]doneecho $f
測試:

[[email protected] test]# ./factorial 5120

注意:如果有多個參數,每個參數必須有個空格,如果一個參數裡面帶空格,必須用單引號或雙引號括起來。

2.讀取程式名

編寫基於所用的指令碼名而執行不同功能的指令碼

# vi addem

#!/bin/shname=`basename $0`echo $nameif [ $name = "addem" ]then        echo $[$1+$2]elif [ $name = "mulem" ]then        echo $[ $1 * $2]fi

# cp addem mulem

測試:

[[email protected] test]# sh addem 3 47[[email protected] test]# sh mulem 33 399

 3.參數計數

可以通過 $# 來統計參數的個數。

可以通過 ${!#} 來獲得最後一個命令列參數變數,或者是params=$#,然後用$params 獲得也可以。

 4.獲得所有資料

$*和[email protected]變數提供了對所有參數的快速存取。

[[email protected] test]# vi test11#!/bin/sh# testing $* and [email protected]echo "Using the \$* method:$*"echo "Using the \[email protected] method:[email protected]"測試:[[email protected] test]# ./test11 rich jjds fds qaa dsddUsing the $* method:rich jjds fds qaa dsddUsing the [email protected] method:rich jjds fds qaa dsdd

$*:儲存的資料相當於單個單詞;

[email protected]:儲存的資料相當於多個獨立的單詞。

 5.shift移動變數

使用shift可以把參數都向前移動一位,$1會被移調,最後$#的大小會減掉1

使用shift 2 表示一次移動兩位

判斷帶入的參數是否不為空白:if [ -n $1 ] 表示判斷$1是否為空白

 6.尋找選項

# vi test12

#!/bin/sh# extracting command line options as parameterswhile [ -n "$1" ]do        case "$1" in        -a) echo "-a option" ;;        -b) echo "-b option" ;;        -c) echo "-c option" ;;        *) echo "$1 is not an option" ;;        esac        shiftdone

測試:

[[email protected] test]# ./test12 -a-a option[[email protected] test]# ./test12 cc is not an option[[email protected] test]#

7.getopt命令和getopts命令

 

8.獲得使用者輸入           

# vi test13

#!/bin/shecho "please enter you name:"read nameecho "hello $name ,where come to here"測試:[[email protected] test]# ./test13please enter you name:hahahello haha ,where come to here

可以在read後面直接跟指定提示符:

read –p "please entry you age:" age

還可以使用-t設定計時器

read –t 5 –p "please entry you age:" age

其它參數:

-s隱式方式讀取,資料會被顯示,只是read命令會將文本顏色設定成跟背景色一樣;

9.從檔案中讀取資料

# vi test15

#!/bin/shcount=1cat test | while read linedo        echo "Line $count: $line"        count=$[$count + 1]doneecho "finish file!"
測試
[[email protected] test]# ./test15Line 1: #!/bin/shLine 2: if dateLine 3: thenLine 4: echo "it worked!"Line 5: fiLine 6: echo "this is a test file"Line 7: echo "this is a test file"finish file!

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.