The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using getopts TurboArkhan Shell Programming and Scripting 3 06-22-2009 04:31 PM
Help in getopts chella Shell Programming and Scripting 4 11-02-2007 01:09 AM
getopts help GrepMe Shell Programming and Scripting 3 06-20-2007 11:47 AM
help in getopts problems Shell Programming and Scripting 1 05-04-2006 11:07 PM
getopts google Shell Programming and Scripting 3 12-05-2002 07:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-22-2006
lyonsd lyonsd is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 35
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 (permalink)  
Old 09-22-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,420
Specify the arguments of the script to the function :
Code:
# Call the function to parse arguments with getopts
my_function "${@}"
Jean-Pierre.
  #3 (permalink)  
Old 09-27-2006
lyonsd lyonsd is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 35
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 (permalink)  
Old 09-27-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
You could unset the getopts variables at the end of your script.
Code:
unset $OPTSTRING
unset $OPTIND
etc...
  #5 (permalink)  
Old 09-27-2006
lyonsd lyonsd is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 35
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0