Gaetan Nadon <[email protected]> (29/12/2010):
> +# perform sanity checks on cmdline args which require arguments
> +# arguments:
> +#   $1 - the option being examined
> +#   $2 - the argument to the option
> +# returns:
> +#   if it returns, everything is good
> +#   otherwise it exit's
> +required_arg() {
> +    # preconds
> +    if [ X"$1" = X ]; then
> +     echo "internal required_arg() error, missing \$1 argument"
> +     exit 1
> +    fi
> +
> +    # check for an argument
> +    if [ X"$2" = X ]; then
> +     echo "the '$1' option is missing its required argument"
> +     echo ""
> +     usage
> +     exit 1
> +    fi
> +
> +    # does the argument look like an option?
> +    echo $2 | grep "^-" > /dev/null
> +    if [ $? -eq 0 ]; then
> +     echo "the argument '$2' of option '$1' looks like an option itself"
> +     echo ""
> +     usage
> +     exit 1
> +    fi
> +}

Can be written like this:

if ! echo $2 | grep -qs '^-'; then
  …
fi

Checking the -q/-s options in (GNU) grep's manpage, it seems to be
less portable than what I thought (but you can keep > /dev/null
instead).

Anyway that doesn't change the fact that you can check for the return
value directly in the if test, rather than using $? from the previous
command.

KiBi.

Attachment: signature.asc
Description: Digital signature

_______________________________________________
[email protected]: X.Org development
Archives: http://lists.x.org/archives/xorg-devel
Info: http://lists.x.org/mailman/listinfo/xorg-devel

Reply via email to