Skip to content

Instantly share code, notes, and snippets.

@ymkjp
Last active May 10, 2018 22:22
Show Gist options
  • Save ymkjp/2e50a332fe6330d7f732324279590045 to your computer and use it in GitHub Desktop.
Save ymkjp/2e50a332fe6330d7f732324279590045 to your computer and use it in GitHub Desktop.
Handy Command
#!/usr/bin/env bash
# Place this command to an arbitrary path e.g. bin/example.bash
# Grant a permission
# chmod u+x bin/ex.bash
# Put a symlink without extension into top of project
# ln -s bin/ex.bash ./ex
# Execute the command
# ./ex init
# Use environment variable to pass arguments (Might better not to make the script such complicated)
# FOO_DIR="~/sample_path/" ./ex download
function ex() {
set -u
local SELF="$(basename $0)"
local NAME="Example Command Desctiption"
local UTIME="$(date +'%Y%m%d')"
local WORK_DIR="$(pwd)"
local PID_PATH="${WORK_DIR}/var/.pids"
init() {
echo "Do some initializations here"
}
__show-message() {
echo "This method doesn't show up in usage if you put a prefix __ (double underscore)"
}
logs() {
tail -F logs/*.log
}
download() {
rsync -avzP --bwlimit=8000 example.com:/path/ ${FOO_DIR:=${WORK_DIR}}
}
usage() {
echo -e "${SELF} -- ${NAME}\nUsage: ${SELF} [sub-command]\n[Sub-commands]:"
declare -F | awk '{print "\t" $3}' | grep -v ${SELF} | grep -vE '^\t__[^_]+'
}
if [[ $# = 0 ]]; then
usage
echo "INFO: ${SELF} requires a sub-command"
elif [[ "$(type -t $1)" = "function" ]]; then
$1
else
usage
fi
}
ex $1
@ymkjp
Copy link
Author

ymkjp commented May 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment