LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-04-2013, 04:54 PM   #1
Batistuta_g_2000
Member
 
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.



GETOPTS-CASE-IN A-CASE
Code:
#!/bin/bash 

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



        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?.


                loglevel=*)
                    val=${OPTARG#*=}
                    opt=${OPTARG%=$val}
                    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?

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

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, 05:12 PM   #2
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora
Posts: 2,315

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
This looks like what you're looking for. http://rsalveti.wordpress.com/2007/0...-with-getopts/
 
  


Reply


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 03:15 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
Case Statement craigjward Programming 6 12-12-2007 11:33 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM
case statement baks Programming 2 03-15-2007 01:19 PM


All times are GMT -5. The time now is 01:39 AM.

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