![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 | Shell Programming and Scripting | 3 | 12-05-2002 07:42 PM | |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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.. |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|