Comparison of 3 methods of shell script passing parameters _linux Shell

Source: Internet
Author: User
#!/bin/bash #extracting command text_text_text_line options as Parameters Help_info () {echo "NAME" echo "\t$0" Ech O "Synopsis" echo "\t$0 is a shell test about process Options" echo "DESCRIPTION" echo "\toption like-a-B param1- C param2-d "} If [$#-lt 0] then Help_info fi nomal_opts_act () {echo-e" \n### nomal_opts_act ###\n "while [
    -N "$" Do case "In-a" echo "Found the-a option";;
    -B) echo "Found the-b option" echo "The parameter follow-b is $" shift;
    -c) echo "Found the-c option" echo "The parameter follow-c is $" shift;
     -D) echo "Found the-d option";;
  *) echo "is not a option";; ESAC shift Done} #用shell命令自建的选项解析, can follow their own ideas to achieve #优点: their own customization, not to do, only unexpected #缺点: Trouble Getopt_act () {ECHO-E "\n### Getopt_ac
     T ###\n "getoptout= ' getopt ab:c:d" $@ "' Set--$GETOPTOUT while [-N ' $] do case $ in-a) echo "Found the-a option";;
    -B) echo "Found the-b option" echo "The parameter follow-b is" $ "" Shift ";
    -c) echo "Found the-c option" echo "The parameter follow-c is" $ "" Shift ";
    -D) echo "Found the-d option";;
     --) shift break;;
  *) echo "unknow option:" "$"; ESAC shift did param_index=1 for Param in ' $@ do echo ' Parameter $param _index: $param ' param_index=$[ $param _index + 1] done} #用getopt命令解析选项和参数 #优点: Compared with getopts is a semi-automatic parsing, automatic organization options and parameters, with--symbols to separate options and parameters #缺点: relative to the shortcomings of the getopts #1. Need to Set--command fit, not required, requires manual shift #2. Spaces such as-a-b dog-c "Earth Moon"-d-f param1 param2 () {getopts_act) are parsed in option arguments echo-e
      "\n### getopts_act ###\n" while Getopts:ab:c:d ARGS does case $ARGS in a) echo "Found the-a option"
    ;;
    b) echo "Found the-b option" echo "The parameter follow-b is $OPTARG";; c) Echo "Found the-c option" echo "The parameter follow-c is $OPTARG";;
     D) echo "Found the-d option";;
  *) echo "unknow option: $ARGS";;
    Esac done Shift $[$OPTIND-1] param_index=1 to param in ' $@ ' do echo ' Parameter $param _index: $param ' param_index=$[$param _index + 1] done} #getopts command resolution options and Parameters #优点: You can include spaces in parameters such as:-C "Earth Moon" # option letters and parameter values can have no spaces such as:

-bdog # can bind undefined options to? output # Unknow option:?  Nomal_opts_act-a-B dog-c earth-d-F param1 param2 getopts_act-a-B dog-c "Earth Moon"-d-f param1 param2 getopt_act -a-b dog-c earth-d-F param1 param2
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.