WAD Output redirection to IF block fails with "unbalanced brackets"

Aug 23, 2010
590
7
TCC-RT 22.00.40 x64 Windows 7 [Version 6.1.7601]
STC:
Code:
@ECHO OFF
ECHO "Test line" | IF "%1" == "-d" (
  FIND /I "line"
) ELSE (
  ECHO "Fail!"
)
CMD runs as expected, TCC breaks with "5/6: Unbalanced parentheses ("/"6: Unknown command )".
 
Aug 23, 2010
590
7
To clarify, CMD creates a subshell with (), much like POSIX shell do.
Constructions like
Code:
(
  echo 1
  echo 2
  echo 3
) > file.ext
Are equally valid in CMD and POSIX.
 

rconn

Administrator
Staff member
May 14, 2008
11,952
133
TCC-RT 22.00.40 x64 Windows 7 [Version 6.1.7601]
STC:
Code:
@ECHO OFF
ECHO "Test line" | IF "%1" == "-d" (
  FIND /I "line"
) ELSE (
  ECHO "Fail!"
)

CMD runs as expected, TCC breaks with "5/6: Unbalanced parentheses ("/"6: Unknown command )".

Your syntax is a bit off. TCC's parser is smarter than CMD's, and is trying to associate the command group with the appropriate shell. (CMD just appends everything on the line, which is bad if you're doing another embedded pipe.)

The correct syntax (which works as expected in both TCC and CMD) is to group the entire pipe process:

Code:
@ECHO OFF
ECHO "Test line" | ( IF "%1" == "-d" (
    FIND /I "line"
  ) ELSE (
    ECHO "Fail!"
  )
)
 
Aug 23, 2010
590
7
If it would have been smarter, it'd recognize entire IF as a block operator without "wrapping it into a single line, like CMD does".
That would've been smart.