General notes on using and scripting Bash.
Here's a nice article on the topic of argument parsing in Bash: Bash: Argument Parsing
From the article:
#!/bin/bash
PARAMS=""
while (( "$#" )); do
case "$1" in
-f|--flag-with-argument)
FARG=$2
shift 2
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
Inspired by the first script found here: Updated Pi Zero Gadgets
A nice explanation of the variable expansion being used: variable expansion in curly braces
#!/bin/bash
op="${1?Must specify op}"
mac="${2:-mac}"
ip="${3:-ip}"
host="${4}"
echo $op
echo $mac
echo $ip
echo $host
How can I declare and use Boolean variables in a shell script?
A nice example from: dotfiles/zsh/.zshrc
case `uname` in
Darwin)
source $HOME/.zshrc-mac
;;
Linux)
source $HOME/.zshrc-linux
;;
FreeBSD)
# commands for FreeBSD go here
;;
esac
outfilename="db.$(date +%Y%m%d_%H%M%S).csv.gz"