每天寫點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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM