寫在前面:案例、常用、歸類、解釋說明。(By Jim)
命令行參數
$1為第一個參數,$2為第二個參數,依次類推...
示例:
#!/bin/bash # using one command line parameter factorial=1 for((number = 1;number<=$1;number++)) do factorial=$[ $factorial*$number ] done echo The factorial of $1 is $factorial
調用
./test1 5
(這樣就把參數傳遞進去了)
結果:
The factorial of 5 is 120
#!/bin/bash # testing parameters before use if [ -n "$1" ] then echo Hello $1,glad to meet you. else echo "Sorry,you didn't identify yourself." fi
(最好能驗證一下是否有參數傳入,這樣最安全了,編程時也要這樣處理)
獲取所有數據
$* 所有參數作為一個單詞處理
$@ 所有參數作為多個單詞處理
$# 最后一個參數
#!/bin/bash # testing $* and $@ echo "Using the \$* method:$*" echo "Using the \$@ method:$@" ./test1 rich jessica tom
結果:
Using the $* method:rich jessica tom
Using the $@ method:rich jessica tom
貌似沒差別,不過我們來看一個例子,就知道差別了
#!/bin/bash # testing $* and $@ 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
測試:test1 1 2 3 4 5
結果:
$* Parameter #1 = 1 2 3 4 5
$@ Parameter #1 = 1
$@ Parameter #2 = 2
$@ Parameter #3 = 3
$@ Parameter #4 = 4
$@ Parameter #5 = 5
(由結果就能看出差別了)
移位
shift將參數變量左移一個位置,於是,變量$3的值就移給變量$2,變量$2的值移給變量$1,變量$1的值被丟棄。
#!/bin/bash # demonstrating the shift command count=1 while [ -n "$1" ] do echo "Parameter #$count = $1" count=$[ $count +1 ] shift done
執行:
test1 1 2 3 4 5
結果:
Parameter #1 = 1
Parameter #2 = 2
Parameter #3 = 3
Parameter #4 = 4
Parameter #5 = 5
(每次向前移一位,最后被置空了)
shift 2表示移動兩位
處理選項
#!/bin/bash # extracting options and 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" ;; --) 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
測試:
test1 -a -b -c test1 test2 -c
結果:
Found the -a option
Found the -b option
Found the -c option
test1 is not an option
test2 is not an option
Found the -c option
(每一個參數都遍歷一次)
測試2:test1 -a -b -c -- test1 test2 -a
結果:
Found the -a option
Found the -b option
Found the -c option
Parameter #1:test1
Parameter #2:test2
Parameter #3:-a
(跳出循環,並將剩余的參數存入$@中去,供下面的代碼使用,即便有-a出現也不會被發現了)
使用getopt命令
getopt -q ab:cd -a -b -test1 -cde test2 test3
(表示有a,b,c,d,其中b后面跟一個參數)
解析的結果為
-a -b '-test1' -c -d -- 'test2' 'test3'
getopt -q ab:cd -a -b -test1 -d -c -cd test2 -c test3
解析結果為
-a -b '-test1' -d -c -c -d -c -- 'test2' 'test3'
(由此可以看出,它會將所有的選項與參數分離,按照一定的順序,之間會自動加上--)
來看一段完整的代碼
#!/bin/bash # extracting options and parameters set -- `getopt -q ab:c "$@"` 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
測試:test1 -a -b test1 -cd test2 test3
結果:
Found the -a option
Found the -b option,with parameter value 'test1'
Found the -c option
Parameter #1:'test2'
Parameter #2:'test3'
(可以想象它就是按照解析的數據進行處理的)
getopts和它的堂兄弟getopt很相像。
案例:
#!/bin/bash # simple demonstration of the getopts command while getopts :ab:c opt do case "$opt" in a) echo "Found the -a option" ;; b) echo "Found the -b option,with parameter value $OPTARG";; c) echo "Found the -c option" ;; *) echo "Unknown option:$opt" ;; esac done
(它會自動處理移位,自動處理參數到變量$OPTARG,自動處理)
測試:
test1 -ab test1 -c -d
結果:
Found the -a option
Found the -b option,with parameter value test1
Found the -c option
Unknown option:?
getopts命令每個處理選項,環境變量OPTIND的值會加1,。當到達getopts處理的末尾時,可以使用shift命令
和OPTIND值進行操作來移動到參數。
看代碼:
#!/bin/bash # simple demonstration of the getopts command while getopts :ab:cd opt do case "$opt" in a) echo "Found the -a option" ;; b) echo "Found the -b option,with parameter 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
測試:test1 -a -b test1 -cd test2 test3
結果:
Found the -a option
Found the -b option,with parameter value test1
Found the -c option
Found the -d option
Parameter 1 : test2
Parameter 2 : test3
獲取用戶輸入
基本讀取
read命令接受標准輸入,得到輸入后,read命令將數據存放到一個標准變量中。
案例:
#!/bin/bash # testing read echo "Enter your name:" read name echo "Welcome ,$name !"
測試:
test1
Enter your name:
jim
Welcome ,jim !
#!/bin/bash # testing read echo -n "Enter your name:" read name echo "Welcome ,$name !"
測試:
[root@localhost shellscript]# test1
Enter your name:jim
Welcome ,jim !
(抑制后面新的字符出現,-n就不會換行了)
read有個-p命令,允許在read命令行中直接指定一個提示:
#!/bin/bash # testing read -p option read -p "Please enter your age:" age days=$[ $age * 360 ] echo "That makes you over $days days old!"
測試:
[root@localhost shellscript]# test1
Please enter your age:25
That makes you over 9000 days old!
(25是輸入的)
