LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 08-31-2006, 11:15 AM   #1
humbletech99
Member
 
Registered: Jun 2005
Posts: 374

Rep: Reputation: 30
Bash: how to handle --options?


I've tried to handle --options in the long gnu style format using the builtin getopts but it doesn't like it.

I know the external getopt does have an -l switch for this but the external is deprecated and I'd rather avoid using it.

I'm creating a wrapper script. What I need it to be able to take any number of arguments and then if a certain switch or two is passed, to tell the user off and exit 1. But I want everything to pass through to the program if one of the dangerous switches wasn't passed. So it needs to be able to not croak when a user passes --help to the underlying program.


Any ideas?
 
Old 08-31-2006, 01:19 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,853

Rep: Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121
the standard getopt in bash does handle long options just fine, one of the standard examples that comes with it in the util-linux package on my fc5 system is...
Code:
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
     -n 'example.bash' -- "$@"`

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

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
        case "$1" in
                -a|--a-long) echo "Option a" ; shift ;;
                -b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;;
                -c|--c-long)
                        # c has an optional argument. As we are in quoted mode,
                        # an empty parameter will be generated if its optional
                        # argument is not found.
                        case "$2" in
                                "") echo "Option c, no argument"; shift 2 ;;
                                *)  echo "Option c, argument \`$2'" ; shift 2 ;;
                        esac ;;
                --) shift ; break ;;
                *) echo "Internal error!" ; exit 1 ;;
        esac
done
echo "Remaining arguments:"
for arg do echo '--> '"\`$arg'" ; done
 
Old 08-31-2006, 06:42 PM   #3
humbletech99
Member
 
Registered: Jun 2005
Posts: 374

Original Poster
Rep: Reputation: 30
err, getopt is not technically bash, it's an external. And yes it can handle long options but it's older and deprecated in favour of the getopts internal builtin.

it looks to me like you're still parsing it manually.

thanks for the tip, I will try this out.

since I'm just writing a wrapper to stop a couple flags going through to a command, I've done a quick and nice looking thing.
Code:
params="$@"

until [ -z $1 ]
    do
    if [ "$1" == "-d" ]
       then
       echo "you are not allowed to use the -d switch... blah blah" >&2
       exit 1
    fi
    etc
    shift
done

command "$params"
 
Old 10-07-2011, 07:20 AM   #4
Arvid Requate
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
Actually the Bash builtin getopts function can be used to parse long options by putting a dash character followed by a colon into the optspec. For an example script see:

http://stackoverflow.com/questions/4...680682#7680682
 
Old 10-07-2011, 07:21 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,853

Rep: Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121
over 5 years old. no more replies please.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 5 10-26-2011 01:52 PM
Execution options in bash eremit Programming 6 03-28-2005 10:00 PM
editing bash to auto activate some options? kintaroe Linux - Newbie 1 10-02-2004 02:07 PM
Kernel 2.6.2 options question - LOCKED options ? tvojvodi Linux - General 0 02-17-2004 04:23 AM
vmware- can it handle?? N_A_J_M Linux - General 2 09-23-2003 10:19 PM


All times are GMT -5. The time now is 03:02 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration