Linux命令列與shell指令碼程式設計大全(二)
阿新 • • 發佈:2018-11-11
十一、處理使用者輸入
命令列引數
讀取引數: $0是程式名,$1是第一個引數,$2是第二個引數,以此類推,直到第9個引數$9。當引數個數超過10以後,需要在變數數字周圍加上花括號,如${10},如果輸入到命令列的引數是字串且含有空格,需要使用引號。
#! /bin/bash echo "Your name is $1" total=$[ ${10} * ${11} ] echo "${10} * ${11} is $total" [email protected]:~/Desktop/Scripts# ./test14.sh 'Rich Blum' 2 3 4 5 6 7 8 9 10 11 Your name is Rich Blum 10 * 11 is 110
特殊引數變數
引數統計: $#含有指令碼執行時攜帶的命令列引數的個數,可以在指令碼任何地方使用這個特殊的變數,和普通變數一樣。如果要獲取最後一個引數,需要使用 ${!#}
抓取所有資料: $*變數會將命令列上所提供的所有慘所當作一個單詞儲存。 [email protected]變數將命令列上提供的所有引數當作同一個字串中的多個獨立的單詞,可以通過for遍歷所有的引數值。
#! /bin/bash # $#統計引數個數 echo "there were $# parameters supplied" # ${!#}獲取最後一個引數 param=$# echo "the last parameter is ${!#}" ##當沒有引數返回指令碼名 echo "the second trial last parameter is ${param}" ##當沒有引數時返回0 # $# 和
[email protected]抓取所有資料 # test for '$*' count=1 for param in "$*" do echo "\$* param #$count=$param" count=$[ $count + 1 ] done # test for '[email protected]' count=1 for param in "[email protected]" do echo "\[email protected] param #$count=$param" count=$[ $count + 1 ] done
移動變數
使用shift命令時,預設情況下它會將每個引數變數向左移動一個位置,$3的值會移到$2中,$2會移到$1中,而$1的值則會被刪除,無法再恢復。
#! /bin/bash
# shift命令移動變數
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count + 1 ]
shift
done
處理選項
查詢選項
1.對於簡單選項,可以使用case語句來判斷某個引數是否為選項。
## 簡單選項
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
[email protected]:~/Desktop/Scripts# ./test14_4.sh -a -b -c -d
found the -a option
found the -b option
found the -c option
-d is not an option
2.分離引數和選項,使用特殊字元(--)表明選項列表結束。
## 分離引數和選項
while [ -n "$1" ]
do
case "$1" in
-a) echo "fount the -a option" ;;
-b) echo "fount the -b option" ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac
shift
done
count=1
for param in "[email protected]"
do
echo "parameter #$count : $param"
count=$[ $count + 1 ]
done
[email protected]:~/Desktop/Scripts# ./test14_4.sh -a -b -c -d -- test1 test2 test3
fount the -a option
fount the -b option
-c is not an option
-d is not an option
parameter #1 : test1
parameter #2 : test2
parameter #3 : test3
3. 處理帶值的選項。
## 處理帶值的選項
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) param=$2
echo "fount the -b option,with parameter value $param"
shift ;;
-c) echo "fount the -c option" ;;
--) shift
break;;
*) "$1 is not an option" ;;
esac
shift
done
count=1
for param in "[email protected]"
do
echo "parameter #$count : $param"
count=$[ $count + 1 ]
done
[email protected]:~/Desktop/Scripts# ./test14_4.sh -a -b test1 -c -d -- test2 test3 test4
found the -a option
fount the -b option,with parameter value test1
fount the -c option
./test14_4.sh: line 47: -d is not an option: command not found
parameter #1 : test2
parameter #2 : test3
parameter #3 : test4
使用getopt命令
## 使用getopt命令,可以將選項和起來使用,當引數帶空格時識別不了。
# 命令格式 getopt optstring parameters
# getopt ab:cd -a -b test1 -cd test2 test3,定義了4個有效字母a,b,c,d,冒號(:)被放在b之後,因為b需要一個引數值
set -- $(getopt -q ab:cd "[email protected]") ##-q選項去掉錯誤訊息
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) param=$2
echo "fount the -b option,with paramter value $param"
shift ;;
-c) echo "fount the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac
shift
done
count=1
for param in "[email protected]"
do
echo "parameter #$count : $param"
count=$[ $count + 1 ]
done
[email protected]:~/Desktop/Scripts# ./test14_4.sh -ab test1 -cd test2 test3
Found the -a option
fount the -b option,with paramter value 'test1'
fount the -c option
-d is not an option
parameter #1 : 'test2'
parameter #2 : 'test3'
使用getops命令
## 使用getopts命令
# 命令格式同getopt,如果要去掉錯誤訊息,需要在optstring前加一個冒號
while getopts :ab:c 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" ;;
*) echo "unknown option :$opt" ;;
esac
done
shift $[ $OPTIND -1 ]
count=1
for param in "[email protected]"
do
echo "Parameter $count : $param"
count=$[ $count + 1 ]
done
[email protected]:~/Desktop/Scripts# ./test14_4.sh -ab test1 -cd test2 test3
found the -a option
found the -b option,with value test1
found the -c option
unknown option :?
Parameter 1 : test2
Parameter 2 : test3
獲得使用者輸入
read命令從標準輸入或者另一個檔案描述符中接受輸入。
#! /bin/bash
## read 命令
echo -n "please enter your name: "
read name
echo "Hello $name,welcome to my program"
## -p 選項允許直接在read命令列指定提示符
read -p "Please enter your age:" age
day=$[ $age * 365 ]
echo "that makes you over $day days old"
## 如果在read命令列不指定變數,read命令把它收到的任何資料都會放在REPLY中
read -p "Enter your name: "
echo "Hello $REPLY,welcome to my program"
## -t選項設定超時
if read -t 5 -p "Please enter you name:" name
then
echo "Hello $name,welcome to my script"
else
echo
echo "sorry,too slow"
fi
## -n選項設定讀取的字元個數
read -n1 -p "Do you want to continue [Y/N]:" ##-n和1一起使用,表示read在接受單個字元後退出
## -s選項隱藏方式讀取,通常用於輸入密碼
read -s -p "Enter your password:" pass
echo "Is your password really $pass"
## 從檔案中讀取,通常使用cat命令將結果通過管道直接傳給含有read命令的while命令
count=1
cat test14_2.sh | while read line
do
echo "Line $count: $line"
count=$[ $count + 1 ]
done