Skip to content

Instantly share code, notes, and snippets.

@hsteinshiromoto
Created March 2, 2020 01:41
Show Gist options
  • Save hsteinshiromoto/b99fc0ca8c1ef94bf59870be21114276 to your computer and use it in GitHub Desktop.
Save hsteinshiromoto/b99fc0ca8c1ef94bf59870be21114276 to your computer and use it in GitHub Desktop.
Bash script to run functions using arguments
# Documentation
display_help() {
echo "Usage: [variable=value] $0" >&2
echo
echo " -m, --my_function run my_function"
echo " -h, --help display help"
echo
# echo some stuff here for the -a or --add-options
exit 1
}
default_function() {
VAR = "var"
}
my_function() {
default_function
echo ${VAR}
}
# Available options
while :
do
case "$1" in
-h | --help)
display_help # Call help function
exit 0
;;
-m | --my_function)
my_function # Call custom function
break
;;
"")
default_function # Call default function
break
;;
--) # End of all options
shift
break
;;
-*)
echo "Error: Unknown option: $1" >&2
## or call function display_help
exit 1
;;
*) # No more options
break
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment