How to pipe the outcome of an if-statement to another command?
Posted: 22 Nov 2021 05:25
I thought I knew quite a lot about Batch and CMD, but the following is puzzling me.
So far so good, but I really don't understand why I'm getting...
...the moment I pipe the outcome of the IF-statement to another command.
I've tried to escape the inner parentheses, but to no avail.
In the meantime an alternative that does work for me:
Bonus question regarding this alternative:
Only with the 2nd ECHO does the 3rd-party-command receive "test". In all other cases it's "test " (with an extra space at the end).
With parentheses around the ECHO-command why does it still put out "test "?
Code: Select all
ECHO test> 1.txt
IF EXIST 1.txt (TYPE 1.txt) ELSE (ECHO test)
test
(IF EXIST 1.txt (TYPE 1.txt) ELSE (ECHO test))
test
Code: Select all
(IF EXIST 1.txt (TYPE 1.txt) ELSE (ECHO test)) | 3rd-party-command ...
( was unexpected at this time.
I've tried to escape the inner parentheses, but to no avail.
In the meantime an alternative that does work for me:
Code: Select all
(TYPE 1.txt 2>NUL || ECHO test) | 3rd-party-command ...
Code: Select all
ECHO test | 3rd-party-command ...
ECHO test| 3rd-party-command ...
(ECHO test) | 3rd-party-command ...
(ECHO test)| 3rd-party-command ...
With parentheses around the ECHO-command why does it still put out "test "?