Last active
August 24, 2019 01:57
-
-
Save vietvudanh/5aa04d0e68069d675d80570cd42aab38 to your computer and use it in GitHub Desktop.
manage script in bash
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
#!/usr/bin/env bash | |
# template for maange script in bash | |
# provide listing functions / usage.. etc | |
CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
# START vars | |
LOG_FILE="$CWD/log.log" | |
# END vars | |
# START functions declare | |
function usage { | |
echo "$(basename $0), params: [$(echo $LIST_FUNCS)]" | |
} | |
function log { | |
echo "`date` $1" | tee -a $LOG_FILE | |
} | |
# define custom functions here | |
LIST_FUNCS=$(declare -F | cut -d ' ' -f3 | sed 's/usage\|log//') | |
# END functions declare | |
# main body | |
case $# in | |
1) # i just need one parmas | |
if [[ -n "`echo $LIST_FUNCS | xargs -n1 echo | grep -e \"^$1$\"`" ]]; then | |
echo "'$1' match" | |
echo "run \`$1\`" | |
type $1 | |
# run, using name of function provied in CLI args | |
$1 | |
else | |
echo "'$1' not match" | |
usage | |
fi | |
;; | |
*) | |
usage | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment