Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

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

Aug
752
10
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 )".
 
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.
 
Current workaround is to wrap IF into another subshell, btw.
Or put it on one line.
Code:
v:\> type iftest.btm
@ECHO OFF
ECHO "Test line" | IF "%1" == "-d" (FIND /I "line") ELSE (ECHO "Fail!")

v:\> iftest.btm
"Fail!"

v:\> iftest.btm -d
"Test line"
 
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!"
  )
)
 
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.
 
Back
Top
[FOX] Ultimate Translator
Translate