A summary of the method of shell script passing parameters _linux Shell

Source: Internet
Author: User
Tags svn

First, receive the fixed length of the parameter

Copy Code code as follows:

[Root@svn shell_example]# Cat params.sh
#!/bin/bash
#传参测试脚本
echo "My name is ' basename $ ' I ' called as $"
echo "My I-parameter is: $"
echo "My second parameter are: $"

Null parameter execution
Copy Code code as follows:

[ROOT@SVN shell_example]# SH params.sh
My name is Params.sh-i was called as params.sh
My-parameter is:
My second parameter are:

Pass 2 parameter execution
Copy Code code as follows:

[ROOT@SVN shell_example]# sh params.sh One Two
My name is Params.sh-i was called as params.sh
My i-Parameter is:one
My second parameter is:two

second, what if there are parameters to do? Do you want to add each one? The answer is No.

The following usage should be familiar, that is, directly executing the script itself without any arguments, then the script throws out the help information. That's how to use this script. See the Scarlet Letter Section

Copy Code code as follows:

[ROOT@SVN shell_example]# SH params_v2.sh
My name is Params_v2.sh-i was called as params_v2.sh
I was called with 0 parameters.
Usage:params_v2.sh-Second
You provided 0 Parameters,but 2 are required.

The code is as follows
Copy Code code as follows:

[Root@svn shell_example]# Cat params_v2.sh
#!/bin/bash
# This is a test example of a test script argument

echo "My name is ' basename $ ' I ' called as $"
echo "I am called with $# parameters."

If ["$#"-eq "2"];then
echo "My-parameter is $"
echo "My second parameter are $"
Else
echo "Usage: ' basename $ ' second '
echo "You provided $# Parameters,but 2 are required."
Fi

The detailed execution process is as follows
Do not pass parameter execution

Copy Code code as follows:

[ROOT@SVN shell_example]# SH params_v2.sh
My name is Params_v2.sh-i was called as params_v2.sh
I was called with 0 parameters.
Usage:params_v2.sh-Second
You provided 0 Parameters,but 2 are required.

Pass 3 parameter execution
Copy Code code as follows:

[ROOT@SVN shell_example]# SH params_v2.sh One Two three
My name is Params_v2.sh-i was called as params_v2.sh
I was called with 3 parameters.
Usage:params_v2.sh-Second
You provided 3 Parameters,but 2 are required.

Pass 2 parameter execution
Copy Code code as follows:

[ROOT@SVN shell_example]# sh params_v2.sh One Two
My name is Params_v2.sh-i was called as params_v2.sh
I was called with 2 parameters.
My I-parameter is one
My second parameter is two

The question comes, if later still need to add the parameter how to do? Or I'm not sure I'm going to pass a few parameters.
The solution is as follows, detailed implementation results are as follows

Copy Code code as follows:

[Root@svn shell_example]# Cat manyparams.sh
#!/bin/bash
#这是个测试脚本传N个参数的例子

echo "My name is ' basename $ '--I was calling from $
echo "I have $# parameters."

Count=1
While ["$#"-ge "1"];d o
echo "parameter number is $count is $"
Let Count=count+1
Shift
Done

A parameter executes

[ROOT@SVN shell_example]# sh manyparams.sh One

My name is manyparams.sh-I was calling from manyparams.sh
I have 1 parameters.
The argument number 1 is one

5 parameter execution

Copy Code code as follows:

[ROOT@SVN shell_example]# SH manyparams.sh One two three four five

My name is manyparams.sh-I'm calling from manyparams.sh
I have 5 parameters
Argument number 1 is one
parameter number is 2 is two
parameter ordinal is 3 is three
parameter ordinal is 4 is four
The argument number 5 is five

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.