John Doe <john.doe@notpresent> writes:
> I learn shell programming and found this behavior:
>
> if ( test "a" > "b" ) then echo t; else echo f; fi
> if ( test "a" < "b" ) then echo t; else echo f; fi
Others have already pointed out the main problem (< and > in this
context are redirection operators, not relational operators).
Another relatively minor point: the parentheses are not necessary,
and are not part of the syntax of the shell's "if" statement.
The condition in an "if" statement is a command, treated as true
if the command succeeded (returned an exit status of 0) and false
if it failed. The "test" command is designed to succeed or fail
in a way that makes it useful as a condition in a shell "if" or
"while" statement. Parentheses are used for grouping; for example:
( echo Line one ; echo Line two ) > two_lines.txt
The commands are executed in a subshell; use { ... } to execute
the commands in the current process.
In this case they're unnecessary (but harmless). But without the
parentheses, you need a semicolon before the "then". The parentheses
are enough to tell the shell that it's reached the end of the
command. That's not clear from the bash documentation, but the
POSIX spec is more explicit.
--
Keith Thompson (The_Other_Keith)
ks...@mib.org <
http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"