# safety
set -euo pipefail
# root directory
ROOT_DIR="$(cd $(dirname $0)/.. ; pwd)"
# directory of this script
CURRENT_DIR="$(cd $(dirname $0) ; pwd)"
# null checks
[ -z "$FOO" ] # null
[ -n "$FOO" ] # not null
# Use variable expansion only for side-effects
: "${var:=$1}"
# Literal special characters
$'\t'
$'\n'
# etc.
# get options http://wiki.bash-hackers.org/howto/getopts_tutorial
# When you want getopts to expect an argument for an option, just
# place a : (colon) after the proper option flag. If you want -A
# to expect an argument (i.e. to become -A SOMETHING) just do:
# getopts fA:x VARNAME
while getopts ":e:" opt; do
case "${opt}" in
e)
ENVIRONMENT=${OPTARG}
[ $ENVIRONMENT == "sandbox" -o $ENVIRONMENT == "production" ] || usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2 ; usage
;;
esac
done
shift $((OPTIND-1))
# usage
usage () {
echo "Usage: $(basename $0) [-e <sandbox|production>] [<script>]" 1>&2
exit 1
}
#fail
fail () {
echo "$@"
exit 1
}
# prompt for y/n
echo
echo -n "Add user to \"admins\" group (y/n)? "
old_stty_cfg=$(stty -g)
stty raw -echo
ANSWER=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done )
stty $old_stty_cfg
echo $ANSWER
# prompt for other input
while [ "$USER_PASSWORD" = "" ]; do
# -s is "secure", because this is a password
read -s -p "Enter a password for the user: " USER_PASSWORD
done
# increment a variable
var=$((var+1))
((var=var+1))
((var+=1))
((var++))
# bind keybindings to history-search
# when they're not in your .inputrc
bind "\e[A": history-search-backward # arrow up
bind "\e[B": history-search-forward # arrow down
See also Commands For History (Bash Reference Manual)
# display keybindings
bind -p
# They can be queried and removed! [8 Linux Bash Shell Readline Bind Command Examples](https://linux.101hacks.com/unix/bind/)
# Previous commands
# !^ first argument
# !$ last argument
# !* all arguments
# !:2 second argument
# !:2-3 second to third arguments
# !:2-$ second to last arguments
# !:2* second to last arguments
# !:2- second to next to last arguments
# !:0 the command
# !! repeat the previous line