Skip to content

Instantly share code, notes, and snippets.

@auramo
Created January 10, 2014 05:31
Show Gist options
  • Save auramo/8347434 to your computer and use it in GitHub Desktop.
Save auramo/8347434 to your computer and use it in GitHub Desktop.
Command line flags for bash
#!/bin/bash
set -e
command="$1"
eka() {
local use_x_flag=false
while getopts ":x:" o; do
case "${o}" in
x)
xarg=${OPTARG}
echo "$xarg"
use_x_flag=true
;;
*)
echo "invalid param for eka"
exit 1
;;
esac
done
echo "x flag $use_x_flag"
if $use_x_flag ; then
echo "X FLAG"
else
echo "NO X FLAG"
fi
}
toka() {
echo "toka"
local yflag=false
while getopts ":y" o; do
case "${o}" in
y)
yflag=true
;;
*)
echo "invalid param for toka"
exit 1
;;
esac
done
echo "yflag $yflag"
if $yflag ; then
echo "Y FLAG"
else
echo "NO Y FLAG"
fi
}
case "$command" in
"eka" )
eka "${@:2}"
;;
"toka" )
toka "${@:2}"
;;
*)
echo "Unknown command $command"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment