Skip to content

Instantly share code, notes, and snippets.

@mike-burns
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-burns/9327099 to your computer and use it in GitHub Desktop.
Save mike-burns/9327099 to your computer and use it in GitHub Desktop.
HQ9+ in sh
#!/bin/sh
main() {
hello_world=0
quine=0
nine=0
plus=0
accumulator=0
while getopts hq9+ opt; do
case "$opt" in
h)
echo hello, world
;;
q)
cat $0
;;
9)
echo Ninety nine bottles of beer on the wall
echo Ninety nine bottles of beer
echo etc.
;;
+)
accumulator=$(($accumulator + 1))
;;
esac
done
}
main "$@"
#!/bin/sh
hello_world=0
quine=0
nine=0
plus=0
accumulator=0
bug_args=
while getopts hq9+ opt; do
case "$opt" in
h)
bug_args="-h $bug_args"
;;
q)
bug_args="-q $bug_args"
;;
9)
bug_args="-9 $bug_args"
;;
+)
bug_args="-+ $bug_args"
;;
esac
done
saved_IFS=$IFS
IFS='
'
for i in $(./bug $bug_args); do
echo $i
done
@pbrisbin
Copy link

pbrisbin commented Mar 3, 2014

I would expect this to fail. "$var" is not the same as "$@" or "$*".

That's why, in our case we must to lsrc $LS_ARGS and not lsrc "$LS_ARGS"

@mike-burns
Copy link
Author

Got it: it was the IFS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment