getopts not updating from subroutine.

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts not updating from subroutine.
# 1  
Old 07-29-2009
getopts not updating from subroutine.

So, I'm running a script with a couple of subroutines, one of which takes arguments using getopts. The first time i call the subroutine everything works as expected, the second time I call it none of the arguments change. Here's a small section of code that shows this behavior.

#Testing getopts

while getopts ":a:b:" opt; do
    case $opt in
    a ) aarg="$OPTARG";;
    b ) barg="$OPTARG";;
    \?) echo "usage" ;;
shift $(($OPTIND - 1))

echo $aarg $barg

Sbrt -a A1 -b B1
Sbrt -a A2 -b B2


A1 B1
A1 B1

So how can I call the subroutine again and have it update the arguments (aside from making the subroutine a separate script, of course). I searched, if anyone knows of any threads pertaining to this let me know and i'll clear this out of here.
# 2  
Old 07-29-2009
I don't know if this is the "proper" way to do things but it works. Change the line:

shift $(($OPTIND - 1))



# 3  
Old 07-29-2009

I thought that line might be trouble but didn't know much about what it did so I left it be.
# 4  
Old 07-29-2009
Ha. Truth be told, I don't know what it does (or is supposed to do) either. I know what shift does. I know what OPTIND is. Together it's weird. OPTIND starts at 1 and at the end of reading your args is 5. Shift will shift positional parameters by 1 (or by n if 'shift n') so, what I see is "shift positional parameters forward 5-1 (or 4).

On the other hand. I've seen that used before just never had to deal with it directly so it must be useful. Maybe someone else will shed some light.

Glad I could help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subroutine or Function Summary

I have a fortran file with code declarations such as Subroutine str_tnum_tu & ( & s, dl, tu, pos & ) ! Class (*), Intent (InOut) :: tu(:) Character (Len=*), Intent (In) :: s, dl Character (Len=*), Intent (In), Optional :: pos ... or ... (11 Replies)
Discussion started by: kristinu
11 Replies

2. Programming

perl: Subroutine question

Hi everyone, I have given up finally trying to find a way to do this. I have a subroutine called LoginFirst where I am starting a new SSH session. I have bunch of subroutines, each one of them uses a (or I have to create a new SSH constructor everytime) ssh connection to get some value so ... (2 Replies)
Discussion started by: dummy_code
2 Replies

3. UNIX for Dummies Questions & Answers

Help with Subroutine

Okay I have a 1TB drive that is almost completely full with vids. I am in the process of converting them to mp4. I have two scripts right now. One is a shell script to convert them with Handbrake. The other is a script to get a sort of progress report. To make things easier to understand, I will... (0 Replies)
Discussion started by: Dalton63841
0 Replies

4. Shell Programming and Scripting

Running more than one function from a subroutine

Hi All I am new to perl so had one question. I have one Subroutine which works as expected and below is just an example. where I ran a command and check the output. so my question is if I have more than one command then how to check the results, meaning I want to add more command and check... (2 Replies)
Discussion started by: tannu
2 Replies

5. Programming

memory allocation in subroutine

Hi everyone, I'm not new to C programming, but I'm having question regarding the memory allocation of a pointer variable which, for instance, will be declared in main(), but its memory will be allocated in subroutine. To clearify my question, I provide a small working example: #include... (1 Reply)
Discussion started by: MIB_Maik
1 Replies

6. Programming

Subroutine Hung

Hi friends I am Administrator for a system works with uinx OS but, many times I get messages from server console inform me about Subroutine is Hanging so what can I do to reset this Subroutine? Note: always when I got that I restart the server but I think that is not professional solution. (3 Replies)
Discussion started by: bintaleb
3 Replies

7. AIX

Using the passwdpolicy() subroutine.

Okay, so in AIX, there are various subroutines that is built in to the OS. The subroutine is I want to use is passwdpolicy(). So I want to construct a C program that will be able to pass credentials into the program and thusly into the subroutine. I'm not asking for homework, or for someone to... (0 Replies)
Discussion started by: syndex
0 Replies

8. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

9. Programming

chpass subroutine not updating NIS

Hello, I'm implementing a change password interface in our company's AIX application but am having trouble getting it to work with NIS using the chpass subroutine. According to the "chpass" subroutine man page it handles password changes for local, NIS, and DCE passwords. However ... (1 Reply)
Discussion started by: rmm47
1 Replies

10. Shell Programming and Scripting

Problem in subroutine calling

Hi, we can call the subroutines using two ways .... 1) calling subroutine name preceeded by & symbol. 2)Another one is without &symbol.... what is the diff b/w these two.... ############################ #usr/bin/perl fun; sub fun { print "hi this is from perl\n"; }... (1 Reply)
Discussion started by: sarwan
1 Replies
Login or Register to Ask a Question