Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Bash: how to handle --options?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
Humbletech99
Posts: 365
Joined: 2005-12-29 00:03

Bash: how to handle --options?

#1 Post by Humbletech99 »

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?
The Human Equation:

value(geeks) > value(mundanes)

User avatar
njkt
Posts: 39
Joined: 2006-07-29 08:43
Location: California

#2 Post by njkt »


ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#3 Post by ajdlinux »

Yes, that link is extremely relevant for shell scripting...
On mod's request, I'll rephrase more nicely :) :

The link provided is for C programming and not really relevant to shell scripting. The way I would do it is in a post below.
Last edited by ajdlinux on 2006-09-04 07:04, edited 1 time in total.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au

User avatar
njkt
Posts: 39
Joined: 2006-07-29 08:43
Location: California

#4 Post by njkt »

Nice use of sarcasm ;-)

My bad, but I like your suggestion too.

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#5 Post by ajdlinux »

The built in getopt is rather limited unfortunately.

In these instances I would use Python or another scripting language to do it in.

BTW, nice use of sarcasm ;)
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#6 Post by ajdlinux »

BTW, here's an example of processing this in Python:

Code: Select all

#!/usr/bin/python

import sys, string, os

dangerous_opts = string.find(sys.argv," --dangerous-option") or string.find(sys.argv,"-rf")

if not dangerous_opts:
    os.system("my_program "+sys.argv[1:])
Haven't tried this code yet but it should work.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au

User avatar
Humbletech99
Posts: 365
Joined: 2005-12-29 00:03

#7 Post by Humbletech99 »

well, because what I'm doing is simple I've just parsed it myself, it's smaller and faster than python. Since this is just a wrapper, it needed to just disallow a couple of switches which is easier done just testing them quickly and bailing with an appropriate warning if found.

otherwise python would have been my next stop.

I'm sure that there is another way of doing this in shell, it's just that most people never get deep enough into it, I'll still keep an eye out for a better way to fully parse --long-options the way OptParse does for python.
The Human Equation:

value(geeks) > value(mundanes)

Post Reply