LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   getopts long word options (https://www.linuxquestions.org/questions/linux-newbie-8/getopts-long-word-options-948721/)

maddyfreaks 06-05-2012 09:51 PM

getopts long word options
 
Hello Linux Experts,

I have an assignment to make a Shell Script in KSH on Linux box and Solaris box which takes few arguments... with long options using getopts (not getopt).

I have my sample code... at the end I will say my requirement... Please help me folks...


$ cat dummy
#!/bin/bash
# Argument = -t test -r server -p password -v

usage()
{
cat << EOF
usage: $0 options

This script run the test1 or test2 over a machine.

OPTIONS:
-h Show help message
-t Take Argument and Print
-v Verbose
EOF
}

TEST=
VERBOSE=
while getopts "ht:v" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
t)
TEST=$OPTARG; echo -e " \n Output : Your Input is ${TEST} \n"
;;
v)
VERBOSE=1
;;
?)
usage
exit
;;
esac
done

*********************

[kumar@explore-02] "/home/kumar/TEST/"
$ dummy.ksh -t Taking_Input

Output : Your Input is Taking_Input

***************************************

but my getopts not allowing long word like -variable

$ dummy.ksh -variable Taking_Input

How to make the modification to script to use -t | -- variable like wise getopt in getopts... please help me...

Sorry if its the same question some one asked.

Any help is appreciated.

AwesomeMachine 06-05-2012 09:56 PM

This is a pretty good explanation:

http://www.mkssoftware.com/docs/man1/getopts.1.asp

maddyfreaks 06-05-2012 10:31 PM

AwesomeMachine - Thanks for your reply. But my questions is not cleared yet..!!

can we do long option in getopts in KSH....?

If so how please and if nt why since my mentor said its possible ( am not sure how )

Please help

AwesomeMachine 06-06-2012 12:10 AM

Try it this way:

/bin/ksh

. . .

OPTION="[h:help?Show help messages.]"
OPTION+="[t:take?Take argument and print.]:$OPTARGS"
OPTION+="[v:verbose?Verbose.]"
while getopts "$OPTION" opt ; do
case $opt in
h) help ;;
t) take $OPTARG ;;
v) verbose ;;

esac

done

I don't know if this gives you enough context to figure out the rest, but it should. It might not be perfect. I didn't actually try it, but you've almost got it. I hope I helped a little bit, but I still want you to learn the material.

maddyfreaks 06-06-2012 07:05 AM

Awesome machine - Thanks once again for your help. The way to organise getopts from your reply did not help. I searched google a lot... still waiting for few more replies from few experts

chrism01 06-06-2012 07:30 PM

http://docs.oracle.com/cd/E19253-01/...s-1/index.html

maddyfreaks 06-07-2012 06:21 AM

Chris - I have tried the way it was shown in Oracle Docs... but its not working...


#!/bin/ksh

#getopts "f:(file)(input-file)o:(output-file)"
while getopts "a:(file)fz" option;
do
case $option in
a)
echo "received -a with parameter ${OPTARG}"
;;
f)
echo received -f
;;
z)
echo received -z
;;
*)
echo "invalid option -$OPTARG"
;;
esac
done

Execution :
$ desk -file HISTORY
received -f
invalid option -
invalid option -
invalid option -

Please help me....


All times are GMT -5. The time now is 03:26 AM.