Process command line parameters in shell scripts

Source: Internet
Author: User
Tags svn update

Command line parameters when running the script, as shown in figure

./myscript.sh -a -b arg

In scripts, case statements are usually used for processing. My personal favorite practice: first try to use long parameter names (short parameter names are usually not significant, after all, your scripts are not so popular), such

./myscript --with-ssl=yes --install-dir=../export

In the script, use the following function to process parameters:

######################################## ######################################## # Analyze command lines, if there are options in the command line (as the function's 1st real parameters): # -- XXX-X-x = vvv #, this function executes _ xxx_x_x = vvv, that is, it is equivalent to assigning vvv to the _ xxx_x_x variable # If the option in the command line does not contain the '=, then, this function inserts the command line parameters into the variables represented by 2nd real parameters for later processing #################### ######################################## ################### function parseoption () {if [$ #-GT 0]; then if ["$ {:2 2}" = "--"]; then local key =$ {1% % = *} if [ "$ {Key }"! = "$ {1}"]; then local value =$ {1 # * =} key =$ {key //-/_} eval $ {key }=$ {value} return 0 Elif [-n "$ {key} "] & [-n" $ {2} "]; then eval $ {2} + = \ "$ {1} \" Return 0 else return 1 fi Elif [-n "$ {2}"]; then eval $ {2} + = \ "$ {1} \" Return 0 else return 1 fi else return 1 fi} arguments = for option in $ * Do parseoption $ {Option} argumentsdone

Arguments can be used for case processing later, but this processing method is very scalable for value-assigned command line parameters.

For example, if we want to deal with SVN, we have the following statement:

svn update

However, when I run this script on another machine, I found that its SVN is not installed in the system path and needs to be referenced in the full path. I just need to modify this line of code

${__svn:-svn} update

Without adding any redundant code, the script can specify the svn path through command line parameters, that is

./myscript --svn=/opt/subversion/bin/svn ...

It is quite convenient. In this way, it is no longer difficult to add command line parameters. The script only needs to correctly use variables.

In addition, some "dynamically named" parameters are hard to be processed using case, such

./build-tool ... --platforms=st5197,st7105 --st5197-config=st5197_setenv.sh --st7105-config=st7105_setenv.sh

The preceding commands mean that st5197 and st7105 are specified on our platform. At the same time, both platforms have preprocessing scripts, before we officially start our work, we should execute it (for example, setting some environment variables ). The number of these scripts is not fixed (corresponding to the number of platforms specified by -- platforms), that is, -- platforms specifies four platforms, there may be four -- {platform}-config (or fewer than four, indicating that the corresponding platform does not require preprocessing scripts). At the same time, the specified parameters must be associated with the corresponding platform, which is why we associate the options with the Platform (that is, these options are not pre-defined ).
In shell scripts, you can handle the following parameters:

For platform in $ {__ platforms} Do # If the -- {platform}-Config = file parameter is specified in the command line () the __{ platform} _ config variable has been assigned a value, convert the value to the platform_config variable to reference eval platform_config = '$' __$ {platform} _ config if [-n "$ {platform_config}"]; then... fidone

You can obtain and process the name of the config file corresponding to the platform.

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.