The UNIX and Linux Forums  

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 12:47 PM
question about getopts ahtat99 Shell Programming and Scripting 2 08-20-2006 02:45 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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-26-2007
andy2000 andy2000 is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 36
getopts question!!!

is there a better way to check if all args are set???

while getopts h:p:u: opt
do
case "$opt" in
h) host="$OPTARG";;
p) port="$OPTARG";;
u) user="$OPTARG";;
\?)
echo >&2 \
"usage: $0 -h host -p port -u user"
exit 1;;
esac
done

shift `expr $OPTIND - 1`

if test -z "$host"
then
echo >&2 \ "host missing !!!\nusage: $0 -h host -p port -u user"
exit 1
fi

if test -z "$port"
then
echo >&2 \ port missing\n "usage: $0 -h host -p port -u user"
exit 1
fi

if test -z "$user"
then
echo >&2 \ "user missing!!\nusage: $0 -h host -p port -u user"
exit 1
fi

Last edited by andy2000; 03-26-2007 at 11:41 AM..
  #2 (permalink)  
Old 03-27-2007
Raghuram.P Raghuram.P is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 44
Hi,
According to me best way to check whether the all arguments are set is to have a default value for all the variables(have separate variable for every option) and checking it at the end of the while loop..like this

A_VAL=""
B_VAL=""
while [ $# -ne 0 ]
do
case $1 in
-a)
if [ $2 != "" ]
then
A_VAL="$2"
shift
fi
;;
-b)
if [ "$2" != "" ]
then
B_VAL="$2"
shift
fi
;;
*)
;;
esac
shift
done
if [ "${A_VAL}" = "" ]
then
echo "Throw error"
fi
.....

Thanks
Raghuram
Closed Thread

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 08:49 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