Use of getopt with and without argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use of getopt with and without argument
# 1  
Old 03-12-2010
Use of getopt with and without argument

Hi,

I am in middle of using some getopt command and am finding some issue. The usage of my script can be like this:
abc.sh <-d | -p |-r> [< -s single_id> | file>] -online < 0 | 1>
The first argument can be either -d or -p or -r. The second argument can be either -s and a id or a file name. So I am stuck up in writing getopts for this.
The below snipper doesn't work.

Code:
while getopts "d:p:r:s:o:" param
do

   case "$param" in      

      -d|-p|-r)    deviceType=$param      			
                  IN_FILE=$OPTARG
                  statFile $IN_FILE                
                ;;

      -s)        requested_id=$OPTARG ;;

In this case i am not sure whether the 2nd argument is -s or a file name. that means -d may or may not have an option. How can I write that. In this perhaps -d is always excepting a next argument and breaking.

Please help me.

Thanks in advance.
Sachin
# 2  
Old 03-12-2010
If you want to give argument to a option we should give ':' after the argument . If you don't want argument for an option the ';' is not needed after the option .

Code:
while getopts "dprs:o" param

Here option d,p,r,o is not deal with the arguments ,so here ';' is not needed .But ':' is need after the option s,because it deals with argument .
# 3  
Old 03-12-2010
Thanks for the reply. But the problem is dpr argument may or may not have an option. For example:
-d filename --> with option
-d -s id --> without option.

I need to handle both type of arguments as I have shown above. Both are valid and i need to handle them.
So please suggest me some way.

Thanks
Sachin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopt Help

Hi All, An old work friend wrote a script which I've been trying to understand how a section of it currently works and work out how i can add some command line switches which i can use later in the script to append the output depending on the command line arguements. Currently it works by... (1 Reply)
Discussion started by: mutley2202
1 Replies

2. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

3. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

4. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

5. Shell Programming and Scripting

Help with getopt

Hi, I want to use the getopt function to parse some arguments for a script. while getopts "i:f:r:" OPTION do case $OPTION in i) iter=$OPTARG;; f) frame=$OPTARG;; r) roi=$OPTARG;; ?) echo Usage: ...... exit 2;; esac done However, I... (5 Replies)
Discussion started by: giorgos193
5 Replies

6. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

7. Shell Programming and Scripting

getopt help

:) Can anybody help me about how to use getopt in shell scripting. (3 Replies)
Discussion started by: darshakraut
3 Replies

8. Shell Programming and Scripting

getopt help

I m trying to use getopt This is my script, but it doesn't take argument in variable, Please help. set - - `getopt mscl: $*` if then echo "Exiting...." exit 2 fi for i in $* do case $i in -m) MAIL="$i"; shift;; -s) SCRIPT=$OPTARG; shift;; -c) COB=$OPTARG; shift;;... (2 Replies)
Discussion started by: darshakraut
2 Replies

9. Shell Programming and Scripting

getopt

#!/bin/sh set -- `getopt "abco:" "$@"` a= b= c= o= while : do case "$1" in -a) a=1;; -b) b=1;; -c) c=1;; -o) shift; o="$1";; --) break;; esac shift done shift # get rid of -- # rest of script... # e.g. ls -l $@ (6 Replies)
Discussion started by: Hitori
6 Replies

10. Shell Programming and Scripting

getopt help

scriptname i have made a script to perform so tasks and i managed to complete the tasks for all the options the problem i am facing is that i can run the scripts individually but i would like to make it such that it can accept multiple options and give me the appropriate output e.g.... (1 Reply)
Discussion started by: problems
1 Replies
Login or Register to Ask a Question