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 - 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 |
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. |
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
|
|
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. |