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
option followed by : taking next option if argument missing with getopts gurukottur Shell Programming and Scripting 2 03-17-2008 12:46 PM
Help in getopts chella Shell Programming and Scripting 4 11-02-2007 01:09 AM
help in getopts problems Shell Programming and Scripting 1 05-04-2006 11:07 PM
getopts yerra Shell Programming and Scripting 5 03-26-2005 10:43 AM
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
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-25-2008
kdelok kdelok is offline
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?
  #2 (permalink)  
Old 07-26-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,861
Yes, but as myscript -a arg3 -b arg4 arg1 arg2. man getopts for further information
  #3 (permalink)  
Old 07-26-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,311
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
Sponsored Links
Closed Thread

Bookmarks

Tags
getopts

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 05:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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