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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-04-2008
TurboArkhan's Avatar
TurboArkhan TurboArkhan is offline
Registered User
  
 

Join Date: Mar 2008
Location: Earth!
Posts: 6
getopts issue! UPDATED! Please read :)

I am having some trouble/questions with getopts that I can't find any solid info on with google

I need it to parse things of the syntax of:

-r # # # -f [word] [filename] -c [filename]

with as many repeats as possible, and it should catch erroneous commands also, but continue going...

my first question is, -r accepts 3 arguments. Does this mean I need 3 :'s when I do
Code:
while getopts r:f:c: o
also, what does the o in the above line represent exactly?

here is my code so far:
Code:
#! /bin/bash

while getopts r:f:c: o
do case "$o" in
	r)sh right.s $OPTARG $(( $OPTARG+1 )) $(( $OPTARG+2 ));;
	f)sh findtext.s $OPTARG $(( $OPTARG+1 ));;
	c)sh count.s $OPTARG;;
	?)echo "Useage: -r [#] [#] [#]"
		echo "	-f [key] [filename]"
		echo "	-c [filename]";;
esac
done
This doesnt work as desired. OPTARG holds the argument for the parsed option...... but since -r takes 3 arguments, does anyone know how to pass it the next THREE arguments? the above method adds 1 and 2 to the value of OPTARG....

Also, in regards to -c , if *.(extension) wildcards are passed, it only operates on the first one........why?

Is there a way to dereference OPTIND which holds the index of the argument to be parsed? Then I could dereference OPTIND, OPTIND+1 and OPTIND+2 to get the next 3 arguments properly passed...!

Thanks for reading! Any help is greatly appreciated :-D

Last edited by TurboArkhan; 03-05-2008 at 12:08 AM..
  #2 (permalink)  
Old 03-05-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
Here is an example of using multiple optional arguments. All of them are in OPTARG.
Code:
#!/bin/ksh
# foo is shell function in this example - it could be a separate script
foo()
{
	echo "foo: I got $# options"	
	echo " they are $@"
}
while getopts a:  opt
do
  set -A arr $OPTARG
  case $opt in
    a) echo "option -a"
       for i in $OPTARG
       do
           echo "subargument = $i"         
       done
       echo "running foo with these options"
       foo $OPTARG
       exit 0 ;;
   \?) echo "\nUsage: $0 [-a <option>]" >&2
        exit 2;;
  esac
done
usage:
Quote:
csadev:/home/jmcnama> t.sh -a "1 2 3 4"
option -a
subargument = 1
subargument = 2
subargument = 3
subargument = 4
running foo with these options
foo: I got 4 options
they are 1 2 3 4
  #3 (permalink)  
Old 03-05-2008
TurboArkhan's Avatar
TurboArkhan TurboArkhan is offline
Registered User
  
 

Join Date: Mar 2008
Location: Earth!
Posts: 6
so you mean to tell me ALL I needed to do this whole time was encase all the arguments in quotation? It works perfect with them!... but is there a proper way to omit the quotes?

..... ??... .....

EDIT: This code block, if i give arguments to bad options, it immediatley exits the program instead of pressing onward

Code:
 #! /bin/bash

while getopts r:f:c: o
do case "$o" in
	r)sh right.s $OPTARG;;
	f)sh findtext.s $OPTARG;;
	c)sh count.s $OPTARG;;
	?)echo "Useage: -r ''[#] [#] [#]'' "
		echo "	-f ''[key] [filename]''"
		echo "	-c ''[filename]''";;
esac
done
EDIT: While this works, if an invalid command is given arguments, it gives the ?) echo's, and then returns to the prompt, as opposed to pressing on if there are more arguments included....

did I miss something?

Last edited by TurboArkhan; 03-05-2008 at 07:52 PM..
  #4 (permalink)  
Old 06-22-2009
HBleuler HBleuler is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 1
Hello

Try indirect expansion with !
First set A3 to the index of the 3rd argument and at the end increment OPTIND by 2

r) A3=$(( $OPTIND + 1 ))
sh right.s $OPTARG ${!OPTIND} ${!A3}
OPTIND=$(( $OPTIND + 2 )) ;;
Sponsored Links
Reply

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 09:59 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