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

Posted 子非木

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每天写点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、$*  [email protected]变量提供了对所有参数的快速访问, $*变量会将命令行上提供的所有参数当作单个单词保存,[email protected]变量会将命令行上提供的所有参数当做同一个字符串中多个独立的词。

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

执行:

# ./test11 rich katie jessica
Using the $* method: rich katie jessica
Using the [email protected] method: rich katie jessica

 

差异:

  1 #!/bin/bash
  2 # testing $* and [email protected]
  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 "[email protected]"
 12 do
 13     echo "\[email protected] parameter #$count = $param"
 14     count=$[ $count + 1 ]
 15 done
~            

执行:

# ./test12.sh rich barbara katie jessica
$* parameter #1 = rich barbara katie jessica
[email protected] parameter #1 = rich
[email protected] parameter #2 = barbara
[email protected] parameter #3 = katie
[email protected] parameter #4 = jessica

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

 









以上是关于每天写点shell--命令行参数的主要内容,如果未能解决你的问题,请参考以下文章

每天一个linux命令:cp命令

每天一个linux命令-cp

每天一个Linux命令(08)--cp命令

每天一个 Linux 命令:cp 命令

(转)每天一个linux命令:cp 命令

Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数