Created
September 27, 2024 08:48
Bash Test for non-zero length string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# by Dennis Williamson | |
# 2010-10-06, revised 2010-11-10 | |
# for http://stackoverflow.com/q/3869072 | |
# designed to fit an 80 character terminal | |
dw=5 # description column width | |
w=6 # table column width | |
t () { printf '%-*s' "$w" " true"; } | |
f () { [[ $? == 1 ]] && printf '%-*s' "$w" " false" || printf '%-*s' "$w" " -err-"; } | |
o=/dev/null | |
echo ' | 1a 2a 3a 4a 5a 6a | 1b 2b 3b 4b 5b 6b' | |
echo ' | [ [" [-n [-n" [-z [-z" | [[ [[" [[-n [[-n" [[-z [[-z"' | |
echo '-----+------------------------------------+------------------------------------' | |
while read -r d t | |
do | |
printf '%-*s|' "$dw" "$d" | |
case $d in | |
unset) unset t ;; | |
space) t=' ' ;; | |
esac | |
[ $t ] 2>$o && t || f | |
[ "$t" ] && t || f | |
[ -n $t ] 2>$o && t || f | |
[ -n "$t" ] && t || f | |
[ -z $t ] 2>$o && t || f | |
[ -z "$t" ] && t || f | |
echo -n "|" | |
[[ $t ]] && t || f | |
[[ "$t" ]] && t || f | |
[[ -n $t ]] && t || f | |
[[ -n "$t" ]] && t || f | |
[[ -z $t ]] && t || f | |
[[ -z "$t" ]] && t || f | |
echo | |
done <<'EOF' | |
unset | |
null | |
space | |
zero 0 | |
digit 1 | |
char c | |
hyphn -z | |
two a b | |
part a -a | |
Tstr -n a | |
Fsym -h . | |
T= 1 = 1 | |
F= 1 = 2 | |
T!= 1 != 2 | |
F!= 1 != 1 | |
Teq 1 -eq 1 | |
Feq 1 -eq 2 | |
Tne 1 -ne 2 | |
Fne 1 -ne 1 | |
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/a/3870055 | |
| 1a 2a 3a 4a 5a 6a | 1b 2b 3b 4b 5b 6b | |
| [ [" [-n [-n" [-z [-z" | [[ [[" [[-n [[-n" [[-z [[-z" | |
-----+------------------------------------+------------------------------------ | |
unset| false false true false true true | false false false false true true | |
null | false false true false true true | false false false false true true | |
space| false true true true true false| true true true true false false | |
zero | true true true true false false| true true true true false false | |
digit| true true true true false false| true true true true false false | |
char | true true true true false false| true true true true false false | |
hyphn| true true true true false false| true true true true false false | |
two | -err- true -err- true -err- false| true true true true false false | |
part | -err- true -err- true -err- false| true true true true false false | |
Tstr | true true -err- true -err- false| true true true true false false | |
Fsym | false true -err- true -err- false| true true true true false false | |
T= | true true -err- true -err- false| true true true true false false | |
F= | false true -err- true -err- false| true true true true false false | |
T!= | true true -err- true -err- false| true true true true false false | |
F!= | false true -err- true -err- false| true true true true false false | |
Teq | true true -err- true -err- false| true true true true false false | |
Feq | false true -err- true -err- false| true true true true false false | |
Tne | true true -err- true -err- false| true true true true false false | |
Fne | false true -err- true -err- false| true true true true false false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment