Re: test z$foo = zbar (and Re: [PATCH 1/3] bisect: relax requirement for a working tree.)
- From
Jonathan Nieder <jrnieder@gmail.com>
- Date
- Aug 7, 2011, 14:10 UTC
- Message-ID
- <20110807141035.GC10748@elie.gateway.2wire.net>
- In-Reply-To
- <201108071505.00762.stefano.lattarini@gmail.com>
Stefano Lattarini wrote:
> $ /usr/xpg4/bin/sh -c 'test -z ")"'; echo $? > 0
Yikes, thanks for pointing it out. I wonder if we should introduce a sane_test function, like this.
empty_string () {
case $* in
?*)
return 1 ;;
*)
return 0 ;;
esac
} # This is like "test", but:
# - it only implements a minimal subset of the functionality
# - it works around shells that cannot cope with parentheses as
# parameters to some operators, like ksh before 2008:
#
# $ /usr/xpg4/bin/sh -c 'test -z ")"'; echo $?
# 0
sane_test () {
# implied -n
test "$#" = 0 && set -- -n ""
test "$#" = 1 && set -- -n "$1"
test "$#" = 2 && test "z$1" = z! && set -- -z "$2"test "$#" -le 4 ||
case $# in 2) case $1 in -z) empty_string "$2" ;; -n) ! empty_string "$2" ;; -*) test "$@" ;; *) die "sane_test: unrecognized unary operator $1" ;; esac ;; 3) case $2 in = | !=) test "z$1" "$2" "z$3" ;; -eq | -lt | -gt | -le | -ge) test "$@" ;; *) die "sane_test: unrecognized binary operator $2" ;; esac ;; 4) test "z$1" = z! || die "sane_test: unrecognized 4-argument test (not a negation)"
shift ! sane_test "$@" ;; *) die 'sane_test: too many arguments' ;; esac }