help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] ${var:?message} customization


From: Greg Wooledge
Subject: Re: [Help-bash] ${var:?message} customization
Date: Thu, 14 May 2015 17:30:07 -0400
User-agent: Mutt/1.4.2.3i

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]