bash - What's wrong with this shell tests? -
in script has functions defined:
#!/bin/bash -- outtrue (){ printf "%15s t|%s" "$*" "$?"; } outfalse (){ printf "%15s f|%s" "$*" "$?"; } tval1(){ [ "$@" ] && outtrue "$@" || outfalse "$@"; } tval2(){ [ "$*" ] && outtrue "$@" || outfalse "$@"; } tval3(){ [[ "$@" ]] && outtrue "$@" || outfalse "$@"; } tval(){ printf "test %s\t" "$1"; shift case $1 in (1) shift; tval1 "$@" ;; (2) shift; tval2 "$@" ;; (3) shift; tval3 "$@" ;; esac printf "\n" }
this tests work (as expected):
xyz="str"; tval "-n var" 1 "-n" "$xyz" xyz="str"; tval "-z var" 1 "-z" "$xyz" one=1; tval "1 -eq \$one" 1 "1" "-eq" "$one" one=2; tval "1 -eq \$one" 1 "1" "-eq" "$one"
results:
test -n var -n str t|0 test -z var -z str f|1 test 1 -eq $one 1 -eq 1 t|0 test 1 -eq $one 1 -eq 2 f|1
however, changing "$@" "$*" or using bash test [[...]]
makes tests fail (note here either function 2 or 3 used) :
one=1; tval "1 -eq \$one" 2 "1" "-eq" "$one" one=2; tval "1 -eq \$one" 2 "1" "-eq" "$one" one=1; tval "1 -eq \$one" 3 "1" "-eq" "$one" one=2; tval "1 -eq \$one" 3 "1" "-eq" "$one"
results:
test 1 -eq $one 1 -eq 1 t|0 test 1 -eq $one 1 -eq 2 t|0 test 1 -eq $one 1 -eq 1 t|0 test 1 -eq $one 1 -eq 2 t|0
do see problem?
the quoting!!
with "$*" , bash [[...]]
command line values not split.
the tests equivalent [ word ]
word not empty, , true.
Comments
Post a Comment