LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 08: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, 10:37 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
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, 04: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, 08:04 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
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, 08:13 AM   #5
Rasna
LQ Newbie
 
Registered: Jul 2012
Location: Bangalore
Posts: 5

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


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

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

All times are GMT -5. The time now is 02:47 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