Created
February 13, 2020 02:30
-
-
Save ryu1/26e2f3afc1a0c7a5608ef84735afe448 to your computer and use it in GitHub Desktop.
Shell Template
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
#!/bin/bash | |
# ------------------------------------------------------------------ | |
# [Author] Title | |
# Description | |
# ------------------------------------------------------------------ | |
SUBJECT=some-unique-id | |
VERSION=0.1.0 | |
USAGE="Usage: command -hv args" | |
# --- Option processing -------------------------------------------- | |
while getopts ":vh" optname | |
do | |
case "$optname" in | |
"v") | |
echo "Version $VERSION" | |
exit 0; | |
;; | |
"h") | |
echo $USAGE | |
exit 0; | |
;; | |
"?") | |
echo "Unknown option $OPTARG" | |
exit 0; | |
;; | |
":") | |
echo "No argument value for option $OPTARG" | |
exit 0; | |
;; | |
*) | |
echo "Unknown error while processing options" | |
exit 0; | |
;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
cmd=$1 | |
param=$2 | |
command="command_$1" | |
# ----------------------------------------------------------------- | |
LOCK_FILE=/tmp/${SUBJECT}.lock | |
if [ -f "$LOCK_FILE" ]; then | |
echo "Script is already running" | |
exit | |
fi | |
# ----------------------------------------------------------------- | |
trap "rm -f $LOCK_FILE" EXIT | |
touch $LOCK_FILE | |
# ----------------------------------------------------------------- | |
function command_test { | |
echo "test" | |
} | |
function command_ping { | |
echo "ping $param" | |
} | |
# ----------------------------------------------------------------- | |
# ----------------------------------------------------------------- | |
if [ -n "$(type -t ${command})" ] && [ "$(type -t ${command})" = function ]; then | |
${command} | |
else | |
echo "'${cmd}' is NOT a command"; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment