[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Broken string comparison leads to always true and redirection
From: |
Leif W |
Subject: |
Broken string comparison leads to always true and redirection |
Date: |
Sun, 2 May 2004 18:36:44 -0400 |
Please advise or request more information. Version is
"2.05b.0(1)-release (i386-pc-linux-gnu)", on Debian GNU/Linux testing.
I'm trying to do a simple lexicographical string comparison. It seems
to be broken. No matter if the string is hard-coded, or in a variable,
it is broken in the same way, but the symptons are slightly different,
depending on the direction of the angle bracket.
If using a hard-coded string, it always evaluates to true, and always
redirects output to the second argument to the binary comparison
operator. Notice in this example, all angle brackets point to the right
(greater-than or redirect STDOUT from screen to file).
<defiant> [2004-05-02@18:13:38] /usr/local/src/httpd -> [ "1" > "0" ] &&
echo "YES";
YES
<defiant> [2004-05-02@18:14:05] /usr/local/src/httpd -> [ "1" > "0" ] &&
echo "YES";
bash: 0: cannot overwrite existing file
<defiant> [2004-05-02@18:14:06] /usr/local/src/httpd -> ls -al 0
-rw-r--r-- 1 root root 0 May 2 18:14 0
<defiant> [2004-05-02@18:18:17] /usr/local/src/httpd -> [ "0" > "1" ] &&
echo "YES";
YES
<defiant> [2004-05-02@18:18:41] /usr/local/src/httpd -> [ "0" > "1" ] &&
echo "YES";
bash: 1: cannot overwrite existing file
<defiant> [2004-05-02@18:18:44] /usr/local/src/httpd -> ls -al 1
-rw-r--r-- 1 root root 0 May 2 18:18 1
<defiant> [2004-05-02@18:19:00] /usr/local/src/httpd ->
Cleanup
<defiant> [2004-05-02@18:21:31] /usr/local/src/httpd -> \rm -fv 0 1
removed `0'
removed `1'
If I put the arguments into a shell variable first, then the use the
left angle bracket (less thanor redirect STDIN from screen to file), the
second argument is used to read from as if it were a file, instead of a
string. Always evaluates to true, as long as a file name exists that
matches the second argument. Always assumes the angle bracket is a sign
for redirection, not acomparison operator.
<defiant> [2004-05-02@18:21:34] /usr/local/src/httpd -> str1="0";
str2="1"; [ $str1 < $str2 ] && echo "YES";
bash: 1: No such file or directory
<defiant> [2004-05-02@18:23:14] /usr/local/src/httpd -> str1="0";
str2="1"; [ $str1 > $str2 ] && echo "YES";
YES
<defiant> [2004-05-02@18:23:20] /usr/local/src/httpd -> str1="0";
str2="1"; [ $str1 < $str2 ] && echo "YES";
YES
<defiant> [2004-05-02@18:23:30] /usr/local/src/httpd -> str1="0";
str2="1"; [ $str1 > $str2 ] && echo "YES";
bash: 1: cannot overwrite existing file
<defiant> [2004-05-02@18:23:34] /usr/local/src/httpd -> echo $str1 $str2
0 1
Leif
- Broken string comparison leads to always true and redirection,
Leif W <=