Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-04-2013, 05:54 PM   #1
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
GETOPTS in CASE statement

Hi there I am trying to figure out how I would do this:

I need to write a script which takes in user options using Getopts but also as a Case within a Case. I came across this but can't really understand exactly what it is doing, have tried to explain in the # notes - but need someone to tell me if is right.


# selection options ":" "-h" "v" "-" + $OPTARGUMENT
# while getopts "$optspec set above optchar (case)
while getopts "$optspec" optchar; do
# outer case 
    case "${optchar}" in
          case "${OPTARG}" in

        val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
                    echo "Parsing option: '--${OPTARG}', value: '${val}'" >&2;
# OPTIND is an index to the next argument to be 
# processed, so this next line increases value by 1
# but what is val="${!OPTIND}"; doing?
# is it setting val to be "not OPTIND" so it is then
# written to STDERR?.

                    echo "Parsing option: '--${opt}', value: '${val}'" >&2

# whats going on here? so argument 1 and argument 2 # are "-" and "loglevel=*" respectively and 
# val is now "$OPTARG=" ...why use "#", "*" and "="?
# then echo writes to STDERR
# echo "Parsing option: --

                    if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
          echo "Unknown option --${OPTARG}" >&2
# What is the ":0:1" for? and why not equals to ":" 
# what does this mean?

            echo "Parsing option: '-${optchar}'" >&2
            if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
                echo "Non-option argument:
 '-${OPTARG}'" >&2
# Again what does this mean?
            echo "usage: $0 [-v] [--loglevel[=]<value>]" >&2
            exit 2
# Why exit 2 here why not 1 for error?

Any help appreciated - new to case and getopts is confusing!!!

Also how can the order be given in any way with getopts? as in - -h -v -* or -v -* -h ?
Old 03-04-2013, 06:12 PM   #2
Senior Member
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 3,117

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
This looks like what you're looking for.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
CASE statement oracledb0101 Linux - Newbie 3 06-13-2012 04:15 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 05:50 AM
Case Statement craigjward Programming 6 12-13-2007 12:33 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 09:05 PM
case statement baks Programming 2 03-15-2007 02:19 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration