Unix Shell - Is it possible in optargs

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Is it possible in optargs





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Is it possible in optargs
BN

2007-05-18, 1:18 am

Greetings

SUN OS 5.10

IF I dont give any arguements to optargs switch it will take the value
I define
if I pass an argument to switch, it should take that value:



#!/bin/ksh

.. ~/.profile

while getopts adl Opt
do
case $Opt in
a) [ -z "$Listener" ] && Listener="LISTENER"
echo start_listener
echo start_db
;;
d|D) echo DB=$OPTARG;
if [ -z "$OPTARG" ]
then
# ORACLE_SID="$ORACLE_SID"; # Take it from
ENV .profile
echo start_db
else
ORACLE_SID="$OPTARG"; echo start_db
fi
;;
l|L) echo "LSNR=$OPTARG";
if [ -z "$OPTARG" ]
then
Listener="LISTENER" ;echo start_listener
else
Listener="$OPTARG"; echo start_listener
fi
;;
*)exit;;
esac
done


Regards & Thanks
BN

sg

2007-05-18, 7:20 am

When you want a value to be passed to an option, you must follow the
option by a colon (. But the option MUST be folowed by the value.
The option letter is case-sensitive. If you ask for option a, d and l,
do not test for D not L.


>
> while getopts adl Opt


to be replaced by


while getopts ad:l: Opt

If the value is missing you will get an error.

Regards,
Stephane
Stephane CHAZELAS

2007-05-19, 1:20 pm

2007-05-17, 20:45(-07), BN:
> Greetings
>
> SUN OS 5.10
>
> IF I dont give any arguements to optargs switch it will take the value
> I define
> if I pass an argument to switch, it should take that value:

[...]

You can't have options with optional arguments with getopts.

If -a was to take optional arguments, in

cmd -a -b

Is "-b" another option or an argument to -a.

At http://stchaz.free.fr/ you'll find an implementation of
getopts that support optional arguments to options. In which
case, you have to write it:

cmd -aARG
cmd -a-b
cmd -a

(and there's no way to pass an empty argument).

or

cmd --long=arg
cmd --long

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com