Passing Shell script parameters in CentOS
1. Default variables of shell scripts
The following uses a shell script to describeDefault shell script variables
[ouyangyewei@localhost workspace]$ cat learn_argument.sh #! /bin/bash## check arguments##-----------------if [ $# -lt 3 ]then echo "Error! Not Enough Arguments." echo "$0 $@" exit 1fi#-----------------func() { return 1;}funcecho '"$?" : ' "$?" # result from funcecho '"$0" : ' "$0"echo '"$1" : ' "$1"echo '"$2" : ' "$2"echo '"$3" : ' "$3"echo '"$@" : ' "$@"echo '"$#" : ' "$#"echo '"$$" : ' "$$"echo '"$?" : ' "$?" # result from last one : "$$"
The execution result is as follows:
[ouyangyewei@localhost workspace]$ sh learn_argument.sh ouyangyewei yihaodian 23"$?" : 1"$0" : learn_argument.sh"$1" : ouyangyewei"$2" : yihaodian"$3" : 23"$@" : ouyangyewei yihaodian 23"$#" : 3"$$" : 3208"$?" : 0
Compare the above script with the execution result, you can better understand the following:Default shell script variables
: 1.$0
Indicates the name of the script to be executed. 2.$1
,$2
,$3
Parameters representing the order (first, second, and third) 3.$@
Representative$1
,$2
,$3
Meaning, each variable is independent (enclosed in double quotation marks) 4.$#
Represents the number of parameters 5.$$
ID of the running process of the current script (PID) 6.$?
Indicates the execution result of the previous command (such as the above script, carefully compare the two$?
, The former value comes from the executionfunc
The value of the latter is from execution.echo '"$$" : ' "$$"
)
2. shell script command line parameter 2.1.
getopts
Introduction
getopts
Is a shell built-in command that can be used to parse command line parameters.getopts
The bash Syntax of is as follows:
getopts optstring name [args]
Optstringoptstring
Contains the option string to be recognized. 1. Ifoptstring
It starts with a colon, meaningslient error
Mode (getopts
Itself, for example, the error of missing parameters or illegal options;slient
The error message is not printed, but the error message is outputOPTARG
Medium; nonslient
The error message is printed to the error output.) 2. if an option character ends with a colon, it indicates that the option represented by this character needs to receive parameters; 3. if an option character does not end with a colon, this character indicates that the option does not receive parameters. The options and parameters are separated by spaces;
OPTARG indicates the parameter value of the current option
For more detailed usage, referman getopts
In the following examplegetopts
.
2.2. Case Analysis
The email warning function includes the sender, recipient, CC, subject, and content of the email. The CC is optional, that is, when the script is executed, if the CC is not specified, the email will not be copied to anyone.
The following shell script implements the mail warning function:
[Ouyangyewei @ localhost workspace] $ cat email_alert.sh #! /Bin/bash #################################### ########### File: check_path # Description: Verify the given path. # If yes, 1 is returned; # Otherwise, 0 is returned, and email notification # Usage: sh check_path \ #-p path_need_to_check \ #-f from @ email \ #-t to @ email \ # [-c cc @ email \] # author: ouyangyewei@yhd.com # date: 2015-10-26 ####################################### ######### check input parameters ## ----------------------------------------------- if [$ #-lt 3] then echo "Error! Not Enough Params. "echo" Usage: sh check_path-p path_need_to_check-f from @ email-t to @ email [-c cc @ email] "exit 1; fi # VARIABLES ## define variables ## ----------------------------------------------- path = 0; # path need to checkfrom = 0; # email senderto = 0; # email receivercc = 0; # email carbon copy #---------------------------------------------#------------------------------------ --------- ## Resolution input parameter ## --------------------------------------------- # optstring is "p: f: t: c:". The string optstring does not start with a colon. # It indicates that the slient error mode is disabled, if an error is reported when getopts is executed, # (for example, an invalid option or incorrect parameter), an error message is printed to the error. # In addition, p, f, t, and c are all options. Each option is followed by a colon. # It indicates that all four options need to receive parameters. # OPTARG record the parameter values of the current option. # While getopts "p: f: t: c:" optdo case $ opt in p) # set option "p" path = $ OPTARG; f) # set option "f" from = $ OPTARG; t) # set option "t" to = $ OPTARG; c) # set option "c" cc = $ OPTARG ;; *) echo "-$ opt not recognized"; esacdoneecho "path: $ path" echo "from: $ from" echo "to: $ to" echo "cc: $ cc"
The execution result is as follows:
[ouyangyewei@localhost workspace]$ sh email_alert.sh Error! Not Enough Params.Usage : sh check_path -p path_need_to_check -f from@email -t to@email [-c cc@email][ouyangyewei@localhost workspace]$ sh email_alert.sh \> -p /user/hive/warehouse/pms.db/pms_tp_config \> -f alert@yhd.com.cn \> -t ouyangyewei@foxmail.compath : /user/hive/warehouse/pms.db/pms_tp_configfrom : alert@yhd.com.cnto : ouyangyewei@foxmail.comcc : 0
In the preceding examplecc
Parameters, socc
The default value specified in the script is obtained.0