技術(shù)交流QQ群:1027579432歇攻,歡迎你的加入!
歡迎關(guān)注我的微信公眾號(hào):CurryCoder的程序人生
本教程使用Linux發(fā)行版Centos7.0系統(tǒng)瓤狐,請(qǐng)您注意~
1.命令行參數(shù)
- bash shell提供了一些不同的方法來(lái)從用戶處獲得數(shù)據(jù)虽缕,包括命令行參數(shù)(添加在命令后的數(shù)據(jù))熊昌、命令行選項(xiàng)(可修改命令行為的單個(gè)字母)以及直接從鍵盤讀取輸入的能力。
- 向shell腳本傳遞數(shù)據(jù)的最基本方法是使用命令行參數(shù)憨颠,命令行參數(shù)允許你在運(yùn)行腳本時(shí)向命令行添加數(shù)據(jù)胳徽。例如 ./addem 10 30
1.1 讀取參數(shù)
- bash shell會(huì)將一些稱為位置參數(shù)的特殊變量分配給輸入到命令行中的所有參數(shù),這也包括腳本的名稱爽彤。位置參數(shù)變量是標(biāo)準(zhǔn)的數(shù)字:$0是程序名养盗,$1是第一個(gè)參數(shù),$2是第二個(gè)參數(shù)适篙,以此類推往核,$9是第九個(gè)參數(shù)。如下例所示:
#!/bin/bash var=1 for (( n = 1; n <= $1; n++ )) do var=$[ $var * n ] done echo "The var of $1 is $var" # 結(jié)果 [njust@njust tutorials]$ ./bar1.sh 5 # 注意命令行中的參數(shù)5 The var of 5 is 120
- 可以在shell腳本中像使用普通變量一樣使用$1變量嚷节,shell腳本會(huì)自動(dòng)將命令行參數(shù)的值分配給變量聂儒,不需要你作任何處理。如果需要輸入更多的命令行參數(shù)硫痰,則每個(gè)參數(shù)之間都必須用空格分開(kāi)衩婚。如下例所示:
#!/bin/bash res=$[ $1 * $2 ] echo "The first parameter is $1" echo "The second parameter is $2" echo "The final result is $res" # 結(jié)果 [njust@njust tutorials]$ ./bar2.sh 3 5 # 輸入更多命令行參數(shù)時(shí),參數(shù)之間用空格進(jìn)行分割碍论! The first parameter is 3 The second parameter is 5 The final result is 15
- 在上面的例子中谅猾,用到的命令行參數(shù)都是數(shù)值,也可以在命令行上使用文本字符串鳍悠。如下例所示:
#!/bin/bash echo "Hello $1, glad to meet you." # 結(jié)果 [njust@njust tutorials]$ ./bar3.sh Curry Hello Curry, glad to meet you.
-
shell將輸入到命令行的字符串值傳遞給腳本税娜,但碰到含有空格的文本字符串時(shí)就會(huì)出現(xiàn)問(wèn)題,因?yàn)槊總€(gè)命令行參數(shù)都是用空格分隔的藏研,所以shell會(huì)將空格當(dāng)成兩個(gè)值的分隔符敬矩。解決方法:對(duì)含有空格的文本字符串,必須要用引號(hào)蠢挡。如下例所示:
[njust@njust tutorials]$ ./bar3.sh 'James Harden' Hello James Harden, glad to meet you.
-
當(dāng)命令行參數(shù)不只9個(gè)時(shí)弧岳,在第九個(gè)變量后凳忙,必須在變量數(shù)字周圍加上花括號(hào){},例如${10}禽炬,如下例所示:
#!/bin/bash res=$[ ${10} * ${11} ] echo "The tenth parameter is ${10}" echo "The eleventh parameter is ${11}" echo "The final result is $res" # 結(jié)果 [njust@njust tutorials]$ ./bar4.sh 1 2 3 4 5 6 7 8 9 10 11 The tenth parameter is 10 The eleventh parameter is 11 The final result is 110
1.2 讀取腳本名
- 可以使用$0參數(shù)獲取shell在命令行啟動(dòng)的腳本名稱涧卵,如下例所示:
#!/bin/bash echo "The zero parameter is set to: $0" # 結(jié)果 [njust@njust tutorials]$ bash bar5.sh # 注意此處運(yùn)行腳本的格式!8辜狻柳恐! The zero parameter is set to: bar5.sh
- 問(wèn)題一:如果使用./bar5.sh方式運(yùn)行腳本,命令會(huì)和腳本混在一起热幔,出現(xiàn)在$0參數(shù)中乐设。如下例所示:
[njust@njust tutorials]$ ./bar5.sh The zero parameter is set to: ./bar5.sh
- 問(wèn)題二:當(dāng)傳給$0變量的實(shí)際字符串不僅僅是腳本名,而是完整的腳本路徑時(shí)绎巨,變量$0就會(huì)使用整個(gè)路徑近尚。如下例所示:
[njust@njust tutorials]$ bash /home/njust/tutorials/bar5.sh The zero parameter is set to: /home/njust/tutorials/bar5.sh
- 解決方法:為了把腳本的運(yùn)行路徑給剝離掉,同時(shí)场勤,還要?jiǎng)h除與腳本名混雜在一起的命令戈锻,可以使用basename命令會(huì)返回不包含路徑的腳本名。如下例所示:
#!/bin/bash name=$(basename $0) echo echo "The script name is:$name" # 結(jié)果 [njust@njust tutorials]$ ./bar6.sh The script name is:bar6.sh # 結(jié)果 [njust@njust tutorials]$ bash bar6.sh The script name is:bar6.sh
-
可以使用上述方法來(lái)編寫基于腳本名執(zhí)行不同功能的腳本却嗡。如下例所示:
#!/bin/bash name=$(basename $0) if [ $name = "addem" ] then res=$[ $1 + $2 ] elif [ $name = "multem" ] then res=$[ $1 * $2 ] fi echo "The calculated value is:$res" # 通過(guò)復(fù)制文件創(chuàng)建addem [njust@njust tutorials]$ cp bar7.sh addem [njust@njust tutorials]$ chmod u+x bar7.sh # 通過(guò)軟鏈接創(chuàng)建multem [njust@njust tutorials]$ ln -s bar7.sh multem # 查看創(chuàng)建的文件 [njust@njust tutorials]$ ls -l *em -rwxrw-r--. 1 njust njust 180 4月 2 21:01 addem lrwxrwxrwx. 1 njust njust 7 4月 2 21:02 multem -> bar7.sh # 最終結(jié)果 [njust@njust tutorials]$ ./addem 2 8 The calculated value is:10 [njust@njust tutorials]$ ./multem 2 8 The calculated value is:16
1.3 測(cè)試參數(shù)
- 在shell腳本中使用命令行參數(shù)時(shí)要小心舶沛,如果不加參數(shù)運(yùn)行嘹承,可能會(huì)出現(xiàn)問(wèn)題窗价。如下例所示:
[njust@njust tutorials]$ ./addem 4 ./addem:行7: 4 + : 語(yǔ)法錯(cuò)誤: 期待操作數(shù) (錯(cuò)誤符號(hào)是 "+ ") The calculated value is:
- 當(dāng)腳本認(rèn)為參數(shù)變量中會(huì)有數(shù)據(jù)而實(shí)際上并沒(méi)有時(shí),腳本很可能會(huì)產(chǎn)生錯(cuò)誤消息叹卷。這樣的腳本寫法并不好撼港,在使用參數(shù)前一定要檢查其中是否存在數(shù)據(jù)。如下例所示:
#!/bin/bash if [ -n "$1" ] # -n測(cè)試來(lái)檢查命令行參數(shù)$1中是否有數(shù)據(jù) then echo "Hello $1,glad to meet you." else echo "Sorry,you did not identify yourself." fi # 結(jié)果 [njust@njust tutorials]$ ./bar8.sh Curry Hello Curry,glad to meet you. [njust@njust tutorials]$ ./bar8.sh Sorry,you did not identify yourself.
2.特殊參數(shù)變量
- 在bash shell中有些特殊變量骤竹,它們會(huì)記錄命令行參數(shù)帝牡。具體如下介紹:
2.1 參數(shù)統(tǒng)計(jì)
- 在腳本中使用命令行參數(shù)之前應(yīng)該檢查一下命令行參數(shù),可以統(tǒng)計(jì)一下命令行中輸入了多少個(gè)參數(shù)蒙揣,無(wú)需測(cè)試每個(gè)參數(shù)靶溜。bash shell提供了特殊變量$#,它含有腳本運(yùn)行時(shí)攜帶的命令行參數(shù)的個(gè)數(shù)懒震,可以在腳本中任何地方使用這個(gè)特殊變量罩息,與普通變量一樣。如下例所示:
#!/bin/bash echo "There were $# parameters supplied." # 結(jié)果 [njust@njust tutorials]$ ./bar9.sh There were 0 parameters supplied. [njust@njust tutorials]$ ./bar9.sh 1 2 3 There were 3 parameters supplied.
- 在使用參數(shù)前測(cè)試參數(shù)的總和个扰,如下例所示:
#!/bin/bash if [ $# -ne 2 ] # -ne測(cè)試命令行參數(shù)數(shù)量瓷炮,如果參數(shù)數(shù)量不對(duì),就會(huì)顯示一條錯(cuò)誤消息告知腳本的正確用法递宅。 then echo "Usage: bar10.sh a b" else res=$[ $1 + $2 ] echo "result is $res" fi # 結(jié)果 [njust@njust tutorials]$ ./bar10.sh Usage: bar10.sh a b [njust@njust tutorials]$ ./bar10.sh 2 Usage: bar10.sh a b [njust@njust tutorials]$ ./bar10.sh 2 3 result is 5
- $#變量含有參數(shù)的總和娘香,因此變量${!#}就代表了命令行中最后一個(gè)參數(shù)苍狰。注意:當(dāng)命令行上沒(méi)有任何參數(shù)時(shí),$#的值為0烘绽,params變量的值也一樣淋昭,但${!#}變量會(huì)返回命令行的腳本名,如下例所示:
#!/bin/bash params=$# echo "The last parameter is $params." echo "The last parameter is ${!#}" # 結(jié)果 [njust@njust tutorials]$ ./bar11.sh 12 3 42 The last parameter is 3. The last parameter is 42 [njust@njust tutorials]$ ./bar11.sh The last parameter is 0. The last parameter is ./bar11.sh
2.2 抓取所有的數(shù)據(jù)
- 有時(shí)候需要抓取命令行上所有的參數(shù)安接,此時(shí)不需要先用$#變量來(lái)判斷命令行上總的參數(shù)响牛,然后再進(jìn)行遍歷『斩危可以使用$*和$@變量來(lái)訪問(wèn)所有的變量呀打,這兩個(gè)變量都能夠在單個(gè)變量中存儲(chǔ)所有的命令行參數(shù)。
- $*變量會(huì)將命令行上提供的所有參數(shù)當(dāng)作一個(gè)單詞保存糯笙,這個(gè)單詞包含了命令行中出現(xiàn)的每個(gè)參數(shù)值贬丛,基本上$*變量會(huì)將這些參數(shù)視為一個(gè)整體;
- $@變量會(huì)將命令行上提供的所有參數(shù)當(dāng)成同一個(gè)字符串中的多個(gè)獨(dú)立的單詞给涕,這個(gè)就可以通過(guò)遍歷所有的參數(shù)值豺憔,得到每個(gè)參數(shù)。通常通過(guò)for命令來(lái)實(shí)現(xiàn)够庙。
- 兩個(gè)變量的具體功能恭应,如下例所示:
[njust@njust tutorials]$ ./bar12.sh 1 3 4 5 2 Using the $* method: 1 3 4 5 2 Using the $@ method: 1 3 4 5 2
- 兩個(gè)變量的具體區(qū)別,如下例所示:
#!/bin/bash count=1 for param in "$*" do echo "\$* parameter #$count = $param" count=$[ $count + 1 ] done echo echo cnt=1 for param in "$@" do echo "\$@ parameter #$cnt = $param" cnt=$[ $cnt + 1 ] done # 結(jié)果 [njust@njust tutorials]$ ./bar13.sh Curry Harden Durant James $* parameter #1 = Curry Harden Durant James $@ parameter #1 = Curry $@ parameter #2 = Harden $@ parameter #3 = Durant $@ parameter #4 = James
3.移動(dòng)變量
- bash shell的shift命令能夠用來(lái)操作命令行參數(shù)耘眨,shift命令會(huì)根據(jù)它們的相對(duì)位置來(lái)移動(dòng)命令行參數(shù)昼榛。默認(rèn)情況下,它會(huì)將每個(gè)參數(shù)變量向左移動(dòng)一個(gè)位置剔难。所以胆屿,變量$3的值會(huì)移動(dòng)$2,變量$2的值會(huì)移到$1中偶宫,變量$1的值會(huì)被刪除非迹。但是,變量$0的值不會(huì)被改變纯趋,因?yàn)樗浅绦蛎?/strong>憎兽。通過(guò)測(cè)試第一個(gè)參數(shù)值的長(zhǎng)度執(zhí)行了一個(gè)while循環(huán),當(dāng)?shù)谝粋€(gè)參數(shù)的長(zhǎng)度為零時(shí)吵冒,循環(huán)結(jié)束纯命。測(cè)試完第一個(gè)參數(shù)后,shift命令會(huì)將所有參數(shù)的位置移動(dòng)一個(gè)位置桦锄,如下例所示:
#!/bin/bash count=1 while [ -n "$1" ] do echo "Parameter #$count = $1" count=$[ $count + 1 ] shift done # 結(jié)果 [njust@njust tutorials]$ ./bar14.sh Curry Harder Durant James Parameter #1 = Curry Parameter #2 = Harder Parameter #3 = Durant Parameter #4 = James
- 也可以一次性移動(dòng)多個(gè)位置扎附,只需要給shift命令提供一個(gè)參數(shù),指明要移動(dòng)的位置數(shù)就行结耀。如下例所示:
#!/bin/bash echo "The original parameters: $*" shift 2 echo "Here's the new first parameter: $1" # 結(jié)果 [njust@njust tutorials]$ ./bar15.sh 3 4 253 5 38 The original parameters: 3 4 253 5 38 Here's the new first parameter: 253
4.處理選項(xiàng)
- 選項(xiàng)是跟在單折線后面的單個(gè)字母留夜,它能改變命令的行為匙铡。下面將介紹3種在腳本中處理選項(xiàng)的方法。
4.1 查找選項(xiàng)
-
處理簡(jiǎn)單選項(xiàng):在提取每個(gè)單獨(dú)參數(shù)時(shí)碍粥,用case語(yǔ)句來(lái)判斷某個(gè)參數(shù)是否為選項(xiàng)鳖眼。如下例所示:
#!/bin/bash while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) echo "Found the -b option";; -c) echo "Found the -c option";; *) echo "$1 is not an option";; esac shift done # 結(jié)果 [njust@njust tutorials]$ ./bar16.sh -a -b -c -d Found the -a option Found the -b option Found the -c option -d is not an option
-
分離參數(shù)和選項(xiàng):在shell腳本中同時(shí)使用選項(xiàng)和參數(shù)的情況,Linux中處理這個(gè)問(wèn)題的標(biāo)準(zhǔn)方法是用特殊字符來(lái)將兩者分開(kāi)嚼摩,該字符會(huì)告訴腳本何時(shí)選項(xiàng)結(jié)束以及普通參數(shù)何時(shí)開(kāi)始钦讳。對(duì)Linux來(lái)說(shuō),這個(gè)特殊字符是--枕面,shell會(huì)用雙破折線來(lái)表明選項(xiàng)列表已經(jīng)結(jié)束愿卒。在雙破折線后,腳本就可以放心將剩下的命令行參數(shù)當(dāng)作參數(shù)潮秘,而不是選項(xiàng)琼开。如下例所示:
#!/bin/bash while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) echo "Found the -b option";; -c) echo "Found the -c option";; --) shift break;; *) echo "$1 is not an option";; esac shift done count=1 for param in $@ do echo "Parameter #$count: $param" count=$[ count + 1 ] done # 結(jié)果 [njust@njust tutorials]$ ./bar17.sh -c -a -b -- test1 test2 test3 Found the -c option Found the -a option Found the -b option Parameter #1: test1 Parameter #2: test2 Parameter #3: test3
-
處理帶值的選項(xiàng):有些選項(xiàng)會(huì)帶上一個(gè)額外的參數(shù)值,如./bar666.sh -a test1 -b -c -d test2枕荞。當(dāng)命令行選項(xiàng)要求額外的參數(shù)時(shí)柜候,腳本必須能檢測(cè)到并正確處理。如下例所示:
#!/bin/bash while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) param="$2" echo "Found the -b option,with parameter value $param." shift;; -c) echo "Found the -c option";; --) shift break;; *) echo "$1 is not an option";; esac shift done count=1 for param in "$@" do echo "Parameter #$count: $param" count=$[ $count + 1 ] done # 結(jié)果 [njust@njust tutorials]$ ./bar19.sh -a -b test1 -f -- demo1 demo2 Found the -a option Found the -b option,with parameter value test1. -f is not an option Parameter #1: demo1 Parameter #2: demo2
4.2 使用getopt命令
- getopt命令是一個(gè)在處理命令行選項(xiàng)和參數(shù)時(shí)非常方便的工具躏精,它能識(shí)別命令行參數(shù)渣刷,從而在腳本中解析它們時(shí)更方便。
-
命令格式:getopt命令可以接受一系列任意形式的命令行選項(xiàng)和參數(shù)矗烛,并自動(dòng)轉(zhuǎn)換成適當(dāng)?shù)母袷礁ú瘢唧w命令格式如下所示:
getopt optstring parameters
- optstring定義了命令行有效的選項(xiàng)字母,還定義了哪些選項(xiàng)字母需要參數(shù)值高诺。optstring中列出了要在腳本中用到的每個(gè)命令行選項(xiàng)字母碌识。然后碾篡,在每個(gè)需要參數(shù)值的選項(xiàng)字母后加一個(gè)冒號(hào)虱而。注意:getopt命令有一個(gè)更高級(jí)的版本即getopts,注意兩者的區(qū)別开泽。具體實(shí)例如下所示:
[njust@njust tutorials]$ getopt ab:cd -a -b test1 -cd test2 test3 -a -b test1 -c -d -- test2 test3
- 上述實(shí)例中牡拇,optstring定義了四個(gè)有效選項(xiàng)字母:a、b穆律、c惠呼、d。字母b后面的冒號(hào)表示b選項(xiàng)需要一個(gè)參數(shù)值峦耘。注意:getopt命令會(huì)將-cd選項(xiàng)分成兩個(gè)單獨(dú)的選項(xiàng)剔蹋,并插入雙破折線來(lái)分隔行中的額外參數(shù)。
- 如果指定了一個(gè)不在abcd選項(xiàng)中的參數(shù)辅髓,默認(rèn)情況下泣崩,getopt命令會(huì)產(chǎn)生一條錯(cuò)誤的信息少梁。如下所示:
[njust@njust tutorials]$ getopt ab:cd -a -b test1 -cdf test2 test3 getopt:無(wú)效選項(xiàng) -- f -a -b test1 -c -d -- test2 test3
- 如果需要忽略這條錯(cuò)誤信息,可以在命令后加上-q選項(xiàng)矫付,如下所示:
[njust@njust tutorials]$ getopt -q ab:cd -a -b test1 -cdf test2 test3 -a -b 'test1' -c -d -- 'test2' 'test3'
-
在腳本中使用getopt:用getopt命令生成的格式后的版本來(lái)替換自己已有的命令行選項(xiàng)和參數(shù)凯沪,用set命令能夠做到。set命令使用的方法如下:
set -- $(getopt -q ab:cd "$@")
- 利用上述方法买优,可以幫我們處理命令行參數(shù)的腳本妨马。如下例所示:
#!/bin/bash set -- $(getopt -q ab:cd "$@") # 注意此條語(yǔ)句!I庇烘跺! while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) param="$2" echo "Found the -b option,with parameter value $param." shift;; -c) echo "Found the -c option";; --) shift break;; *) echo "$1 is not an option";; esac shift done count=1 for param in "$@" do echo "Parameter #$count: $param" count=$[ $count + 1 ] done # 結(jié)果 [njust@njust tutorials]$ ./bar20.sh -ac # 合并選項(xiàng)情況下也能處理! Found the -a option Found the -c option
-
但是,getopt命令也存在一個(gè)小問(wèn)題脂崔。它并不擅長(zhǎng)處理帶空格和引號(hào)的參數(shù)值液荸。它會(huì)將空格當(dāng)成參數(shù)分隔符,而不是根據(jù)雙引號(hào)將兩者看成一個(gè)參數(shù)脱篙,解決方法是使用更高級(jí)的getopts命令娇钱。
[njust@njust tutorials]$ ./bar20.sh -a -b test1 -c "test2 test3" test4 Found the -a option Found the -b option,with parameter value 'test1'. Found the -c option Parameter #1: 'test2 Parameter #2: test3' Parameter #3: 'test4'
4.3 使用更高級(jí)的getopts命令
- getopts命令是shell中的內(nèi)建命令,它比getopt命令多了一些擴(kuò)展功能绊困。getopt命令將命令行上選項(xiàng)和參數(shù)處理后只生成一個(gè)輸出文搂,而getopts命令能夠和已有的shell參數(shù)變量配合使用。每次調(diào)用它時(shí)秤朗,它一次只處理命令行上檢測(cè)到的一個(gè)參數(shù)煤蹭,處理完所有的參數(shù)后,它會(huì)退出并返回一個(gè)大于0的退出狀態(tài)碼取视,這讓它非常適合用解析命令行所有參數(shù)的循環(huán)中硝皂。getopts命令的格式如下:
getopts optstring variable
- optstring值類似于getopt命令中的那個(gè),getopts命令將當(dāng)前參數(shù)保存在命令行中定義的variable中作谭。getopts命令會(huì)用到兩個(gè)環(huán)境變量稽物,如果選項(xiàng)需要跟一個(gè)參數(shù)值,OPTARG環(huán)境變量就會(huì)保存這個(gè)值折欠,OPTIND環(huán)境變量保存了參數(shù)列表中g(shù)etopts正在處理的參數(shù)位置贝或,這樣你就能在處理完選項(xiàng)后繼續(xù)處理其他命令行參數(shù)了。如下例所示:
#!/bin/bash while getopts :ab:c opt do case "$opt" in a) echo "Found the -a option";; # 注意:getopts命令在解析命令行選項(xiàng)時(shí)會(huì)移除開(kāi)頭的單破折線锐秦,所以在case定義中不用單破折線咪奖! b) echo "Found the -b option,with value $OPTARG";; c) echo "Found the -c option";; *) echo "Unknown option: $opt";; esac done # 結(jié)果 [njust@njust tutorials]$ ./bar21.sh -ab test1 -c Found the -a option Found the -b option,with value test1 Found the -c option
- getopts命令的優(yōu)點(diǎn)之一是可以在參數(shù)值中包含空格,如下例所示:
[njust@njust tutorials]$ ./bar21.sh -b "test1 test2" -a Found the -b option,with value test1 test2 Found the -a option
getopts命令的另一個(gè)優(yōu)點(diǎn)是將選項(xiàng)字母和參數(shù)值放在一起使用酱床,而不用加空格羊赵。如下例所示:
[njust@njust tutorials]$ ./bar21.sh -abdemo1 Found the -a option Found the -b option,with value demo1
- 此外,getopts命令還可以將命令行上找到的所有未定義的選項(xiàng)統(tǒng)一輸出為問(wèn)號(hào)扇谣,如下例所示:
[njust@njust tutorials]$ ./bar21.sh -acdf Found the -a option Found the -c option Unknown option: ? Unknown option: ?
- getopts命令知道何時(shí)停止處理選項(xiàng)昧捷,并將參數(shù)留給你處理揖闸。在getopts處理每個(gè)選項(xiàng)時(shí),它會(huì)將OPTIND環(huán)境變量值加1料身,在getopts完成處理時(shí)汤纸,使用shift命令和OPTIND值來(lái)移動(dòng)參數(shù)。如下例所示:
#!/bin/bash while getopts :ab:cd opt do case "$opt" in a) echo "Found the -a option";; b) echo "Found the -b option,with value $OPTARG";; c) echo "Found the -c option";; d) echo "Found the -d option";; *) echo "Unknown option: $opt";; esac done shift $[ $OPTIND - 1 ] count=1 for param in "$@" do echo "Parameter $count:$param" count=$[ $count + 1 ] done # 結(jié)果 [njust@njust tutorials]$ ./bar22.sh -a -b test1 -d test2 test3 test4 Found the -a option Found the -b option,with value test1 Found the -d option Parameter 1:test2 Parameter 2:test3 Parameter 3:test4
5.將選項(xiàng)標(biāo)準(zhǔn)化
- 下面是Linux中常用到的一些命令行選項(xiàng)的含義芹血。
選項(xiàng) 含義 -a 顯示所有對(duì)象 -c 生成一個(gè)計(jì)數(shù) -d 指定一個(gè)目錄 -e 擴(kuò)展一個(gè)對(duì)象 -f 指定讀入數(shù)據(jù)的文件 -h 顯示命令的幫助信息 -i 忽略文本大小寫 -l 產(chǎn)生輸出的長(zhǎng)格式版本 -n 使用非交互模式 -o 將所有輸出重定向到指定的輸出文件 -q 以安靜模式運(yùn)行 -r 遞歸地處理目錄和文件 -s 以安靜模式運(yùn)行 -v 生成詳細(xì)輸出 -x 排除某個(gè)對(duì)象 -y 對(duì)所有問(wèn)題回答yes
6.獲得用戶輸入
- 盡管命令行選項(xiàng)和參數(shù)是從腳本用戶處獲得輸入的一種重要方式贮泞,但是有時(shí)候腳本的交互性還需要更強(qiáng)一些。例如在運(yùn)行腳本時(shí)問(wèn)一個(gè)問(wèn)題幔烛,并等待運(yùn)行腳本的人來(lái)回答啃擦,bash shell為此提供了read命令。
6.1 基本的讀取
- read命令從標(biāo)準(zhǔn)輸入或另一個(gè)文件描述符中接收輸入饿悬。在收到輸入后令蛉,read命令會(huì)將數(shù)據(jù)放進(jìn)一個(gè)變量,如下例所示:
#!/bin/bash echo -n "Please Enter your name: " # -n選項(xiàng)不會(huì)在字符串末尾輸出換行符狡恬,允許腳本用戶緊跟其后輸入數(shù)據(jù)珠叔,而不是在下一行輸入! read name echo "Hello $name,welcome to my home." # 結(jié)果 [njust@njust tutorials]$ ./bar23.sh Please Enter your name: Curry # 自己輸入的弟劲! Hello Curry,welcome to my home.
- read命令包含了-p選項(xiàng)祷安,它允許你直接在read命令行指定提示符,如下例所示:
#!/bin/bash read -p "Please Enter your age: " age days=$[ $age * 365 ] echo "That makes you over $days days old" # 結(jié)果 [njust@njust tutorials]$ ./bar24.sh Please Enter your age: 28 That makes you over 10220 days old
- 也可以在read命令行中不指定變量兔乞,read命令會(huì)將它收到的任何數(shù)據(jù)都放進(jìn)特殊環(huán)境變量REPLY中汇鞭,REPLY環(huán)境變量會(huì)保存輸入的所有數(shù)據(jù),可以在shell腳本中像其他變量一樣使用庸追。如下例所示:
#!/bin/bash read -p "Enter your name: " echo "Hello $REPLY,welcome to NewYork." # 結(jié)果 [njust@njust tutorials]$ ./bar25.sh Enter your name: Stephen Curry Hello Stephen Curry,welcome to NewYork.
6.2 超時(shí)
- 使用read命令時(shí)一定要注意霍骄,腳本很可能會(huì)一直等待用戶的輸入。如果不管是否有數(shù)據(jù)輸入淡溯,腳本都必須繼續(xù)執(zhí)行读整,可以使用-t選項(xiàng)來(lái)指定一個(gè)計(jì)數(shù)器。-t選項(xiàng)指定了read命令等待輸入的秒數(shù)血筑,當(dāng)計(jì)數(shù)器超時(shí)后绘沉,read命令會(huì)返回一個(gè)非零退出狀態(tài)碼,如下例所示:
#!/bin/bash if read -t 5 -p "Please enter your name: " name then echo "Hello $name,welcome to NewYork." else echo echo "Sorry, it's wrong!" fi # 結(jié)果 [njust@njust tutorials]$ ./bar26.sh Please enter your name: Sorry, it's wrong!
- 也可以不對(duì)輸入過(guò)程計(jì)時(shí)豺总,而是讓read命令來(lái)統(tǒng)計(jì)輸入的字符數(shù),當(dāng)輸入的字符達(dá)到預(yù)設(shè)的字符數(shù)時(shí)择懂,就自動(dòng)退出喻喳,將輸入的數(shù)據(jù)賦值給變量。如下例所示:
#!/bin/bash read -n1 -p "Do you want to continue [Y/N]? " answer case $answer in Y | y) echo echo "Fine,continue on...";; N | n) echo echo "Ok,goodbye" exit;; esac echo "This is the end of the script" # 結(jié)果 [njust@njust tutorials]$ ./bar27.sh Do you want to continue [Y/N]? Y Fine,continue on... This is the end of the script
- 上述例子中困曙,-n選項(xiàng)與值1一起使用表伦,它告訴read命令在接收單個(gè)字符后退出谦去。只要按下單個(gè)字符后,read命令就會(huì)接收輸入并將它傳給變量蹦哼,無(wú)需按下回車鍵鳄哭。
6.3 隱藏方式讀取
- 有時(shí)候需要從用戶輸入處得到輸入,但又不能在屏幕中顯示輸入纲熏。其中典型的例子就是輸入的密碼妆丘,此外還有很多其他需要隱藏的數(shù)據(jù)類型。-s選項(xiàng)可以避免在read命令中輸入的數(shù)據(jù)出現(xiàn)在屏幕上局劲,如下例所示:
#!/bin/bash read -s -p "Enter you password: " passwd echo echo "Is your password really $passwd? " # 結(jié)果 [njust@njust tutorials]$ ./bar28.sh Enter you password: Is your password really 12345?
6.4 從文件中讀取
- 可以用read命令來(lái)讀取Linux系統(tǒng)上文件中保存的數(shù)據(jù)勺拣,每次調(diào)用read命令,它都會(huì)從文件中讀取一行文本鱼填。當(dāng)文本中沒(méi)有內(nèi)容時(shí)药有,read命令會(huì)退出并返回非零退出狀態(tài)碼。注意:最難的部分是將文件中的數(shù)據(jù)傳給read命令苹丸,最常見(jiàn)的方法是對(duì)文件使用cat命令愤惰,將結(jié)果通過(guò)管道直接傳給含有read命令的while命令。如下例所示:
#!/bin/bash count=1 cat demodemo | while read line do echo "Line $count: $line" count=$[ $count + 1 ] done echo "Finished processing the file" # 結(jié)果 [njust@njust tutorials]$ ./bar29.sh Line 1: The quick brown dog jumps over the lazy fox. Line 2: This is a test, this is only a test. Line 3: O Romeo, Romeo!Wherefore art thou Romeo? Finished processing the file