Skip to content

Instantly share code, notes, and snippets.

@hgraca
Last active March 11, 2021 15:28
Show Gist options
  • Save hgraca/9ce79225a08e2d2e947f33bd13db9cd9 to your computer and use it in GitHub Desktop.
Save hgraca/9ce79225a08e2d2e947f33bd13db9cd9 to your computer and use it in GitHub Desktop.
bash-examples
#!/usr/bin/env bash
SCRIPT_PATH="$(
cd "$(dirname "$0")" || exit >/dev/null 2>&1
pwd -P
)"#!/usr/bin/env bash
#################################
# Named arguments for base scripts
#################################
ARG_1='' ARG_2='' ARG_3='' ARG_4="4th arg"
eval "${@}"
ARG_3=${ARG_3:-"$(echo $PWD)"}
echo "ARG_1=${ARG_1}"
echo "ARG_2=${ARG_2}"
echo "ARG_3=${ARG_3}"
echo "ARG_4=${ARG_4}"
if [ "${ARG_1}" == "" ]; then
echo 'ARG_1 == ""'
else
echo 'ARG_1 != ""'
fi
if [ "${ARG_2}" == "" ]; then
echo 'ARG_2 == ""'
else
echo 'ARG_2 != ""'
fi
if [ "${ARG_3}" == "" ]; then
echo 'ARG_3 == ""'
else
echo 'ARG_3 != ""'
fi
if [ "${ARG_4}" == "" ]; then
echo 'ARG_4 == ""'
else
echo 'ARG_4 != ""'
fi
#################################
. lib.sh
addToStartOfTrapCommand "exit 1"
function help() {
echo "Usage: cmd <ARG> [-h] [-r]"
echo "Options:"
echo " -h Show this help"
echo " -r Remove the obsolete deployment after the new deployment takes its place (as opposed to only when a new deployment is triggered)"
}
if [ "$#" -ne 1 ]; then
echo "Invalid number of arguments, $# given but 1 expected."
echo ""
help
exit 1
fi
ARG=${1}
addToStartOfTrapCommand "exit 1"
export TOP_PID=$$
echo "Initial OPTIND: ${OPTIND}" # OPTIND contains the index of the next option to handle
# Prepend the list of valid options with ':' to disable the default error handling of invalid options.
# Options that themselves have arguments are followed by a ':'.
# The argument to an option is placed in the variable 'OPTARG'.
while getopts ":ab:ch" opt; do
case ${opt} in
a)
echo "Option a"
echo "OPTIND: ${OPTIND}"
echo "'hostname':"
hostname # the actual hostname
echo "'echo hostname':"
echo hostname # hostname
echo "'echo \"\$(hostname)\"':"
echo "$(hostname)" # the actual hostname
echo "'getHostname':"
getHostname # the actual hostname
echo "'echo getHostname':"
echo getHostname # getHostname
echo "'echo \"\$(getHostname)\"':"
echo "$(getHostname)" # the actual hostname
echo "'HOSTNAME=\$(getHostname) && echo \${HOSTNAME}':"
HOSTNAME=$(getHostname)
echo ${HOSTNAME} # the actual hostname
;;
b)
target=$OPTARG
echo "Option b: ${target}"
echo "OPTIND: ${OPTIND}"
;;
c)
echo "Option c"
echo "OPTIND: ${OPTIND}"
;;
h)
help
echo "OPTIND: ${OPTIND}"
exit 0
;;
\?) # If an invalid option is provided, the option variable is assigned the value ?.
echo "Invalid option: $OPTARG"
echo "OPTIND: ${OPTIND}"
help
exit 1
;;
:)
echo "Invalid option: $OPTARG requires an argument"
echo "OPTIND: ${OPTIND}"
help
exit 2
;;
esac
done
# It is common practice to call the shift command at the end of your processing loop
# to remove options that have already been handled from $@.
shift $((OPTIND - 1))
echo "Final OPTIND: ${OPTIND}"
echo "ARG=${ARG}"
echo "Trap command: $(getCurrentTrapCommand "TERM")"
echo "This script path: ${SCRIPT_PATH}"
#
# Named arguments in Bash
#
# Synopsis: someFunction ARG_1="..." ARG_2="..." ARG_3="..." ARG_4="..."
#
function someFunction() {
echo "${FUNCNAME[0]}" "$@"
local ARG_1 ARG_2 ARG_3 ARG_4="4th arg"
local "${@}"
ARG_3=${ARG_3:-"$(echo $PWD)"}
echo "ARG_1=${ARG_1}"
echo "ARG_2=${ARG_2}"
echo "ARG_3=${ARG_3}"
echo "ARG_4=${ARG_4}"
if [ "${ARG_1}" == "" ]; then
echo 'ARG_1 == ""'
else
echo 'ARG_1 != ""'
fi
if [ "${ARG_2}" == "" ]; then
echo 'ARG_2 == ""'
else
echo 'ARG_2 != ""'
fi
if [ "${ARG_3}" == "" ]; then
echo 'ARG_3 == ""'
else
echo 'ARG_3 != ""'
fi
if [ "${ARG_4}" == "" ]; then
echo 'ARG_4 == ""'
else
echo 'ARG_4 != ""'
fi
}
someFunction ARG_1="1st arg"
#!/usr/bin/env bash
export TOP_PID=$$
#
# Synopsis: addToStartOfTrapCommand ${NEW_COMMAND} ${SIGNAL}
#
function addToStartOfTrapCommand() {
local NEW_COMMAND
NEW_COMMAND=${1}
local SIGNAL
SIGNAL=${2:-"TERM"}
local CURRENT_TRAP_COMMAND
CURRENT_TRAP_COMMAND=$(getCurrentTrapCommand "${SIGNAL}")
trap "${NEW_COMMAND}; ${CURRENT_TRAP_COMMAND}" "${SIGNAL}"
}
#
# Synopsis: assertEquals "${CONTENT_A}" "${CONTENT_B}"
#
function assertEquals() {
local CONTENT_A
CONTENT_A=$(trim "${1}")
local CONTENT_B
CONTENT_B=$(trim "${2}")
echo "assertEquals ${CONTENT_A} ${CONTENT_B}"
if [ "${CONTENT_A}" != "${CONTENT_B}" ]; then
echo "Assertion failed: '${CONTENT_A} != ${CONTENT_B}'"
kill -s TERM ${TOP_PID} # This will exit the main script and trigger the trap
fi
}
#
# Synopsis: getCurrentTrapCommand ${SIGNAL}
#
function getCurrentTrapCommand() {
local SIGNAL
SIGNAL=${2:-"TERM"}
extract_trap_cmd() { printf '%s\n' "$3"; }
eval "extract_trap_cmd $(trap -p "${SIGNAL}")"
}
#
# Synopsis: getHostname
#
function getHostname() {
hostname # doing 'echo "$(hostname)"' would be doing a redundant echo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment