Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parameter question

Status
Not open for further replies.

hokky

Technical User
Nov 9, 2006
170
AU
Hi guys,

I just wanna have flexible parameter for my script and I couldn't find out how to put validation on it.

The script has one or more parameter, but the sequence could be changed

example :
./script.ksh -U user (accepted)
./script.ksh -U user -P password (accepted)
./script.ksh -P password -U user (accepted)

Anyone got any idea ?

Thx heaps guys
 
Probably I need to put more information,

If I want to input username, I have to give -U first.
Same if I wanna input password, I have to give -P first.

But there is input which is super user that doesn't need any password.

How to make this parameter feasible ?

Thanks guys
 
ksh anni,

Any idea how ??
Thanks dude
 
#!/usr/bin/sh

set -- `getopt PU: $*`

#getopt UP: $*

while [ $# -gt 0 ]; do
case $1 in
-U)
shift 1
USER=$1
;;
-P)
shift 1
PASSWD=$2
;;
--)
shift
break
;;
esac
shift
done

echo USER $USER
echo PASSWD $PASSWD
 
In ksh don't use the deprecated getopt but getopts:
man getopts

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks guys,

After work around I found this script :
Code:
#!/usr/bin/ksh

#USER=
#PASSWD=

while getopts P:U: OPTION 2>/dev/null
do
        case ${OPTION} in
                U)
                        USER=${OPTARG}
                        ;;
                P)
                        PASSWD=${OPTARG}
                        ;;
                *) echo "please put proper input"
                        exit 2;;
        esac
done

echo USER $USER
echo PASSWD $PASSWD

The interesting part is

please see this input and result
Code:
[root@dhcppc0 Script]# ./coba.ksh -U user -P password
USER user
PASSWD password
[root@dhcppc0 Script]# ./coba.ksh -P user -U password
USER password
PASSWD user
[root@dhcppc0 Script]# ./coba.ksh -U user
USER user
PASSWD
[root@dhcppc0 Script]# ./coba.ksh -P password
USER [b]test[/b]
PASSWD password

see the last test, where the "test" coming from ??? probably from previous input, but how come it doesn't get cleared ?

Any explanation guys ?

That's why I have to put
USER=
PASSWD=

as declaration
 
I forgot one thing, what "shift" command for ??

I read the man shift, but I don't quite understand.

Thanks guys
 
You are probably logged in as user "test", it is a predefined variable in Korn shell.

Try use another variable name cobauser or so.

The shift loses the first parameter given and shifts the other variables to the left.

testscript a b c d

at invocation:
$1=a
$2=b
$3=c
$4=d
$#=4

after 'shift':
$1=b
$2=c
$3=d
$4 is not set anymore (null string)
$#=3

after another 'shift 2':
$1=d
$#=1
$2 to $4 are not set anymore



HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top