You are not logged in.
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
search the forums/google for getopt or getopts
This silver ladybug at line 28...
Offline
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
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)
//github/
Offline
@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
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
@lolilolicon: Impressive. Have saved it in my scripts folder.
Offline
@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
@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