The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
option followed by : taking next option if argument missing with getopts gurukottur Shell Programming and Scripting 2 03-17-2008 09:46 AM
Help in getopts chella Shell Programming and Scripting 4 11-01-2007 10:09 PM
help in getopts problems Shell Programming and Scripting 1 05-04-2006 08:07 PM
getopts yerra Shell Programming and Scripting 5 03-26-2005 07:43 AM
getopts google Shell Programming and Scripting 3 12-05-2002 04:42 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-25-2008
Registered User
 

Join Date: Jul 2008
Posts: 3
getopts with non-option arguments?

Hello everyone,

Is it possible to use getopts and also receive arguments without option flags?

e.g. myscript arg1 arg2 -a arg3 -b arg4

If so, how do you stop getopts from exiting as soon as it detects the non-option arguments?
Reply With Quote
Forum Sponsor
  #2  
Old 07-26-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 1,045
Yes, but as myscript -a arg3 -b arg4 arg1 arg2. man getopts for further information
Reply With Quote
  #3  
Old 07-26-2008
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 784
Quote:
Originally Posted by kdelok View Post
Hello everyone,

Is it possible to use getopts and also receive arguments without option flags?

e.g. myscript arg1 arg2 -a arg3 -b arg4

If so, how do you stop getopts from exiting as soon as it detects the non-option arguments?

Save and remove the leading arguments before calling getopts, e.g.:

Code:
n=1
while [ $# -gt 0 ]
do
  case $1 in
    -*) break;;
    *) eval "arg_$n=\$1"; n=$(( $n + 1 )) ;;
  esac
  shift
done

while getopts abc opt
do
  case $opt in
    a|b|c) echo opt $opt ;;
  esac
done
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
getopts

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0