Linuxshell Programming Guide-passing parameters to scripts

Source: Internet
Author: User
We have discussed how to use a specific variable $1 $9 to pass parameters to the script. $ Is used to count the number of passed parameters. You can create a usage statement to notify you how to call a script or function with appropriate parameters when necessary. To put it simply, the following script framework controls

We have discussed how to use a specific variable $1 and $9 to pass parameters to the script. $ # Used to count the number of passed parameters. You can create a usage statement to notify you how to call a script or function with appropriate parameters when necessary.

To put it simply, the following script framework control parameters start and stop. The script requires two parameters. if no two parameters are input, a usage statement is generated. Note that the case statement is used to process different parameters of the input script.
[Root @ localhost huangcd] # cat opt
#! /Bin/bash
Usage ()
{
Echo "usage: 'basename $ 0' start | stop process name"
}
OPT = $1
PROCESSID = $2
If [$ #-ne 2]
Then
Usage
Exit1
Fi
Case $ OPT in
Start | Start)
Echo "Starting... $ PROCESSID"
;;
Stop | Stop)
Echo "Stoping... $ PROCESSID"
;;
*) Usage
;;
Esac
[Root @ localhost huangcd] # opt start named
Starting... named

Any UNIX or LINUX commands are in the normal format:

Command option file

The option part can contain up to two different values. In the above script, if you must control different command options, you need to add a large number of scripts. Here, only two options are controlled: start and stop.

Fortunately, shell provides the shift command to help with the offset option. using shift can remove the restriction of passing parameters only from $1 to $9.

When passing parameters to a script, you sometimes need to offset each parameter to process options. this is the function of the shift command. It offsets the parameter position to the left at a time. The following describes the function using a simple script. The script uses the while loop to feedback all parameters passed to the script.
[Root @ localhost huangcd] # opt2 huang cheng du
Huang
0
Cheng
0
Du
0
[Root @ localhost huangcd] # cat opt2
#! /Bin/bash
Loop = 0
While [$ #-ne 0]
Do
Echo $1
Echo $ loop
Shift
Done

Although the eval command has not yet been mentioned, if you need to know the last parameter entered in the command line (usually a file name), you can have two options: use the command eval echo \ $ #; use the shift command: shift 'expr $#-2 '.
[Root @ localhost huangcd] # opt2 huang cheng du
Huang
Du
Cheng
Du
Du
Du
[Root @ localhost huangcd] # cat opt2
#! /Bin/bash
Loop = 0
While [$ #-ne 0]
Do
Echo $1
Eval echo \ $ #
Shift
Done

Getopts can write scripts to make it easier to control multiple command line parameters. Getopts is used to form the standard form of command line processing. In principle, the script should be able to validate the standard format of command files with multiple options.

The example can better understand getopts. The getopts script below accepts the following options or parameters.

A sets ALL to true.

H. set the HELP variable to true.

F sets the variable FILE to true.

V sets VERBOSE to true.

For all variable settings, it is generally assumed that its initial state is false:

The general format of getopts is:

Getopts option_string variable

In the above example, use the script:

While getopts ahfgv OPTION

The while loop is used to read the command line. optionstring is the specified five options (a, h, f, g, v), and variable is the OPTION in the script. Note that each option is not specified with a hyphen.

Getopts reads optionstring and learns that valid options are used in the script.

Getopts checks all parameters starting with a hyphen and regards them as an OPTION. If an OPTION is entered, it is compared with optionstring. if a match is found, the variable is set to OPTION, if no matching characters are found, the variable can be set? Repeat this process until the option input is complete.

After receiving all the parameters, getopts returns a non-zero state, that is, the parameter is successfully transferred. the variable OPTION stores the final processing parameter, and the benefits of doing so will be seen in a moment.
[Root @ localhost huangcd] # getopt1-a-h-p
ALL is true
HLEP is true
/Home/huangcd/getopt1: illegal option-p
[Root @ localhost huangcd] # getopt1-a-g
ALL is true
[Root @ localhost huangcd] # cat getopt1
#! /Bin/bash
ALL = false
HELP = false
FILE = false
VERBOSE = false
While getopts ahfgv OPTION
Do
Case $ OPTION in
A) ALL = true
Echo "ALL is $ ALL"
;;
H) HELP = true
Echo "HLEP is $ HELP"
;;
F) FILE = true
Echo "FILE is $ FILE"
;;
V) VERBOSE = true
Echo "VERBOSE is $ VERBOSE"
;;
Esac
Done

It is sometimes necessary to specify the value of the command line option in the script. Getopts provides a way to put a colon after the option in optionstring. For example:
Getopts ahfvc: OPTION

The preceding script indicates that options a, h, f, and v can be passed without adding actual values, while Option c must be set. When using the option value, you must use the variable OPTRG to save the value. If you try not to pass this option, an error message is returned. The error message is not clear, so you can use your feedback to shield it. the method is as follows:

Place the colon at the beginning of optionstring.
While getopts: ahfgvc: OPTION

In the case statement? Create a statement to capture errors.
[Root @ localhost huangcd] # cat getopt1
#! /Bin/bash
ALL = false
HELP = false
FILE = false
VERBOSE = false
While getopts ahfgv OPTION
Do
Case $ OPTION in
A) ALL = true
Echo "ALL is $ ALL"
;;
H) HELP = true
Echo "HLEP is $ HELP"
;;
F) FILE = true
Echo "FILE is $ FILE"
;;
V) VERBOSE = true
Echo "VERBOSE is $ VERBOSE"
;;
Esac
Done
[Root @ localhost huangcd] # vim getopt1
[Root @ localhost huangcd] # cat getopt1
#! /Bin/bash
ALL = false
HELP = false
FILE = false
VERBOSE = false
COPIES = 0
While getopts: ahfgvc: OPTION
Do
Case $ OPTION in
A) ALL = true
Echo "ALL is $ ALL"
;;
H) HELP = true
Echo "HLEP is $ HELP"
;;
F) FILE = true
Echo "FILE is $ FILE"
;;
V) VERBOSE = true
Echo "VERBOSE is $ VERBOSE"
;;
C) COPIES = $ OPTARG
Echo "COPIES is $ COPIES"
;;
\?)
Echo "'basename $ 0'-[ahfv]-[c value] file"> & 2
;;
Esac
Done

Run the above script. If Option c is not assigned a value, an error is returned, but the feedback in the script statement is displayed:
[Root @ localhost huangcd] # getopt1-ah-c 3
ALL is true
HLEP is true
COPIES is 3
[Root @ localhost huangcd] # getopt1-ah-c
ALL is true
HLEP is true
[Root @ localhost huangcd] # getopt1-aho
ALL is true
HLEP is true
Getopt1-[a h f v]-[c value] file

When you specify the command line option in a script, it is best to make the naming rule consistent with UNIX or LINUX. The following is a list of options and their meanings.

Option description
A extension
C count, copy
D Directory, device
E execution
F file name, force
H help
I ignore status
L register a file
O complete output
Q exit
P path
-V display mode or version

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.