When I run this, it tests to Y, but why? If I take the double quotes off of test, i get the same.
# !/bin/ksh
TEST="N"
if [ "$TEST"="Y" ]; then
echo $TEST" yes"
else
echo "no"
fi
When I run this, it tests to Y, but why? If I take the double quotes off of test, i get the same.
# !/bin/ksh
TEST="N"
if [ "$TEST"="Y" ]; then
echo $TEST" yes"
else
echo "no"
fi
Hi.
Code tags would make this readable (i.e. I see three errors at a glance, and it's not obvious if you typed this or pasted it)..
The error is
"$TEST"="Y"
This would evaluate to true (as a non-empty string in itself.)
Should be
[ "$TEST" = "Y" ]
(with spaces either side of the =)
You also have a space between # and ! on line 1.
Also worth mentioning is that the preferred method for testing in Korn Shell is to use the [[ ... ]] construct instead of [ ... ]. There are several advantages, one of them is that it is much less picky about quoting and empty strings. For instance you could write your script without any double quotes:
#!/bin/ksh
TEST=N
if [[ $TEST = Y ]]; then
echo $TEST yes
else
echo no
fi
or even:
#!/bin/ksh
TEST=Y
[[ $TEST = Y ]] && echo $TEST yes || echo no
There's something to be said for both [[ ... ]] and && || and something against.
/root/tmp # [[ Y = Y ]] && paaa y || echo b
-ksh: paaa: not found [No such file or directory]
b
Personally I prefer [ ... ] and quotes where needed, and an if [ ... ]; then else where the && part could itself fail.
[[ ]] is testing the test, and leaves you not knowing necessarily why the inner test failed. Was the condition false? Was the variable not declared when it should have been?
Just a preference.