每天写点shell--命令行参数

1、读取参数:位置参数变量是标准的数字: $0是程序名, $1是第一个参数, $2是第二个参数...

  1 #!/bin/bash
  2 # using one command line parameter
  3 
  4 factorial=1
  5 for (( number = 1; number <= $1; number++ ))
  6 do
  7    factorial=$[ $factorial * $number ]
  8 done
  9 echo The factorial of $1 is $factorial

执行:

# ./test1.sh 5
The factorial of 5 is 120

 

2、输入多个命令行选项,则在命令行上每个参数都必须用空格分开:

  1 #!/bin/bash
  2 # testing two command line parameters
  3 
  4 total=$[ $1 * $2 ]
  5 echo The first parameter is $1
  6 echo The second parameter is $2
  7 echo The total value is $total

 执行:

# ./test2.sh  3 4
The first parameter is 3
The second parameter is 4
The total value is 12

 

3、如果脚本需要多个9个命令行参数,在变量数字周围加花括号:

  1 #!/bin/bash
  2 # handling lots of parameters
  3 
  4 total=$[ ${10} * ${11} ]
  5 echo The tenth parameter is ${10}.
  6 echo The eleventh parameter is ${11}.
  7 echo The total is $total

执行:

# ./test4.sh 1 2 3 4 5 6 7 8 9 10 11
The tenth parameter is 10.
The eleventh parameter is 11.
The total is 110

 

4、测试参数

  1 #!/bin/bash
  2 # testing parameters before use
  3 
  4 if [ -n "$1" ]     # -n 参数测试 $1(第一个参数)是否为null 
  5 then
  6     echo Hello $1, glad to meet you.
  7 else
  8     echo "Sorry, you did not identify yourself"
  9 fi

执行:

# ./test7.sh
Sorry, you did not identify yourself

./test7.sh frank
Hello frank, glad to meet you.

 

5、特殊参数变量(参数计数 $#)

  1 #!/bin/bash
  2 # getting the number of parameters
  3 echo There were $# parameters supplied.

执行:

# ./test8.sh
There were 0 parameters supplied.

./test8.sh 1 2 3
There were 3 parameters supplied.

 

6、使用参数前测试参数的总数

  1 #!/bin/bash
  2 # testing parameters
  3 
  4 if [ $# -ne 2 ]
  5 then
  6     echo Usage: test a b
  7 else
  8     total=$[ $1 + $2 ] 
  9     echo The total is $total
 10 fi  

执行:

# ./test9.sh 1 2
The total is 3

# ./test9.sh
Usage: test a b

 

7、不需要知道有多少个参数个数,抓取最后一个参数 ${!#}

  1 #!/bin/bash
  2 params=$#
  3 echo The number of parameter is $params
  4 echo The last parameter is ${!#}                                   

执行:

# sh test10.sh 1 2 3 4 4
The number of parameter is 5  #参数的总个数
The
last parameter is 4 #最后一个参数的值

 

8、$*  $@变量提供了对所有参数的快速访问, $*变量会将命令行上提供的所有参数当作单个单词保存,$@变量会将命令行上提供的所有参数当做同一个字符串中多个独立的词。

  1 #!/bin/bash
  2 # testing $* and $@
  3 
  4 echo "Using the \$* method: $*"
  5 echo "Using the \$@ method: $@"
                                 

执行:

# ./test11 rich katie jessica
Using the $* method: rich katie jessica
Using the $@ method: rich katie jessica

 

差异:

  1 #!/bin/bash
  2 # testing $* and $@
  3 count=1              #赋值的时候注意不要有空格
  4 for param in "$*"    #将所有参数变为一个参数
  5 do
  6     echo "\$* parameter #$count = $param"
  7     count=$[ $count + 1 ]
  8 done
  9 
 10 count=1
 11 for param in "$@"   #各个参数独立
 12 do
 13     echo "\$@ parameter #$count = $param"
 14     count=$[ $count + 1 ]
 15 done
~            

执行:

# ./test12.sh rich barbara katie jessica
$* parameter #1 = rich barbara katie jessica
$@ parameter #1 = rich
$@ parameter #2 = barbara
$@ parameter #3 = katie
$@ parameter #4 = jessica

可见, $*会将所有的参数当成单个单数,而$@变量会单独处理每个参数。

 

9、shift的用法,遍历命令行参数

  1 #!/bin/bash
  2 #demonstrating the shift command
  3 
  4 count=1
  5 while [ -n "$1" ]    #注意要加冒号
  6 do
  7   echo "parameter #$count = $1"   
  8   count=$[ $count + 1 ]
  9   shift                   #参数变量逐次减一
 10 done
~            

执行:

# ./test13.sh a b c d e
parameter #1 = a
parameter #2 = b
parameter #3 = c
parameter #4 = d
parameter #5 = e

 

10、shift命令提供一个参数执行多位移动

  1 #!/bin/bash
  2 # demonstrating a multi-position shift
  3 
  4 echo "The original parameters: $*"
  5 shift 2
  6 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

 

posted @ 2016-12-06 11:23  子非木  阅读(2838)  评论(0编辑  收藏  举报