Created
March 2, 2020 01:41
-
-
Save hsteinshiromoto/b99fc0ca8c1ef94bf59870be21114276 to your computer and use it in GitHub Desktop.
Bash script to run functions using arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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