In bash shell script programming, how can I correctly obtain "script option Parameters" and "Script Parameters?

Source: Internet
Author: User

Some linnux commands have very powerful functions, mainly because they support many Command Options. For example, the [IP] command can be used to configure and manage IP addresses and route entries. This command can complete all the functions implemented by the [ifconfig] and [Route] commands. A function is a separate function module. If a function can receive option parameters, the functions of the function become rich and flexible. The same is true for scripts.

What is the command format in Linux?

[[Email protected] ~] # Command [optons parameter1 | parameter2]... parameter3 parameter3... Command Option option parameter command parameter (1) command parameter (2)


The script writing standard is similar to the Command Format in Linux. If our script supports option parameters, the format is as follows:

Scriptname {-option | -- Option parameter}... scriptparameter... Script Name short option long option parameter script Parameter

Description:

Option uses the parallel bars (-) or parallel bars (--) to indicate options. Parameter option parameter scriptparameter script Parameter


These options, option parameters, and Script Parameters are collectively referred to as positional parameter. Shell uses special variables to record them. Therefore, you can use these variables in scripts or functions to reference the parameters of scripts or functions. Since it is called a location parameter, there should be a forward and backward order.So how does bash shell identify parameters in different locations?

$1 this variable records 1st parameters $2 this variable records 2nd parameters $3 this variable records 3rd parameters ....... $9 this variable records 9th parameters $ {10} This variable records 10th parameters ...... $0 when running the script in the command line: if you use the absolute path to run the script, the absolute path is used to record the Script Name. If you use a relative path to run the script, the relative path is used to record the Script Name. You can use the [basename $0] method to remove the script path. Only get the script name. $ # Number of location parameters $ * Get all parameters and treat these parameters as a word. For iteration is not allowed. [Email protected] also obtains all parameters. Treat it as a string. This string contains multiple words separated by space. In this way, you can use for to iterate each word in the string (location parameter.

So how do we obtain the option parameter in a script or function?

Analysis:When writing scripts or functions, if option parameters are supported, follow the preceding format. Option option has one feature: both use the parallel bars (-) or parallel bars (--) to indicate that this is the option of the script or function. It is followed by the option parameter. How is this achieved? There are two implementation methods:

1. Use the if statement.

If [["$1" = ~ ^-]; Then $2 is the fi parameter of option $1.


2. Select a structure: Case

Case $1 in-A | -- add) $2 is the parameter of option $1; esac


This method of obtaining the option parameters of a script or function has too many limitations. It is useful for obtaining scripts or functions with only one option. Scripts/functions with multiple option parameters are powerless.

Although multiple if statements can be used to obtain the parameters of multiple options. Every option should be treated as a branch statement of the IF statement, which is cumbersome.


If multiple options are used in our scripts or functions. How can we obtain the parameters of the options?

BASH Shell provides the [shift] command to help operate command line parameters. This command can change the relative position of command line parameters. Shift shifts each parameter variable to the left by default.

For example, if two parameters are passed to the script or function, each time the shift command is executed, the value of variable $2 is moved to group variable $1, and the value of variable $1 is discarded.

Shift command usage

Shift: Shift [N. The default value is 1.

We can use the [shift] command and the SELECT statement case to conveniently obtain the parameters of each option, which is also very simple. Example:

Mkscript. Sh [-d | -- Description "script description"] [-A | -- Author "script author"]/pa/File

Case $1 in-d | -- Description) the parameter of this option is $2 shift 2;-A | -- author) the parameter of this option is $2 shift 2; *) echo "Usage: how to use a script or function" esac

For the above extraction, there is a problem with the option Parameter Method of the script/function:

Whether the parameters passed during script mkscript execution are correct or not, "Echo" Usage: How to Use the script or function "is displayed.

Ideally, if you use this script with options, if "options" are not the "options" listed in the case statement, you will be notified of the method to use this script or function. The script parameters/pa/file can be obtained correctly.

Analysis:

To execute the case selection statement, you must have one condition: if the "parameter" is "option", the case statement is executed. In the script, the "option" flag is: horizontal bar (-) or parallel bar (--). Then, we can determine whether the case selection statement is executed based on this flag.

Because we have many script options, we need to use a loop to determine whether it is an "option" as a condition for loop. Modify the options and methods for extracting script parameters to the following:

While ["$1 "! = "$ {1 # [--,-]}"]; do case $1 in-d | -- Description) the parameter of this option is $2 shift 2 ;; -A | -- author) the parameter of this option is $2 shift 2; *) the script does not support this option even though it is used, match All * here, the user is prompted: Shift 2 is used for the script; esacdone script parameter = $1


This article is from the "Linux" blog, please be sure to keep this source http://9528du.blog.51cto.com/8979089/1529634

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.