Help on getopts in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on getopts in ksh
# 1  
Old 06-07-2012
Help on getopts in ksh

I am trying 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...


Code:
$ 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

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

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

Output : Your Input is Taking_Input

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

but my getopts not allowing long arguments like -variable

Code:
$ dummy.ksh -variable Taking_Input
or
$ 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.

Last edited by methyl; 06-07-2012 at 09:40 AM.. Reason: please use code tags
# 2  
Old 06-07-2012
Hi.

I am confused. You say you want to use ksh, but the script seems to use bash ... cheers, drl
# 3  
Old 06-07-2012
I believe getopts only accepts characters, not strings, as options. You may have to code for it yourself if that is what you really want to do.
# 4  
Old 06-07-2012
Dont you think there were a reason to have two utilities getopt and getopts?
# 5  
Old 06-07-2012
This site has an interesting discussion:
unix - Using getopts in bash shell script to get long and short command line options - Stack Overflow

Post 5 has a link to a shell function called getopts_long which could be sourced in your script. I have not studied it though but it looks interesting.
# 6  
Old 06-07-2012
The getopt long function is not a feature of ksh or the Posix Shell.
# 7  
Old 06-07-2012
Hi.

Here is a sample of the some of the extended features of ksh internal getopts:
Code:
#!/bin/ksh

# @(#) s4	Demonstrate features in ksh getopts.
# http://www.linuxmisc.com/12-unix-web-servers/7c7aac453490253a.htm

USAGE="[+NAME?$0]"
USAGE+="[+DESCRIPTION?Just a little test script]"
USAGE+="[h:hello?Greets you.]"
USAGE+="[n:name]:?[given:=drl0?Your name.]"

hello=0

while getopts "$USAGE" opt; do
    case $opt in
        h) hello=1 ;;
        n) given="$OPTARG" ;;
    esac
done

print "Your name is $given"

if [[ $hello != 0 ]]; then
    print "Hello $given!"
fi 

exit 0

and here is a driver:
Code:
#!/usr/bin/env ksh

# @(#) run4	Exercise ksh script s4.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
version && version ksh

r=./s4

pl " No parameters:"
$r

pl " Set name, -n:"
$r -n drl1

pl " Set name, --name:"
$r --name=drl2

pl " Say hello:"
$r -h -n drl3

pl " Say hello:"
$r  --name=drl4 --hello

pl " Print help:"
$r --help

pl " Print man page:"
$r --man

exit 0

producing:
Code:
% ./run4
ksh 93s+

-----
 No parameters:
Your name is 

-----
 Set name, -n:
Your name is drl1

-----
 Set name, --name:
Your name is drl2

-----
 Say hello:
Your name is drl3
Hello drl3!

-----
 Say hello:
Your name is drl4
Hello drl4!

-----
 Print help:
Usage: ./s4 [ options ]
OPTIONS
  -h, --hello     Greets you.
  -n, --name[=given]
                  Your name. The option value may be omitted. The default value
                  is drl0.

-----
 Print man page:
NAME
  ./s4

SYNOPSIS
  ./s4 [ options ]

DESCRIPTION
  Just a little test script

OPTIONS
  -h, --hello     Greets you.
  -n, --name[=given]
                  Your name. The option value may be omitted. The default value
                  is drl0.

See Google results, O'Reilly's Learning the Korn Shell, 2nd Ed., Appendix B ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

2. Shell Programming and Scripting

Using getopts in ksh

I want to use getopts in ksh scripting to obtain multiple values currently im using while getopts vt: opt do case "$opt" in -v) vn=$OPTARG;; -t) tn="$OPTARG";; ;; # terminate while loop esac done however variables vn tn dont store any... (3 Replies)
Discussion started by: E.sh
3 Replies

3. Shell Programming and Scripting

Getopts in the subshell of ksh

Hi, the getopts doesnt seem to be working in the subshell of the ksh. when I echo $@ and $* from the subshell it shows nothing. even when I am capturing the parameters from the outer shell and passing while invoking the file then I am still not getting it properly. the below code is in the... (9 Replies)
Discussion started by: hitmansilentass
9 Replies

4. Shell Programming and Scripting

ksh "getopts" -- Unsetting an Option

I use the "getopts" ksh built-in to handle command-line options, and I'm looking for a clean/standard way to "unset" an option on the command line. I don't know if this is a technical question about getopts or more of a style/standards question. Anyway, I understand that getopts processes its... (4 Replies)
Discussion started by: Matt Miller
4 Replies

5. Shell Programming and Scripting

ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the... (2 Replies)
Discussion started by: kchriste
2 Replies

6. Shell Programming and Scripting

Using getopts

I am having some trouble/questions with getopts that I can't find any solid info on with google I need it to parse things of the syntax of: -r # # # -f -c with as many repeats as possible, and it should catch erroneous commands also, but continue going... my first question is, -r... (3 Replies)
Discussion started by: TurboArkhan
3 Replies

7. Shell Programming and Scripting

ksh and getopts

Hello, I'm writing a script that uses command line options and arguments. To handle these, I'm using getopts. Is there a way to set up an option where an argument is optional? What I'm trying to do is offer -v for verbose output, -vv for increased output -vvv etc. Any suggestions? ... (1 Reply)
Discussion started by: garskoci
1 Replies

8. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

9. Shell Programming and Scripting

ksh: can you use getopts in a function?

I wrote a script that uses getopts and it works fine. However, I would like to put the function in that script in a startup file (.kshrc or .profile). I took the "main" portion of the script and made it a function in the startup script. I source the startup script but it doesn't seem to parse... (4 Replies)
Discussion started by: lyonsd
4 Replies

10. Shell Programming and Scripting

getopts

I have a script which fires a command based on certain parameters. I am posting the code below.. The options needs be given such that -u option goes along with -d and -s, -f goes with -d and -t goes with -s and -d. 1) How do I ensure that user misses any of the option then he should be... (5 Replies)
Discussion started by: yerra
5 Replies
Login or Register to Ask a Question