On Thu, May 14, 2015 at 06:22:56PM -0300, Matias A. Fonzo wrote:
> Not really, what I have is a simple function to check if a variable is
> empty or unset (without distinction)[1] for handling positional
> parameters (arguments, options). If I can do a simple check taking the
> approach of parameter expansion, I would avoid the function call.
>
> [1]
> _checkArgument() {
> if [ "$1" = "" ] ; then # Report empty argument.
> echo "bootstrap: '${2}' requires an argument. See 'bootstrap
> --help'" 1>&2
> exit 1
> fi
> }
>
> while [ "$1" ] ; do
> case "$1" in
> -s)
> stage="$2"
> _checkArgument "$stage" '-s'
> shift 2
> ;;
> *) break;
> esac
> done
That's not bad. I'd do it more like this:
die() {
echo "$1" >&2
echo "usage: ..." >&2
exit 1
}
while [ "$1" ]; do
case $1 in
-s) [ "$2" ] || die "-s option requires an argument"
stage=$2
shift 2
;;
etc.
esac
done