getopts standard approach - does one exist?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts standard approach - does one exist?
# 1  
Old 05-15-2007
Question getopts standard approach - does one exist?

Hi,
I've just been looking through all the getopts related posts and there appear to be many different approaches to reading a command and its options in and checking for errors.
However, the only one I can get to work is as follows:

while getopts :fvriR opt
do
case $opt in
f) echo "f";;
v) echo "v";;
R|r) echo "Rr";;
i) echo "i";;
\?) echo "invalid option $OPTARG"
esac
done

Would I be correct in saying that the above will catch all false options entered? I honestly do not understand the significance of the ":" character in getopts - if anyone knows a page explaining its usage (other than the "man" page), please let me know.
Otherwise, I have seen the following kinds of syntax:

while getopts :f:v:r:i:R opt
while getopts f:v:r:i:R: opt

These don't work on my script - i.e. they don't catch erroneous options entered, so what are they used for, does anyone know?

And is my code above a "catch-all" for erroneous options, or does it miss out on any that I can't think of?

Last edited by dkieran; 05-15-2007 at 06:35 PM.. Reason: Error in line
# 2  
Old 05-16-2007
# 3  
Old 05-16-2007
Hi.
Returns "page deleted" for me ... cheers, drl
# 4  
Old 05-16-2007
Hi.

Your answer for colon is in the man page:
Quote:
getopts optstring name [args]
getopts is used by shell procedures to parse positional parame-
ters. optstring contains the option characters to be recog-
nized; if a character is followed by a colon, the option is
expected to have an argument,

getopts can report errors in two ways. If the first character
of optstring is a colon, silent error reporting is used.

-- excerpt from man bash in the section SHELL BUILTIN COMMANDS
See also a long example at http://www.tldp.org/LDP/abs/html/internal.html#EX33 ... cheers, drl
# 5  
Old 05-16-2007
Thanks, Ill check that one out.
# 6  
Old 05-18-2007
Another getopts problem, similar to the above. How to account for the situation where someone types the following:

command -ri file1 -v file2

My code above cannot handle that.

So here is what I have been experimenting with:

#################################################

while [ $# -gt 1 ]
do
while getopts :fvriR opt
do
case $opt in
f) fopt=1; iopt=0;;
v) vopt=1;;
[Rr]) ropt=1;;
i) iopt=1; fopt=0;;
\?) invalid_option $OPTARG;;
esac
done

# Move past the option arguments to the file arguments
shift `expr $OPTIND - 1`

filelist="$filelist $1"

done

do_whatever $filelist

#################################################

The above works for "command -ri file1 -v file2"
However, it breaks down when I try something like follows:
"command -ri file1 file2 -v file3"
I suppose the shift command is doing the damage somehow, but I can't figure out how to fix it.
Does anyone have any suggestions?

Last edited by dkieran; 05-18-2007 at 12:22 PM..
# 7  
Old 05-18-2007
dkieran,
pls use vB Codes when posting to the forums - it'll improve the chances of your questions being answered.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the right approach to take?

Hello every one, I will love to know what is the best approach to take in obtaining books online. I find it disturbing just googling a book online and downloading it without actually paying for it. I strongly believe that this is wrong and that i may not be able to unlock the key contents and... (2 Replies)
Discussion started by: despiragado
2 Replies

2. Shell Programming and Scripting

Help with approach and developing script

Hi- I need to develop a script for following scenario in AIX and K shell environment.I am from windows server background for most my career ,so please bear with me and advise suitable approach and technical assistance.Having said that I am aware of unix shell commands but never pput together at... (1 Reply)
Discussion started by: nirasm
1 Replies

3. Red Hat

What would be the best approach?

I have a table in one of my DB. The DB is about 300 gig - of that 249 gig is in this table. The data is somewhat important but even if we delete half of it won't affect anybody. I would like to reclaim some space back so my question is what would be the best approach to accomplish this task.... (6 Replies)
Discussion started by: newborndba
6 Replies

4. Programming

Oracle Procedure approach

HI All , I am new to oracle procedures. Please help me for the approach to satify the requirement. I need to create procedures. with parameters passed ( say report,type,identities,country ) It should also call sql query within the procedures and passed parameters should be used in where clause... (2 Replies)
Discussion started by: Perlbaby
2 Replies

5. Shell Programming and Scripting

Approach on Header record

All, I currently have a requirement to fetch a Date value from a table. And then insert a Header record into a file along with that date value. ex: echo "HDR"" "`date +%Y%j` `date +%Y%m%d` In the above example I used julian date and standard date using Current Date. But the requirement... (0 Replies)
Discussion started by: cmaroju
0 Replies

6. Shell Programming and Scripting

Standard out and standard error

I need to run a cronjob and in the cronjob I execute a script that if there is an error produces standard error so I do /RUNMYSCRIPT 2> mylogfile.log However, if it runs correctly, I don't get a standard error output, I get a standard out output. How do I redirect both standard error and... (2 Replies)
Discussion started by: guessingo
2 Replies

7. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

8. Shell Programming and Scripting

Approach to writting a script

Hello all, I've just joined. I did a google search and your site came up, I had a look and thought I'd like to become a member. I'm from Ireland. I've written a few scripts before, but this new task has me foxed. I would like to figure out the best approach to achieving the following ... (15 Replies)
Discussion started by: Bloke
15 Replies

9. Shell Programming and Scripting

standard error to standard out question

Hi there how can i get the result of a command to not give me its error. For example, on certain systems the 'zfs' command below is not available, but this is fine becaues I am testing against $? so i dont want to see the message " command not found" Ive tried outputting to /dev/null 2>&1 to no... (5 Replies)
Discussion started by: hcclnoodles
5 Replies
Login or Register to Ask a Question