$cat test4.sh #!/bin/bash # handling lots of parameters # total=$[ ${10} * ${11} ] echo The tenth parameter is ${10} echo The eleventh parameter is ${11} echo The total is $total $ $ ./test4.sh 123456789101112 The tenth parameter is 10 The eleventh parameter is 11 The total is 110 $
$cat test5.sh #!/bin/bash # Testing the $0 parameter # echo The zero parameter is set to: $0 $ $ bash test5.sh The zero parameter is set to: test5.sh
当传给$0变量的实际字符串不仅仅是脚本名,而是完整的脚本路径时, 变量$0就会使用整个路径。
1 2 3
$ bash /home/Christine/test5.sh The zero parameter is set to: /home/Christine/test5.sh $
basename命令会返回不包含路径的脚本名
1 2 3 4 5 6 7 8 9 10 11 12 13
$ cat test5b.sh #!/bin/bash # Using basename with the $0 parameter # name=$(basename$0) echo echo The script name is: $name # $ bash /home/Christine/test5b.sh The script name is: test5b.sh $ $ ./test5b.sh The script name is: test5b.sh
2. 特殊参数变量
2.1 $#
特殊变量$#含有脚本运行时携带的命令行参数的个数
案例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
$ cat test8.sh #!/bin/bash # getting the number of parameters # echo There were $# parameters supplied. $ $ ./test8.sh There were 0 parameters supplied. $ $ ./test8.sh 1 2 3 4 5 There were 5 parameters supplied. $ $ ./test8.sh 1 2 3 4 5 6 7 8 9 10 There were 10 parameters supplied. $ $ ./test8.sh "Rich Blum" There were 1 parameters supplied.
不能在花括号内使用美元符。必须将美元符换成感叹号。
案例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$cat test10.sh #!/bin/bash # Grabbing the last parameter # params=$# echo echo The last parameter is $params echo The last parameter is ${!#} echo # $ $ bash test10.sh 12345 The last parameter is 5 The last parameter is 5 $ $ bash test10.sh The last parameter is 0 The last parameter is test10.sh $
2.2 $* 和 $@
$* 和 $@ 变量可以用来轻松访问所有的参数。这两个变量都能够在单个变量中存储所有的命 令行参数
$* 变量会将这些参数视为一个整体,而不是多个个体
$@ 变量会将命令行上提供的所有参数当作同一字符串中的多个独立的单词
案例1:
1 2 3 4 5 6 7 8 9 10 11 12
$cat test11.sh #!/bin/bash # testing $* and $@ # echo echo"Using the \$* method: $*" echo echo"Using the \$@ method: $@" $ $ ./test11.sh rich barbara katie jessica Using the $* method: rich barbara katie jessica Using the $@ method: rich barbara katie jessica
$cat test13.sh #!/bin/bash # demonstrating the shift command echo count=1 while [ -n"$1" ] do echo"Parameter #$count = $1" count=$[ $count + 1 ] shift done $ $ ./test13.sh rich barbara katie jessica Parameter#1 = rich Parameter#2 = barbara Parameter#3 = katie Parameter#4 = jessica $
也可以一次性移动多个位置,只需要给shift命令提供一个参数,指明要移动的位 置数就行了
案例2:
1 2 3 4 5 6 7 8 9 10 11 12
$ cat test14.sh #!/bin/bash # demonstrating a multi-position shift # echo echo"The original parameters: $*" shift 2 echo"Here's the new first parameter: $1" $ $ ./test14.sh 1 2 3 4 5 The original parameters: 1 2 3 4 5 Here's the new first parameter: 3
$ cat test15.sh #!/bin/bash # extracting command line options as parameters # echo 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 $ $ ./test15.sh -a -b -c -d Found the -a option Found the -b option Found the -c option -d is not an option
$ ./test16.sh -c -a -b test1 test2 test3 Found the -c option Found the -a option Found the -b option test1 isnot an option test2 isnot an option test3 isnot an option $ ======================================== $ ./test16.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 $
$ cat test19.sh #!/bin/bash # simple demonstration of the getopts command # echo whilegetopts :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 $ $ ./test19.sh -ab test1 -c Found the -a option Found the -b option, with value test1 Found the -c option $
可以在参数值中包含空格。
可以将选项字母和参数值放在一起使用,而不用加空格。
getopts还能够将命令行上 找到的所有未定义的选项统一输出成问号
5. 选项标准化
6. 获得用户输入
read命令从标准输入(键盘)或另一个文件描述符中接受输入。
read命令包含了-p选项,允许你直接在read命令行指定提示符。
案例1:
1 2 3 4 5 6 7 8 9 10 11 12
$cat test22.sh #!/bin/bash # testing the read -p option # read -p"Please enter your age: " age days=$[ $age * 365 ] echo"That makes you over $days days old! " # $ $ ./test22.sh Please enter your age: 10 That makes you over 3650 days old!
$ cat test24.sh #!/bin/bash # Testing the REPLY Environment variable # read -p "Enter your name: " echo echo Hello $REPLY, welcome to my program. # $ $ ./test24.sh Enter your name: Christine Hello Christine, welcome to my program.
$ cat test25.sh #!/bin/bash # timing the data entry # ifread -t 5 -p "Please enter your name: " name then echo"Hello $name, welcome to my script" else echo echo"Sorry, too slow! " fi $ $ ./test25.sh Please enter your name: Rich Hello Rich, welcome to my script $ $ ./test25.sh Please enter your name: Sorry, too slow! $
$ cat test26.sh #!/bin/bash # getting just one character of input # read -n1 -p "Do you want to continue [Y/N]? " answer case$answerin Y | y) echo echo"fine, continue on…";; N | n) echo echo OK, goodbye exit;; esac echo"This is the end of the script" $ $ ./test26.sh Do you want to continue [Y/N]? Y fine, continue on… This is the end of the script $ $ ./test26.sh Do you want to continue [Y/N]? n OK, goodbye
-s 选项可以避免在read命令中输入的数据出现在显示器上,如:密码输入
案例5:
1 2 3 4 5 6 7 8 9 10 11
$ cat test27.sh #!/bin/bash # hiding input data from the monitor # read -s -p "Enter your password: " pass echo echo"Is your password really $pass? " $ $ ./test27.sh Enter your password: Is your password really T3st1ng?
$ cat test28.sh #!/bin/bash # reading data from afile # count=1 cat test | whilereadline do echo"Line $count: $line" count=$[ $count + 1] done echo"Finished processing the file" $ $ cat test The quick brown dog jumps over the lazy fox. This isa test, this isonlya test. O Romeo, Romeo! Wherefore art thou Romeo? $ $ ./test28.sh Line 1: The quick brown dog jumps over the lazy fox. Line 2: This isa test, this isonlya test. Line 3: O Romeo, Romeo! Wherefore art thou Romeo? Finished processing the file