LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2015, 07:40 PM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Rep: Reputation: 0
Question on getops --long


Please Help me on this all....

I need to write a shell script with getopts long format taking couple arguments.
You can use either bash/ksh
I am using RHEL6

Code:
OPTS=`getopt -o vhns: --long verbose,dry-run,help,stack-size: -n 'parse-options' -- "$@"`
#`OPTS=`getopt -o vhn: --long verbose,dry-run,help: -n 'parse-options' -- "$@"`

if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi

echo "$OPTS"
eval set -- "$OPTS"

VERBOSE=false
HELP=false
DRY_RUN=false
STACK_SIZE=0

while true; do
case "$1" in
-v | --verbose )        VERBOSE=true; shift ;;
-h | --help )           HELP=true; shift ;;
-n | --dry-run )        DRY_RUN="$2"; shift 2;;
-s | --stack-size )     STACK_SIZE="$2"; shift 3;;
-- ) shift; break ;;
* ) break ;;
esac
done

echo VERBOSE=$VERBOSE
echo HELP=$HELP
echo DRY_RUN=$DRY_RUN
echo STACK_SIZE=$STACK_SIZE
=======================================
When executing ...
$ ./test2 -s ABCD
-s 'ABCD' --
VERBOSE=false
HELP=false
DRY_RUN=false
STACK_SIZE=ABCD #Here I can see the output...

$ ./test2 -n ABCD
-n -- 'ABCD'
VERBOSE=false
HELP=false
DRY_RUN=-- #Here I am not able to see the output.. this is supposed to get value of ABCD to DRY_RUN
STACK_SIZE=0

Please help me fix this
 
Old 06-03-2015, 01:02 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
replace your OPTS variable
Code:
OPTS=`getopt -o vhn:s: --long verbose,help,dry-run:,stack-size: -n 'test.sh' -- "$@"`
 
Old 06-14-2015, 08:10 PM   #3
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
Tried what ever you asked but partially it helps not completly. Please see below.

Code:
./abcd.ksh  -n 2 -s 3
 -n '2' -s '3' --
VERBOSE=false
HELP=false
DRY_RUN=2     #It worked.
STACK_SIZE=3  #It worked.

#Tried changing the order it failed.

./abcd.ksh  -s 2 -n 3
 -s '2' -n '3' --
VERBOSE=false
HELP=false
DRY_RUN=false    #It Failed.
STACK_SIZE=2     #It Worked.
Not sure why it only works for S but not for N option.
 
Old 06-14-2015, 09:04 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Code:
-n | --dry-run )        DRY_RUN="$2";    shift 2;;
-s | --stack-size )     STACK_SIZE="$2"; shift 3;;
Do you see your error?
 
Old 06-15-2015, 08:26 AM   #5
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
No i dont see any error. If you feel there is one .. can you please correct. In sheer of completing the script.

your help is appreciated.
 
Old 06-15-2015, 08:43 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Try:

Code:
-s | --stack-size )     STACK_SIZE="$2"; shift 2;;
 
Old 06-15-2015, 08:44 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
dry-run needs no argument, therefore shift 2 is not ok, but I'm not really sure what do you really want, probably there is an argument
STACK_SIZE needs 1 argument, therefore shift 3 is definitely wrong, you need shift 2.
 
Old 06-16-2015, 07:30 PM   #8
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
DRY_RUN="$2" #this means we need an argument correct ?
 
Old 06-17-2015, 12:35 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
for me it is completely irrelevant if you want to have it an argument. But actually I have no idea what is that good for. But that does not mean it is not ok, just I can't understand.
 
Old 06-17-2015, 09:56 AM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by maddyfreaks View Post
Code:
OPTS=`getopt -o vhns: --long verbose,dry-run,help,stack-size: -n 'parse-options' -- "$@"`
In your getopt call you indicate that "dry-run" does not take an argument (no trailing ":"). Later in your script, you handle it like it does have an argument. That is not going to work.
 
Old 06-17-2015, 10:49 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
The OP indicated that an argument was desired so that is why a4z added the :. Since an argument is desired $2 is required.

Code:
OPTS=`getopt -o vhn:s: --long verbose,help,dry-run:,stack-size: -n 'test.sh' -- "$@"`
 
Old 06-18-2015, 06:03 PM   #12
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
I need to have an argument to dry-run. As said after pointing out ...
Here is the updated script.

Code:
#!/bin env ksh
OPTS=`getopt -o vhn:s: --long verbose,dry-run,help,stack-size: -n 'parse-options' -- "$@"`

if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi

echo "$OPTS"
eval set -- "$OPTS"

VERBOSE=false
HELP=false
DRY_RUN=false
STACK_SIZE=0

while true; do
case "$1" in
-v | --verbose )        VERBOSE=true; shift ;;
-h | --help )           HELP=true; shift ;;
-n | --dry-run )        DRY_RUN="$2"; shift 2;;
-s | --stack-size )     STACK_SIZE="$2"; shift 3;;
-- ) shift; break ;;
* ) break ;;
esac
done

echo VERBOSE=$VERBOSE
echo HELP=$HELP
echo DRY_RUN=$DRY_RUN
echo STACK_SIZE=$STACK_SIZE
####################################
When i change the order of the passing arguments.. output is not desirable.

Code:
$ ./testcase.ksh -n 2 -s 3
 -n '2' -s '3' --
VERBOSE=false
HELP=false
DRY_RUN=2
STACK_SIZE=3
#the above works good.
Code:
$ ./testcase.ksh -s 2 -n 3
 -s '2' -n '3' --
VERBOSE=false
HELP=false
DRY_RUN=false
STACK_SIZE=2
##This failes.. though i passed Dry_run = 2 it went for other varaiable to stack-size.

Last edited by maddyfreaks; 06-18-2015 at 06:04 PM.
 
Old 06-18-2015, 06:44 PM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
You still did not correct your script per our posts 6 and 7.
 
  


Reply



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] Question about long long int in C smag Programming 2 10-18-2011 03:50 PM
Need help with getops Fatking Programming 1 12-04-2010 09:46 PM
A Long Story, A Simple Question! mdoubledragon Linux - Newbie 4 06-04-2005 12:33 AM
Long filename question on burning CD satimis Linux - General 3 05-22-2004 04:06 AM
Long filename access question Taking Back Linux - Newbie 2 03-25-2003 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 PM.

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