LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-10-2013, 09:14 AM   #1
Rasna
LQ Newbie
 
Registered: Jul 2012
Location: Bangalore
Posts: 5

Rep: Reputation: Disabled
getopt command handling


Hi,
while using getopt for reading the commandline args for my script I am expecting it to be reading allowed values or else exit with error.
pls find the code getopt.sh
Code:
#!/bin/bash

# getopt.sh example

echo "BEFORE GETOPT: $@";

# Execute getopt
ARGS=$(getopt -o a:b:c -l "ay:,bee:cee" -n "getopt.sh" -- "$@");

#Bad arguments
if [ $? -ne 0 ];
then
echo "ERROR ...."
 exit 1
fi
echo "BEFORE GETOPT: $@";

eval set -- "$ARGS";
So here I am expecting my script will accept args
-a, -b, -c , --ay ,--bee , --cee only and if not it should fail with ERROR.
But it is not failing in that case. How to make my script fail incase of wrong value of args?
OUTPUT:
1: SUCCESS case
# ./getopt.sh -a value
BEFORE GETOPT: -a value
BEFORE GETOPT: -a value
2:FAILURE CASE(SHOULD FAIL!!!)
# ./getopt.sh -am value
BEFORE GETOPT: -am value
BEFORE GETOPT: -am value
3:FAILURE CASE(SHOULD FAIL!!!)
# ./getopt.sh -be value
BEFORE GETOPT: -be value
BEFORE GETOPT: -be value

Thanks in advance
Rasna
 
Old 06-10-2013, 11:37 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
When you invoke as "./getopt.sh -am value", the "m" will be taken as the argument for the "-a" option and "value" is then a non-option positional parameter. The situation is similar for "./getopt.sh -be value".
 
Old 06-11-2013, 05:48 AM   #3
Rasna
LQ Newbie
 
Registered: Jul 2012
Location: Bangalore
Posts: 5

Original Poster
Rep: Reputation: Disabled
The scenarios like
# ./getopt.sh --be value

# ./getopt.sh --ce value

# ./getopt.sh --be value
BEFORE GETOPT: --be value
BEFORE GETOPT: --be value

ideally it should be --cee and --bee.. But still it is accepting succesfully --ce and --be
How to resolve this?
 
Old 06-11-2013, 09:04 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by Rasna View Post
ideally it should be --cee and --bee.. But still it is accepting succesfully --ce and --be
How to resolve this?
The manpage for getopt specifically states:
"Long options may be abbreviated, as long as the abbreviation is not ambiguous."
One way to block that behavior would be to declare another option that differs only in the final letter, e.g. "ceX" and "beX", and then handle those option as errors.
 
1 members found this post helpful.
Old 06-11-2013, 09:13 AM   #5
Rasna
LQ Newbie
 
Registered: Jul 2012
Location: Bangalore
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you very much !
 
  


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
LXer: Handling Command Submission For The Intel DRM Driver LXer Syndicated Linux News 0 11-02-2012 08:10 AM
LXer: Bash: Handling Command Not Found LXer Syndicated Linux News 1 04-24-2010 09:07 AM
Exim4 can't start - getopt: command not found TheCat Linux - Newbie 1 04-23-2009 01:21 AM
alsaconf returns error msg "getopt: command not found" bezdomny Linux - Hardware 4 07-13-2007 10:20 AM
command line handling folder/filenames Basslord1124 Linux - Newbie 1 03-25-2005 10:43 AM


All times are GMT -5. The time now is 08:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration