LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-05-2012, 09:51 PM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Rep: Reputation: 0
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.
 
Old 06-05-2012, 09:56 PM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
This is a pretty good explanation:

http://www.mkssoftware.com/docs/man1/getopts.1.asp
 
Old 06-05-2012, 10:31 PM   #3
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
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
 
Old 06-06-2012, 12:10 AM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
Old 06-06-2012, 07:05 AM   #5
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
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
 
Old 06-06-2012, 07:30 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,434

Rep: Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790
http://docs.oracle.com/cd/E19253-01/...s-1/index.html
 
Old 06-07-2012, 06:21 AM   #7
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
Chris - I have tried the way it was shown in Oracle Docs... but its not working...


#!/bin/ksh

#getopts "ffile)(input-file)ooutput-file)"
while getopts "afile)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....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] getopts is not considering (skipping) one of the options akhand jyoti Linux - Newbie 3 11-18-2011 01:44 PM
[SOLVED] Script to get the word count of a paragraph from a long message lakshminarayanan Linux - Newbie 7 11-10-2011 01:07 AM
Getting word too long error while sourcing csh file Kotresh gowda Linux - Newbie 1 08-12-2011 06:49 PM
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 06:38 AM
Word too long error at login arobic Linux - Newbie 1 08-19-2003 07:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration