TIL: bash flags
Fri, Aug 26, 2022
One-minute read
TIL: bash flags
TIL how to create flags/ optional arguments in bash scripts. Previously I just used “${1} ${2}” etc.
error()
{
echo "$arg0: $*" >&2
exit 1
}
flags()
{
while test $# -gt 0
do
case "$1" in
(-s|--source)
shift
[ $# = 0 ] && error "No source directory specified"
export SOURCE="$1"
shift;;
(-d|--destination)
shift
[ $# = 0 ] && error "No destination specified"
export DESTINATION="$1"
shift;;
(-c|--credentials)
shift
[ $# = 0 ] && error "No credentials specified"
export CREDENTIALS="$1"
shift;;
(-b|--bandwidth)
shift
[ $# = 0 ] && error "No bandwidth specified"
export BANDWIDTH="$1"
shift;;
(-t|--timeout)
shift
[ $# = 0 ] && error "No timeout specified"
export TIMEOUT="$1"
shift;;
(-p|--port)
shift
[ $# = 0 ] && error "No port specified"
export PORT="$1"
shift;;
(-l|--compression-level)
shift
[ $# = 0 ] && error "No compression level specified"
export COMPRESS_LEVEL="$1"
shift;;
(-h|--help)
help;;
# (-V|--version)
# version_info;;
(*) usage;;
esac
done
}
flags "$@"
Tags:
#bash #til