Passing Shell script parameters in CentOS

Source: Internet
Author: User

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.$0Indicates the name of the script to be executed. 2.$1,$2,$3Parameters representing the order (first, second, and third) 3.$@Representative$1,$2,$3Meaning, 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 executionfuncThe value of the latter is from execution.echo '"$$" : ' "$$")

2. shell script command line parameter 2.1. getoptsIntroduction

getoptsIs a shell built-in command that can be used to parse command line parameters.getoptsThe bash Syntax of is as follows:

getopts optstring name [args]

OptstringoptstringContains the option string to be recognized. 1. IfoptstringIt starts with a colon, meaningslient errorMode (getoptsItself, for example, the error of missing parameters or illegal options;slientThe error message is not printed, but the error message is outputOPTARGMedium; nonslientThe 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 getoptsIn 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 exampleccParameters, soccThe default value specified in the script is obtained.0

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.