getopts - optional and problem to display help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts - optional and problem to display help
# 1  
Old 04-07-2011
getopts - optional and problem to display help

In the below code

Code:
while getopts :rfw:d:s:a: options
do
    case "$options" in
        r) echo reverse;;
        f) echo forward;;
        w) window=$OPTARG;;
        d) duration=$OPTARG;;
        s) search=$OPTARG;;
        a) value=$OPTARG;;
        *) help; exit;;
    esac
done

here in case with r and f only one should be entered. how s the code need to be modified. and the other problem is in display help. even if i execute the above code inside the script it not goes in to help case.

please help me out.
# 2  
Old 04-07-2011
Do you have function 'help' defined at all?

Maybe you want something like this?
Code:
#!/bin/bash

help() {
    echo "I need some help"
}
rFlag=0;
fFlag=0;
while getopts :rfw:d:s:a: options
do
    case "$options" in
        r) [[ $(($rFlag + $fFlag)) -gt 0 ]] && echo "can't have both, baby" &&  exit 1 ; ((rFlag++)); echo reverse ;;
        f) [[ $(($rFlag + $fFlag)) -gt 0 ]] && echo "Don't be greedy" &&  exit 1 ; ((fFlag++)); echo forward ;;
        w) window=$OPTARG;;
        d) duration=$OPTARG;;
        s) search=$OPTARG;;
        a) value=$OPTARG;;
        *) help; exit;;
    esac
done
shift $((OPTIND-1))

echo "---------------"
echo window $window 
echo duration $duration 
echo search $search 
echo value $value

which can be called:
Code:
miro@miro-ntb:~$ ./test -f  -s someString -a aaaaaa
forward
---------------
window
duration
search someString
value aaaaaa
miro@miro-ntb:~$ ./test -ffff  -s someString -a aaaaaa
forward
Don't be greedy
miro@miro-ntb:~$ ./test -f -s someString -r -a aaaaaa
forward
can't have both, baby
miro@miro-ntb:~$ ./test -g -ffff  -s someString -a aaaaaa
I need some help

# 3  
Old 04-07-2011
Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 Replies

2. Shell Programming and Scripting

Getopts with optional parameters

Hi Unix Gurus, i am on learning path of unix, and yet to discover many things. I came across with this requirement where i need to pass parameters but the position of parameters is not fixed so after doing some google search got to know "getopts" can handle that. So here is my code: function... (3 Replies)
Discussion started by: gnnsprapa
3 Replies

3. Shell Programming and Scripting

Identify problem with while getopts

can anyone spot a problem with the below: $ $ cat getopts.sh #!/bin/sh usage() { echo "myscript.sh local /tmp data.txt 600s -query" 1>&2; exit 1; } while... (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

problem with getopts

Hi, I have written a script to take command line arguments using geopts.This is the code. #!/bin/sh # The usage of this script. usage="Usage is $0" usage="$usage " usage="$usage " usage="$usage " # Use the getopt utility to set up the command line flags. set -- `/usr/bin/getopt... (4 Replies)
Discussion started by: arijitsaha
4 Replies

5. Shell Programming and Scripting

getopts problem

Hi everyone I want to know how can we pass multiple argument in getopts suppose PARAMS="abcd" while getopts ${PARMS} FLAG do case ${FLAG} in (a) (b) (c) (d) esac (6 Replies)
Discussion started by: aishsimplesweet
6 Replies

6. Shell Programming and Scripting

getopts problem

How do I get the getopts command to display whats written at my help option if no option is types in? For example, myscript.sh -h will bring up my help option, however, I also want myscript.sh to do the same! #!/bin/bash while getopts :abh opt do case "$opt" in... (2 Replies)
Discussion started by: linuxkid
2 Replies

7. UNIX for Advanced & Expert Users

getopts problem

i was going through the man page of getopts this particular section is not clear to me can anyone please clarify in a little detail so that i can understand the concept MANPAGE:: Since getopts affects the current shell execution environ- ment, it is generally provided as a... (7 Replies)
Discussion started by: mobydick
7 Replies

8. Shell Programming and Scripting

problem with getopts

Hi, I am a new member to unix.com. Actually I am facing a problem with getopts. In my script i have used getopts to parse the parameters. when i use the script as shown below its working fine: find_status -p all ### where find_status is a script name. But even if I pass more than one... (3 Replies)
Discussion started by: pvamsikr
3 Replies

9. Shell Programming and Scripting

Problem in getopts

while getopts l:f:s:o:h: c do case $c in l) tail -${OPTARG} /etc/passwd exit 2;; f) head -${OPTARG} /etc/passwd exit 3;; s) grep ${OPTARG} /etc/passwd | cut -d: -f7 exit 4;; o) OARG=$OPTARG exit 5;; h) ... (3 Replies)
Discussion started by: nadman123
3 Replies

10. Shell Programming and Scripting

Problem with getopts

I need to parse parameters but the arguments could be NULL,example: > cat getopts.sh while getopts "a:b:" opt 2>/dev/null do case "${opt}" in a) echo "A:${OPTARG}" ;; b) echo "B:${OPTARG}" ;; *) exit 1 ;; esac done > getopts.sh -a TEST1 -b TEST2... (5 Replies)
Discussion started by: Klashxx
5 Replies
Login or Register to Ask a Question