[code]$ ./addem 10 30
$0:程序名
$1:第一個參數
2:第二個參數,以此類推,直到第9個參數2:第二個參數,以此類推,直到第9個參數9
[code]$cat test #!/bin/bash total=$[ $1 * $2 ] echo The total value is $total. $./test 2 5 The total value is 10.shell參數可以是字符串,每個參數都是用空格分隔的,所以shell會將空格當成分隔兩個值的分隔符
參數值中包含空格,必須要用引號(單引號雙引號都可以)
[code]#cat test #!/bin/bash echo Hello $1, glad to meet you $./test 'Rich Blum' Hello Rich Blum, glad to meet you. $腳本需要多於9個命令行參數時,需要使用花括號({}),比如,${10}
basename命令會只返回程序名而不包括路徑
[code]$cat test #!/bin/bash name=`basename $0` echo The command entered is : $0 echo The command entered is : name $./test The command entered is : ./test The command entered is : test $/home/rich/test The command entered is : /home/rich/test The command entered is : test $
解決方法:在使用參數前檢查參數[ -n “$1” ]
當命令行上沒有任何參數時,#的值為0,在params變量中也為0,但#的值為0,在params變量中也為0,但{!#}變量返回命令行用到的腳本名
[code]$cat test
#!/bin/bash
if [ $# -ne 2 ]
then
echo Usage: test a b
else
total=$[ $1 + $2 ]
echo The total is $total
fi
$./test
Usage: test a b
$./test 10
Usage: test a b
$./test 10 15
The total is 25
$./test 10 15 20
Usage: test a b$*:會將命令行上提供的所有參數當做單個單詞保存,即當成一個參數
$@:將命令行上提供的所有參數當做同一字符串中的多個獨立的單詞,允許遍歷所有的值,將提供的每個參數分隔開來,通常用for命令完成
shift命令可以提供一個參數n來執行多位移動,如shift 2:連續移動2位
當一個參數被移除後,它的值會被丟掉無法恢復
變量$0的值,也就是程序名不會改變
可以用shift命令遍歷命令行參數,尤其在不知道到底有多少個參數的時候
[code]$cat test
#!/bin/bash
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count + 1 ]
shift
done
$
$./test rich barbara
parameter #1 = rich
Parameter #2 = barbara[code]$cat test
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) echo "Found the -b option";;
*) echo "$1 is not an option";;
esac
shift
done
$
$./test -a -c
Found the -a option
-c is not an option
$分離參數和選項 shell會用雙破折線來表明選項結束了,遇到雙破折線之後,腳本會安全地將剩下的命令行參數當做參數來處理,而不是選項
[code]$cat test
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) echo "Found the -b 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
$
$./test -a test1
Found the -a option
test1 is not an option
$./test -a -- test1
Found the -a option
Parameter #1: test1處理帶值的選項 當命令行選項要求額外的參數時,腳本必須能檢測並能正確的處理
[code]$cat test
#!/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 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
$
$./test -a -b test1 -d
Found the -a option
Found the -b option, with parameter value test1
-d is not an option
$
$./test -b test1 -a -d
Found the -b option, with parameter value test1
Found the -a option
-d is not an option
$如果將多個選項放進一個參數中時,它就不工作了[code]$ ./test -ac -ac is not an option $
命令格式:getopt options optstring parameters
optstring:定義了命令行有效的選項字母,還定義了哪些選項字母需要參數值
首先,在optstring中列出你要在腳本中用到的每個命令行選項字母
然後,在每個需要參數值的選項字母後加一個冒號
getopt命令會給予你定義的optstring解析提供的參數
[code]$getopt ab:cd -a -b test1 -cd test2 test3 -a -b test1 -c -d -- test2 test3 $如果指定了一個不在optstring中的選項,默認情況下,getopt命令會產生一條錯誤信息
如果想忽略這條錯誤,在命令後加上-q選項
[code]$getopt ab:cd -a -b test1 -cde test2 test3 getopt: invail option --e -a -b test1 -c -d -- test2 test3 $ $getopt -q ab:cd -a -b test1 -cde test2 test3 -a -b test1 -c -d -- test2 test3 $在腳本中使用getopt
可以在腳本中使用getopt來格式化輸入給腳本的任何命令行選項
方法:
首先,用getopt命令生成的格式化後的版本來替換已有的命令行選項和參數,用set命令可以做到
set命令的選項之一是雙破折號,它會將命令行參數替換成set命令的命令行的值
然後,該方法將原始的腳本命令行參數傳遞給getopt命令
之後再將getopt命令的輸出傳給set命令,用getopt格式化後的命令行參數來替換原始的命令行參數
看起來如下:
[code]set -- `getopts -q ab:cd "$@"`
[code]$cat test
#!/bin/bash
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
$
$./test -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 #1: 'test3'