Problem with getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with getopts
# 1  
Old 03-19-2007
Problem with getopts

I need to parse parameters but the arguments could be NULL,example:
Code:
> cat getopts.sh  
while getopts "a:b:" opt 2>/dev/null
do
    case "${opt}" in
      a)   echo "A:${OPTARG}" ;;
      b)   echo "B:${OPTARG}" ;;
      *)   exit 1 ;;
    esac
done

> getopts.sh -a TEST1 -b TEST2
A:TEST1
B:TEST2

> getopts.sh -a -b TEST2      
A:-b

The first posted execution of the script works fine , but i have a problem with the second one, getopts uses the -b parameter as the argument of the -a one :-(

How can i avoid this??

Thank u all in advance.
# 2  
Old 03-19-2007
Try a quick fix!
if you MUST have "-a xx AND -b zz" on the command line then add this to your code.....

if [ $# != 4 ]
then
echo "Usage; `basename $0` -a value -b value'
exit 9
fi

while getopts "aSmilie" opt 2>/dev/null
do
case "${opt}" in
a) echo "A:${OPTARG}" ;;
b) echo "B:${OPTARG}" ;;
*) exit 1 ;;
esac
done

> getopts.sh -a TEST1 -b TEST2
A:TEST1
B:TEST2

> getopts.sh -a -b TEST2
Usage; getopts.sh -a value -b value
# 3  
Old 03-20-2007
Thank“s for the reply ,however that doesn't solve my issue.

The key point here is that i need a way to handle with NULL/NOT NULL arguments.
So, in the example previously posted , the result of:
Code:
getopts.sh -a -b TEST2

Must be:
Code:
A:
B:TEST2

I know i can use other scripting tools , but i would like to do it with getopts.

Thank you all.

Cheers

Last edited by Klashxx; 03-20-2007 at 08:17 AM..
# 4  
Old 03-20-2007
Hi,
You can achieve the same without using getopts :
Code:
#!/usr/bin/bash

ARGUMENTS="a:b"
check_in_args()
{
	PAT=`echo ${ARGUMENTS} | awk -F: -v arg=$1 '{
			for(i=1;i<=NF;i++)
			{
				if("-" $i == arg)
				{
					printf("FOUND");
					break;
				}
			}
		}'`
	if [ "${PAT}" = "FOUND" ]
	then
		return 1
	else
		return 0
	fi
}

while [ $# -ne 0 ]
do
	case $1 in
	-a)
		check_in_args $2 RET
		if [ $? -eq 1 ]
		then
			A_VAL=""
		else
			A_VAL="$2"
			shift
		fi
	;;
	-b)
		check_in_args $2 RET
		if [ "${RET}" = "1" ]
		then
			B_VAL=""
		else
			B_VAL="$2"
			shift
		fi
	;;
	*)
	;;
	esac
	shift
done
echo "A:${A_VAL}"
echo "B:${B_VAL}"

Thanks
Raghu

Last edited by Perderabo; 03-20-2007 at 01:22 PM.. Reason: Add code tags for readability
# 5  
Old 03-20-2007
Quote:
Originally Posted by Klashxx
The key point here is that i need a way to handle with NULL/NOT NULL arguments.
NULL arguments are not a problem. This is how to do a NULL argument:
Code:
getopts.sh -a "" -b TEST2

What you want is for a missing argument to be treated as NULL. Suppose that I want to run the script only using the -a option and I want the argument to the -a option to be "-b". This why "getopts.sh -a -b" must be treated the way it is. What I have sometimes done in cases like this is to use -a and -b which expect no arguments and -A and -B that demand an argument. The only other solution is to give up on getopts as Raghu suggested.
# 6  
Old 03-21-2007
Thanks!! Perderabo for clarify me this question, and Thanks!! to Andrek and Raghu for the solutions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify problem with while getopts

can anyone spot a problem with the below: $ $ cat getopts.sh #!/bin/sh usage() { echo "myscript.sh local /tmp data.txt 600s -query" 1>&2; exit 1; } while... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

problem with getopts

Hi, I have written a script to take command line arguments using geopts.This is the code. #!/bin/sh # The usage of this script. usage="Usage is $0" usage="$usage " usage="$usage " usage="$usage " # Use the getopt utility to set up the command line flags. set -- `/usr/bin/getopt... (4 Replies)
Discussion started by: arijitsaha
4 Replies

3. Shell Programming and Scripting

getopts - optional and problem to display help

In the below code while getopts :rfw:d:s:a: options do case "$options" in r) echo reverse;; f) echo forward;; w) window=$OPTARG;; d) duration=$OPTARG;; s) search=$OPTARG;; a) value=$OPTARG;; *) help; exit;; esac done ... (2 Replies)
Discussion started by: Amutha
2 Replies

4. Shell Programming and Scripting

getopts problem

Hi everyone I want to know how can we pass multiple argument in getopts suppose PARAMS="abcd" while getopts ${PARMS} FLAG do case ${FLAG} in (a) (b) (c) (d) esac (6 Replies)
Discussion started by: aishsimplesweet
6 Replies

5. Shell Programming and Scripting

getopts problem

How do I get the getopts command to display whats written at my help option if no option is types in? For example, myscript.sh -h will bring up my help option, however, I also want myscript.sh to do the same! #!/bin/bash while getopts :abh opt do case "$opt" in... (2 Replies)
Discussion started by: linuxkid
2 Replies

6. UNIX for Advanced & Expert Users

getopts problem

i was going through the man page of getopts this particular section is not clear to me can anyone please clarify in a little detail so that i can understand the concept MANPAGE:: Since getopts affects the current shell execution environ- ment, it is generally provided as a... (7 Replies)
Discussion started by: mobydick
7 Replies

7. Shell Programming and Scripting

problem with getopts

Hi, I am a new member to unix.com. Actually I am facing a problem with getopts. In my script i have used getopts to parse the parameters. when i use the script as shown below its working fine: find_status -p all ### where find_status is a script name. But even if I pass more than one... (3 Replies)
Discussion started by: pvamsikr
3 Replies

8. Shell Programming and Scripting

Problem in getopts

while getopts l:f:s:o:h: c do case $c in l) tail -${OPTARG} /etc/passwd exit 2;; f) head -${OPTARG} /etc/passwd exit 3;; s) grep ${OPTARG} /etc/passwd | cut -d: -f7 exit 4;; o) OARG=$OPTARG exit 5;; h) ... (3 Replies)
Discussion started by: nadman123
3 Replies

9. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

10. Shell Programming and Scripting

getopts

I have a script that facillitates NDM (Connect::\Direct) transfer to remote hosts. This script uses getopts to parse through the parameters passed to it and to set appropriate variables based upon what was passed in. Kickoff="mv $PATH/$FILE1 $PATH/$FILE2" ndm_shell.ksh -p $Node -s $Source -d... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question