bash shell如何獲取命令行參數(添加到命令后的數據)、命令行選項(確定命令行為的英文字母)、鍵盤輸入數據?
操作命令行參數
1 讀取參數
bash shell用位置參數變量(positional parameter)存儲命令行輸入的所有參數,包括程序名。其中,$0表示程序名,$1表示第1個參數,$2表示第2個參數,...,$9表示第9個參數。如果參數個數多於9個,必須如下表示變量:${10},${11},...
for((count = 1; count <= $1; count++)) do echo The number is $count. done
echo $1, $2, $10, $15
2 讀取程序名
首先想到的是利用$0,但是$0獲取的文件名包括./以及路徑等前綴信息,如下:
echo The command entered is: $0 # 運行:./ # 輸出:The command entered is: ./14.sh
如果想僅得到文件名,而不包含./,可以使用basename命令:
name=`basename $0` echo The command entered is: $name # 運行:./ # 輸出:The command entered is: 14.sh
3 特殊變量
$#表示命令行參數的個數:
params=$# echo The number of params is: $params for((i = 1; i <= params; i++)) do echo The param is: $i done
那么很自然地,${$#}表示最后一個變量的值,如下程序:
params=$# echo The number of params is: $params echo The last parameter is: ${$#}
這是怎么回事呢?原來在花括號內不能使用$符號,而是以!來代替$工作:
params=$# echo The number of params is: $params echo The last parameter is: ${!#}
如果想獲取所有的參數,當然可以利用$#和循環逐個遍歷。也可以利用如下兩個特殊變量:$*將所有的命令行參數看作一個整體存儲,而$@將命令行中以空格間隔的參數單獨存儲,如下:
count=1 for param in "$*" do echo "\$* parameter $count = $param" count=$[ $count + 1 ] done count=1 for param in "$@" do echo "\$@ parameter $count = $param" count=$[ $count + 1 ] done
4 移動變量命令shift
shift命令用於將后一個變量的值移到前一個變量上,即$3的值移到$2,$2的值移到$1,$1的值被刪除。 注:變量$0的值不會被替換。該命令可以使得在不需要知道有多少個參數的情況下即可遍歷所有參數:
count=1 while [ -n "$1" ] do echo "parameter $count = $1" count=$[ $count + 1 ] shift done
注:使用shift命令移除的參數值是無法恢復的,也可以使用shift移到多位,如:shift 2。

操作命令行選項
1 查找選項
可以判斷命令行參數是否存在有效選項:
#! /bin/bash # extracting command line options as parameters 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
在Linux中,命令行選項和參數的分離使用--符號,shell將--前邊的參數作為選項來處理。如下腳本可以有效地分離出選項和參數:
#! /bin/bash # extracting command line options as parameters 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";; --) break;; *) echo "$1 is not an option";; esac shift done shift # 確保跳出while循環后,將--符號移除掉 count=1 for param in $@ do echo "parameter $count: $param" count=$[ $count + 1 ] done
如何處理帶有參數值的選項呢?比如,./22.sh -a -b benxin -c -d
#! /bin/bash # extracting command line options as parameters 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 2;; -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
合並選項時可以使用getopt命令,格式如下:
getopt options optstring parameters
options定義了命令行中有效的選項字母以及哪些選項字母需要附加參數值等(在需要附加參數值的選項字母后加一個冒號),如getopt ab:cd -a -b benxin -cd tuzi benxintuzi,optstring定義了4個有效字母:a、b、c、d,並且說明了選項b需要附帶參數值。getopt命令自動將-cd拆分成-c和-d兩個選項,並且用雙破折號--隔開命令行中的額外參數。

2 選項的標准化約定
| 選項 |
說明 |
| -a |
顯示所有對象 |
| -c |
生成一個計數 |
| -d |
指定一個目錄 |
| -e |
擴展一個對象 |
| -f |
指定輸入文件 |
| -h |
顯示命令幫助 |
| -i |
忽略文本大小寫 |
| -l |
產生輸出的長格式版本 |
| -n |
使用非交互模式(批量) |
| -o |
指定輸出文件 |
| -q |
以安靜模式運行 |
| -r |
遞歸處理目錄或文件 |
| -s |
以安靜模式運行 |
| -v |
生成詳細輸出 |
| -x |
排除某個對象 |
| -y |
對所有問題回答yes |
操作鍵盤輸入
1 基本的讀取
read命令接受從鍵盤或文件描述符中的輸入數據,將其存儲到一個指定變量中。使用-p選項可以指定輸入提示信息,如下所示:
#! /bin/bash # testing the read option read -p "Please enter your name: " name echo "Hello $name."
read命令中,可以根據需要將輸入的數據保存在多個變量中,如果指定的變量比較少,那么最后一個變量將包含余下的所有輸入,如下所示:
#! /bin/bash # testing the read option read -p "Enter the values: " val1 val2 val3 echo "$val1" echo "$val2" echo "$val3"
注:如果使用read命令時不指定變量,那么read將其接收的任何數據都放到環境變量REPLY中。
2 輸入超時
使用read命令時,腳本會一直等待用戶輸入數據,如果想設置等待時間,使用-t選項即可指定一個計時器,當計時器超時后,read命令會返回一個非零狀態碼。
if read -t 5 -p "Please enter your name: " name then echo "Hello $name." else echo echo "Sorry, too slow" fi
3 隱藏輸入顯示
當輸入敏感信息時,比如密碼,不希望顯示在屏幕上,那么可以使用read的-s選項,-s選項的功能是將輸入文本顏色設置為與背景色一樣:
#! /bin/bash # testing the read option read -s -p "Enter your password: " passwd echo echo "Is your password really $passwd?"
4 從文件中讀取
read每次從文件中讀取一行數據,當文件中沒有數據時,read命令返回非零狀態碼。最常見的方法是將cat命令運行后的結果通過管道傳給含有read的while語句,如下:
#! /bin/bash # testing the read option count=1 cat 25.sh | while read line do echo "Line $count: $line" count=$[ $count + 1 ] done echo "Finished processing the file"












