ksh: can you use getopts in a function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh: can you use getopts in a function?
# 1  
Old 09-22-2006
ksh: can you use getopts in a function?

I wrote a script that uses getopts and it works fine. However, I would like to put the function in that script in a startup file (.kshrc or .profile). I took the "main" portion of the script and made it a function in the startup script. I source the startup script but it doesn't seem to parse through the getopts case.

So my question is if using getopts in a function is even valid?

Thanks.
# 2  
Old 09-22-2006
Specify the arguments of the script to the function :
Code:
# Call the function to parse arguments with getopts
my_function "${@}"

Jean-Pierre.
# 3  
Old 09-27-2006
See if anybody can figure this out...

I have a number of functions in .bashrc

One function I'll call hctinfo() and it is considered by me to be the "main" part of my set of functions. It calls other functions depending on the options (handled by getopts.

Looks like this:

Code:
version()
{
...
}
usage()
{
...
}
help()
{
...
}
getAdminStatus()
{
...
}
getOperStatus()
{
...
}
getDescription()
{
...
}
getImage()
{
...
}
getGroup()
{
...
}
getMod()
{
...
}
getPackages()
{
...
}
hctinfo()
{
        ADMIN_STAT=0
        GROUP=0
        IMAGE=0
        DESC=0
        MOD=0
        OPER_STAT=0
        PACKS=0
        OPTSTRING=":aAdDhHiIgGmMoOpPvV"
        while getopts ${OPTSTRING} option
        do
                case ${option} in
                        h|H)    help
                                return 0;;
                        v|V)    version
                                return 0;;
                        a|A)    printf "ADMIN_STAT=1\n"
                                ADMIN_STAT=1;;
                        i|I)    IMAGE=1;;
                        g|G)    GROUP=1;;
                        d|D)    DESC=1;;
                        m|M)    MOD=1;;
                        o|O)    OPER_STAT=1;;
                        p|P)    PACKS=1;;
                        ?)      printf "\nWTF? Way to go, dumbass.\n\n"
                                usage
                                return 1;;
                esac
        done

        shift $(($OPTIND - 1))
...
...
}

The first time I log in to the system and source the file that has the functtions define and run it, it works. Each subsequent time I attempt to run it it fails to parse the options.


Code:
$ . ./.bashrc
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
\nWorking directory is /dvs/dncs\nDatabase is dncsdb\n
[14:00:22]$ hctinfo -a 00:40:7B:E1:AB:66
ADMIN_STAT=1
  Deployed
[14:00:26]$ hctinfo -a 00:40:7B:E1:AB:66
[14:00:32]$

I think I know what the problem is...

Somehow the options, options count, etc... (everything associated with getopts) needs to get reset.

Any ideas?
# 4  
Old 09-27-2006
You could unset the getopts variables at the end of your script.
Code:
unset $OPTSTRING
unset $OPTIND
etc...

# 5  
Old 09-27-2006
Quote:
Originally Posted by Glenn Arndt
You could unset the getopts variables at the end of your script.
Code:
unset $OPTSTRING
unset $OPTIND
etc...

You are a genius. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

2. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

3. Shell Programming and Scripting

Using getopts in ksh

I want to use getopts in ksh scripting to obtain multiple values currently im using while getopts vt: opt do case "$opt" in -v) vn=$OPTARG;; -t) tn="$OPTARG";; ;; # terminate while loop esac done however variables vn tn dont store any... (3 Replies)
Discussion started by: E.sh
3 Replies

4. Shell Programming and Scripting

Getopts in the subshell of ksh

Hi, the getopts doesnt seem to be working in the subshell of the ksh. when I echo $@ and $* from the subshell it shows nothing. even when I am capturing the parameters from the outer shell and passing while invoking the file then I am still not getting it properly. the below code is in the... (9 Replies)
Discussion started by: hitmansilentass
9 Replies

5. Shell Programming and Scripting

ksh "getopts" -- Unsetting an Option

I use the "getopts" ksh built-in to handle command-line options, and I'm looking for a clean/standard way to "unset" an option on the command line. I don't know if this is a technical question about getopts or more of a style/standards question. Anyway, I understand that getopts processes its... (4 Replies)
Discussion started by: Matt Miller
4 Replies

6. Shell Programming and Scripting

Getopts inside a function is not working

Hi All, I am using geopts inside a function in shell script. But it is doesnt seem to read the input args and I always gt empty value in o/p. my code is http://sparshmail.ad.infosys.com/owa/14.2.318.4/themes/base/pgrs-sm.gif This message has not been sent. #!/bin/ksh IFS=' '... (1 Reply)
Discussion started by: prasperl
1 Replies

7. Shell Programming and Scripting

Help on getopts in ksh

I am trying to make a Shell Script in KSH on Linux box and Solaris box which takes few arguments... with long options using getopts (not getopt). I have my sample code... at the end I will say my requirement... Please help me folks... $ cat dummy #!/bin/bash # Argument = -t test -r server... (6 Replies)
Discussion started by: explorer007
6 Replies

8. Shell Programming and Scripting

getopts function in Perl

Hi All I have searches getopts function in Perl a lot, but yet i didn't cleared with it. First I want to know what is the meaning of getopts('t:c:', \%options); and please explain getopts function in an easy way.. (4 Replies)
Discussion started by: parthmittal2007
4 Replies

9. Shell Programming and Scripting

ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the... (2 Replies)
Discussion started by: kchriste
2 Replies

10. Shell Programming and Scripting

ksh and getopts

Hello, I'm writing a script that uses command line options and arguments. To handle these, I'm using getopts. Is there a way to set up an option where an argument is optional? What I'm trying to do is offer -v for verbose output, -vv for increased output -vvv etc. Any suggestions? ... (1 Reply)
Discussion started by: garskoci
1 Replies
Login or Register to Ask a Question