Example of using getopt in Shellscript

Source: Internet
Author: User
The example of using getopt in Shellscript to use getopt has the following format options and corresponding parameters: www.2cto. comShell code #! /Bin/bashset -- 'getopt-qab: c & quot ;@ @ & quot; 'While [-n & quot; $1 & quot;] docase & quot ;... in Shell script, The getopt example can easily use the format option with the corresponding parameter. The example is www.2cto.com Shell code #! /Bin/bash set -- 'getopt-q AB: c "$ @" 'while [-n "$1"] do case "$1" in-) echo "option-a";-B) value = "$2" echo "option-B with para $ value" shift;-c) echo "option-c"; --) shift break; *) echo "$1 not option ";; esac shift done count = 1 for para in "$ @" do echo "# $ count = $ para" count = $ [$ count + 1] done. /test-AB test1-cd test2 test3 test4 result: option-a option-B with para 'test1' option-c #1 = 'Test2' #2 = 'test3' #3 = 'test4' getopt usage: getopt options optstring parameters. getopt is powerless for parameter types such as/test-AB test1-cd "test2 test3" test4. This requires getopts www.2cto.com Shell code #! /Bin/bash while getopts: AB: c opt do case $ opt in a) echo "-a option"; B) echo "-B option with value $ OPTARG ";; c) echo "-c option"; *) echo $ opt not a option; esac done OPTARG environment variable stores the parameters of corresponding options. /test-AB "hello, world"-cdefg result:-a option-B option with value hello, world-c option? Not a option? Not a option? Not a option? Not a option command parameter processing Shell code #! /Bin/bash while getopts: AB: cd opt do case $ opt in a) echo "-a option"; B) echo "-B option with value $ OPTARG ";; c) echo "-c option"; d) echo "-d option"; *) echo $ opt not a option ;; esac done count = 1 shift $ [$ OPTIND-1] for para in "$ @" do echo "# $ count = $ para" count = $ [$ count + 1] done. /test-abtest1-cd test2 test3 test4 result: www.2cto.com-a option-B option with value test1-c option-d option #1 = test2 #2 = test3 #3 = test4 OPTIND environment variable to store the current bit of the remaining parameter list of the getopts command value usage: getopts optstring variable

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.