You are not logged in.

#1 2009-11-03 13:17:07

chrispoole
Member
Registered: 2008-12-30
Posts: 121

Testing for flags with bash shell script?

I want to make a simple script, myscript.sh, accept some options, like

myscript.sh -k 'hello' -o 'output.html',

that kind of thing. Is there a standard, clean way of doing this (BASH support)?

I'm new to shell scripting and can only think of something esoteric with $1, $2 variables, etc.

Offline

#2 2009-11-03 13:22:14

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Testing for flags with bash shell script?

search the forums/google for getopt or getopts


This silver ladybug at line 28...

Offline

#3 2009-11-03 14:49:42

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Testing for flags with bash shell script?

getopt(s) is messy though, right? I never really got it.

You could try this too:

until [ -z "$1" ]; do
  case "$1" in
    -k|--keyword) shift; keyword="$1"; shift ;;
    -o|--output) shift; output="$1"; shift ;;
  esac
done

Offline

#4 2009-11-03 14:57:07

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Testing for flags with bash shell script?

i like to do it this way (i agree, getopts is fugly).

while true; do
  case $1 in
    -k) keyword="$2"; shift 2 ;;
    -o) output="$2"; shift 2 ;;
    *)  break ;;
  esac
done

basically the same thing.  just my preference, to each his own.

Last edited by brisbin33 (2009-11-03 14:58:41)

Offline

#5 2009-11-03 15:43:17

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Testing for flags with bash shell script?

@Procyon
Both getopt and getopts fit my daily use quite well. I use getopt a lot and getopts if I'm too lazy to type/don't care about long options.

Your code above will keep on waiting if you run the script with extra arguments, say...`./myscript.sh -k key -o out extra`
Let me follow your idea:

until [ -z "$1" ]; do
  case "$1" in
    -k|--keyword) shift; keyword="$1"; shift ;;
    -o|--output) shift; output="$1"; shift ;;
    -*) shift ;;
    *) break ;;
  esac
done
echo "$keyword $output"
echo "$@"

This time you can run it like `./myscript.sh -k key -o out extra`. But you cannot run it like `./myscript.sh extra -k key -o out` since the loop breaks out at 'extra'
With getopt, this limitation does not exist, i.e. order does not matter.
Then I have an idea around this:

args=()
until [ -z "$1" ]; do
  case "$1" in
    -k|--keyword) keyword="$2"; shift 2 ;;
    -o|--output)  output="$2";  shift 2 ;;
    --) shift ; break ;;
    -*) echo "invalid option $1" 1>&2 ; shift ;; # or, error and exit 1 just like getopt does
    *) args+=("$1") ; shift ;;
  esac
done
echo "keyword: $keyword ; output: $output"
echo '$@:' $@
args+=("$@")
for arg in "${args[@]}" ; do
    echo "++ ${arg}"
done

run it like this to see what it does:

./myscript.sh x -k key y --output out --long -- z -short
invalid option --long
keyword: key ; output: out
$@: z -short
++ x
++ y
++ z
++ -short

pretty good a result huh?


This silver ladybug at line 28...

Offline

#6 2009-11-03 16:08:08

chrispoole
Member
Registered: 2008-12-30
Posts: 121

Re: Testing for flags with bash shell script?

Thanks guys; getopt was what I was remembering in the back of my mind but had forgotten.

And for this quick hack of a script, I may well just use the case syntax above.

Offline

#7 2009-11-03 17:51:34

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Testing for flags with bash shell script?

@lolilolicon: Impressive. Have saved it in my scripts folder.

Offline

#8 2009-11-04 05:12:36

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Testing for flags with bash shell script?

@brisbin33 @Procyon @lolilolicon -- not to hijack this thread but  where did you guys learn bash?  What reference(s) would you recommend?  I get most of my knowledge from #bash on freenode.org and http://mywiki.wooledge.org/BashGuide  I'm curious to know where you guys go.

Offline

#9 2009-11-04 05:51:50

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,403
Website

Re: Testing for flags with bash shell script?

@steve___: I recommend: http://tldp.org/LDP/abs/html/

Just as an FYI, makepkg uses a custom option parser that is a bash implementation getopt (which was not a portable as hoped...).

Offline

Board footer

Powered by FluxBB