LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 03-16-2009, 06:57 AM   #1
dairo
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
Handling null options in a bash script which ought to have a value


I am having a shell script that is triggered by an external program. This external program starts the shell script with a few options and values. The problem here is that the external script does not always fill all the options with values,it sometimes leaves option on the command line without any value attached to it. In that case the options which does not have a value assumes the next option to be its value

************Program code************************************
cat testing
#!/bin/bash
while getopts c:l:g:u:s:t:a:e:y:z option
do
case ${option} in
c) c=${OPTARG};;
l) l=${OPTARG};;
s) s=${OPTARG};;
g) g=${OPTARG};;
t) t=${OPTARG};;
a) a=${OPTARG};;
e) e=${OPTARG};;
u) u=${OPTARG};;
y) y=${OPTARG};;
z) z=${OPTARG};;
\?) echo " usage error"
exit;
esac
done

echo "c=$c"
echo "l=$l"
echo "s=$s"
echo "t=$t"
echo "a=$a"
echo "e=$e"
echo "g=$g"
echo "u=$u"
echo "y=$y"
echo "z=$z"
**********************End of program**************************

eg :-
./testing -a val1 -l val2 -g -u val4
output:-
c=
l=val2
s=
t=
a=val1
e=
g=-u
u=
y=
z=


Here we see that -g option assumes that -u is a value fed to it. This way I am loosing out on the last value val4. Is there a work around for this problem in bash?
 
Old 03-16-2009, 07:13 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 2,873

Rep: Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700
Try it like this (with the double quotes); this is Korn shell (so "print -u2" might not work for you) but it should give you the idea:
Code:
#       define a fatal error function
function fatal
{
        print -u2 "${1}"
        exit 1
}
#       initialize flags to null
aflag=
bflag=
#       define usage message
USAGE="Usage:\t${0} [-a value] [-b value] args"
#       process command line arguments
while getopts :?a:b: name
do
        case ${name} in
        a)      aflag=1
                aval="${OPTARG}";;
        b)      bflag=1
                bval="${OPTARG}";;
        :)      print -u2 "${0}:\t${OPTARG} requires a value"
                fatal "${USAGE}";;
        \?)     print -u2 "${0}:\tunknown option"
                fatal "${USAGE}";;
        esac
done
if [ ! -z "${aflag}" ]
then
        print "Option -a ${aval} specified"
fi
if [ ! -z "${bflag}" ]
then
        print "Option -b ${bval} specified"
fi
shift $((${OPTIND}-1))
if [ "$*" ]
then
        print "Remaining arguments are:\t${*}"
fi
exit 0
The problem is that you're not trapping an option when an argument is required (that's what "colon)" is for).

[EDIT]
You know, I've been fooling around with this and, dang it, there just doesn't seem to be anything doable so that the problem you've described will not happen if an option does not have a required argument. You can have getopts require a numeric or alpha (or, for that matter, alphanumeric) argument and that will help sometimes but just a not show the next option as the argument value, well, I can't see a way to do that.

You mention that another application is invoking the shell program; have you considered evaluating the arguments there; i.e., if an argument is null, don't invoke the shell program? That might be a more useful alternative (you know, if you don't have what you need to keep going, quit rather than make a mess).

Sorry, this one hit my limit switch and crashed.

Last edited by tronayne; 03-16-2009 at 12:30 PM.
 
Old 03-16-2009, 12:27 PM   #3
dairo
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Hi tronayne,
Thank you very much for the quick response. My apologies, looks like I have slightly misquoted the problem. The code you have written definitely retrieves the values that were otherwise lost.But still the problem of an option without a value eating the adjacent option remains.
Here my requirement is to capture the value along with the right option.In the new script I will not be sure what the value really means as it only displays the remaining arguments, after which I still don't know what they mean.
This is what I did with the new code:
Code:
#!/bin/ksh
#       define a fatal error function
function fatal
{
        print -u2 "${1}"
        exit 1
}
#       initialize flags to null
aflag=
bflag=
cflag=
dflag=
#       define usage message
USAGE="Usage:\t${0} [-a value] [-b value] args"
#       process command line arguments
while getopts :?a:b:c:d: name
do
        case ${name} in
        a)      aflag=1
                aval="${OPTARG}";;
        b)      bflag=1
                bval="${OPTARG}";;
        c)      cflag=1
                cval="${OPTARG}";;
        d)      dflag=1
                dval="${OPTARG}";;
        :)      print -u2 "${0}:\t${OPTARG} requires a value"
                fatal "${USAGE}";;
        \?)     print -u2 "${0}:\tunknown option"
                fatal "${USAGE}";;
        esac
done
if [ ! -z "${aflag}" ]
then
        print "Option -a ${aval} specified"
fi
if [ ! -z "${bflag}" ]
then
        print "Option -b ${bval} specified"
fi
if [ ! -z "${cflag}" ]
then
        print "Option -c ${cval} specified"
fi
if [ ! -z "${dflag}" ]
then
        print "Option -d ${dval} specified"
fi
shift $((${OPTIND}-1))
if [ "$*" ]
then
        print "Remaining arguments are:\t${*}"
fi
exit 0
Test results of this script yielded the following:-
Code:
./testing  -a -b bbb -d -c
Option -a -b specified
Remaining arguments are:        bbb -d -c
What we see here is that -b gets assigned to "a" and the actual value of b (bbb) is detached from it. After this I will be left with a string of characters (bbb -d -c)which I will not be able to use intelligently.

Further elaborating, this script is triggered by a very rigid process written in java or c++ (I am not really sure) where it says:

Code:
/home/testing -a %access% -c %canceluser% -l %lenght% -s %setuser% -g %get% -t %trigger% -e %extratext% -u %user% -y %uptime% -z %create%
This is a fixed line from the program, now in reality this script will not always have values for all the variables placed within the % sign. This is when we come up with my problem. An example is like this
Code:
/home/testing -a 10:30 -c -l five -s smith -g get -t -e adding up things -u smith -y -z
What we see here is that a number of options like c,t,y,z do not have values. This kind of conditions should not affect the script, this script is a multi utility script which changes its behavior based on the available variable values. Hence I must some how be able to get the right values into the right variables.
 
  


Reply

Tags
null, options


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to rsync remote dir with options uopjohnson Linux - General 3 04-23-2010 01:16 AM
help with complex bash script-XML handling panoskan Programming 4 09-18-2008 07:19 PM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 09:56 AM
bash script: making options? johngreenwood Programming 4 01-04-2007 01:55 PM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM


All times are GMT -5. The time now is 08:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration