according to the test(1)
manpages:
-n STRING
the length of STRING is nonzero
so I expected this to run fine:
[ -n ${var} ] && echo "var is not empty"
I used this logic in a real case, a script like this:
[...]
dne() {
echo DEBUG: wc -c: $(echo -n $peopkg |wc -c)
echo DEBUG: pdir: $pdir
echo "Error: ${package}: doesn't exist in local repos"
exit 1
}
# packages are listed as such:
# p/pname/package-version-release-distrelease...
# pname is inconsistent, but it is guaranteed that the first word in package
# (all to lowercase) will match at least one pname.
# NOTE: package-[0-9]* matches package-32bit. *-32bit is a subpackage we want
# to match later, but not when it's not already in pcase.
# So that must be negated too
pfirst=${pcase%%-*}
for pdir in ${p}/${pfirst}*
do
# check if the glob matched anything at all
[ ${pdir} = "${p}/${pfirst}*" ] && dne
peopkg=$(find ${pdir} \
-name ${package}-[0-9]* \
! -name *.delta.eopkg \
! -name ${package}-32bit* |sort -rn |head -1)
echo DEBUG: in-loop peopkg: $peopkg
echo DEBUG: in-loop wc -c: $(echo -n $peopkg |wc -c)
echo DEBUG: in-loop test -n: $(test -n $peopkg && echo true || echo false)
#------------------------------------------------------------#
# break on ANY match. There's supposed to be only one anyway #
[ -n ${peopkg} ] && break # <--- [issue here] #
#------------------------------------------------------------#
done
[ -z ${peopkg} ] && dne
[...]
what matters here is that when I run this, I get these messages:
DEBUG: in-loop peopkg:
DEBUG: in-loop wc -c: 0
DEBUG: in-loop test -n: true
DEBUG: wc -c: 0
DEBUG: pdir: a/alsa-firmware
Error: alsa-utils: doesn't exist in local repos
This makes zero sense to me.. DEBUG: pdir: a/alsa-firmware
indicates that the loop exits always on the first iteration. Which can only happen if the glob pattern a/alsa* matched something AND peopkg was nonzero length.
PS: I'm trying to be POSIX compliant.