Write point shell--command line arguments every day

Source: Internet
Author: User

1, reading parameters: Positional parameter variable is the standard number: $ $ is the program name, "$" is the first parameter, and the second argument is ...

 1  #!/bin/bash  2   # using one command line parameter   3  4  factorial=1  5  for  (number = 1 ; <= $1 ; Number++ 6   do  7  factorial=$[$factorial * $number]  8  done  9  Span style= "COLOR: #0000ff" >echo  the factorial of $1  is $factorial 

Perform:

#./test1. SH 5  5

2. To enter multiple command-line options, each parameter must be separated by a space on the command line:

  1 #!/bin/bash  2  # Testing command line  parameters3    4 total=$[$1 * $2  ]  5echo The first parameter is $ 1  6 Echo The second parameter is $2  7echo The total value is $total

Perform:

#./test2. SH  3 4  3 4

3. If the script requires more than 9 command-line arguments, add curly braces around the variable numbers:

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

Perform:

#./test4. SH 1 2 3 4 5 6 7 8 9 Ten  One    ten

4. Test parameters

  1#!/bin/Bash2# Testing parameters before use3   4 if[-N" $"] #-N parameter Tests if $ (first argument) is null5  Then  6     EchoHello $1, glad to meet. 7 Else  8     Echo "Sorry, You do not identify yourself"  9 fi

Perform:

#./test7. SH Sorry, you do not identify yourself. /test7. SH Frankhello Frank, glad to meet.

5. Special parameter variable (parameter Count $#)

  1 #!/bin/bash  2  #  gettingthe number of parameters3Echo There were $# parameters supplied.

Perform:

#./test8. SH  0 parameters supplied.


There were 3 parameters supplied.

6, the total number of test parameters before using parameters

  1#!/bin/Bash2# Testing Parameters3   4 if[$#-ne2 ]  5  Then  6     EchoUsage:test a B7 Else  8Total=$[$1+ $2 ]   9     EchoThe total is $totalTen fi  

Perform:

#./test9. SH 1 2  3

#./test9.sh
Usage:test a B

7, do not need to know how many parameters, grab the last parameter ${!#}

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

Perform:

sh test10. SH 1 2 3 4 4  5  #参数的总个数
Last 4 #最后一个参数的值

8. $* [EMAIL protected] variable provides quick access to all parameters, $* variable saves all arguments supplied on the command line as a single word, and the [email protected] variable treats all arguments supplied on the command line as multiple independent words in the same string.

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

Perform:

#./test11 Rich Katie jessicausing the $* Method:rich Katie jessicausing The [email protected] Method:rich Katie Jessica

Difference:

  1#!/bin/Bash2# testing $*and [email protected]3Count=1 #赋值的时候注意不要有空格  4  forParaminch "$*"  5  Do  6     Echo "\$* parameter # $count = $param"  7count=$[$count +1 ]  8  Done  9  TenCount=1  One  forParaminch "[email protected]"  A  Do  -     Echo "\[email protected] parameter # $count = $param"  -count=$[$count +1 ]  the  Done~

Perform:

#./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 # C8>3 = katie[email protected] parameter #4 = Jessica

As you can see, $* will treat all parameters as a single singular, while the [email protected] variable will handle each parameter separately.

Write point shell--command line arguments every day

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.