Reputation: 105
I made a CMD batch script for usage on Windows 7.
And I must use pipe line conditions.
But this my batch script is not working as expected by me.
Why does not work below script in pipe line?
And below echo |
syntax is a must required virtual conditions to pipe.
And unbalance quote is required conditions.
And I must be done without usage of SETLOCAL ENABLEDELAYEDEXPANSION
.
I must use only cmd.exe /v:on /c " .... "
.
@echo off
echo | cmd.exe /v:on /c "set z=Car and" Airplane is Machine & echo !z!"
rem OR
echo | cmd.exe /v:on /c "set z=Car and" Airplane is Machine & call echo %%z%%"
pause >nul
exit 0
Result is:
!z!"
"
Why do not work both batch command lines with pipe operator?
EDIT - Interpretion by dbenham:
The nonsensical command is simply a rudimentary test platform that demonstrates the problem the OP is having.
The OP wants the right side of a pipe to be a CMD.EXE process that defines a variable containing an odd number of quotes, and then echoes the resultant value. The question is poorly written, but it is an interesting problem. I left the original question, just in case my interpretation is incorrect.
Upvotes: 0
Views: 145
Reputation: 130849
Your problem has nothing to do with the pipe. Your problem is how to escape the command string properly such that the assignment includes an odd number of quotes in the value, and then your ECHO command properly displays the resultant value. You have the exact same problem if you remove the pipe and simply execute the CMD.EXE command on its own.
Before I provide a solution, you definitely should have a look at Why does delayed expansion fail when inside a piped block of code? for an in depth exploration of the intricacies of complex pipe operations.
There are a few things that complicate your problem:
1) The command string gets parsed multiple times - first by the main batch script, in which case you must make sure that the &
is either quoted or escaped such that it is passed through to CMD.EXE. The second time it is parsed by CMD.EXE itself, and this time you need to somehow include a quote in the assignment, yet now the &
must not be escaped or quoted such that it properly fires the ECHO command to show the result.
2) The outer most quotes enclosing the command string are removed by CMD.EXE prior to execution. But it is helpful to know that the outer quotes are not required :-) You simply must find the correct escape and quote sequences.
There are many solutions, and it is entirely predictable. However it can easily make your head explode when trying to trace through how it works ;-)
All of the following work. I added square brackets around the output to demonstrate that no unwanted characters are included in the assignment. The unwanted IGNORED text is just to demonstrate how a quoted assignment ignores text that follows the last quote. The IGNORED text can simply be removed.
@echo off
:: Without quotes around the command
echo | cmd.exe /v:on /c set z=Car^^" and Airplane is Machine&echo [!z!]
echo | cmd.exe /v:on /c set z=Car^^^" and Airplane is Machine^&echo [!z!]
:: With quotes around the command
echo | cmd.exe /v:on /c "set z=Car^" and Airplane is Machine^&echo [!z!]"
echo | cmd.exe /v:on /c ^"set z=Car^^" and Airplane is Machine&echo [!z!]"
:: With quotes around the assignment such that text after the last quote is ignored
:: Without quotes around the command
echo | cmd.exe /v:on /c set "z=Car" and Airplane is Machine^^" IGNORED & echo [!z!]
echo | cmd.exe /v:on /c set ^^"z=Car" and Airplane is Machine" IGNORED & echo [!z!]
echo | cmd.exe /v:on /c set "z=Car" and Airplane is Machine^^^" IGNORED ^& echo [!z!]
echo | cmd.exe /v:on /c set ^^"z=Car" and Airplane is Machine^" IGNORED ^& echo [!z!]
:: With quotes around the assignment such that text after the last quote is ignored
:: With quotes around the command
echo | cmd.exe /v:on /c "set "z=Car" and Airplane is Machine^" IGNORED ^& echo [!z!]"
echo | cmd.exe /v:on /c "set ^"z=Car" and Airplane is Machine" IGNORED ^& echo [!z!]"
echo | cmd.exe /v:on /c "set "z=Car^" and Airplane is Machine^^" IGNORED & echo [!z!]"
echo | cmd.exe /v:on /c "set ^"z=Car^" and Airplane is Machine" IGNORED & echo [!z!]"
--RESULT--
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
Surely your actual code is significantly different. I wouldn't be surprised if you have some additional work to do when you try to apply these concepts to your actual problem. Good luck!
Upvotes: 1
Reputation: 56190
That very much sounds like homework. Or a riddle.
Assuming the last (as the first would be off Topic):
echo Car and^" Airplane is Machine|cmd /v:on /c "set /p z=&echo !z:is=are!s"
should meet the requirements:
echo | syntax is a must.
check
unbalance quote is required.
check
no usage of SETLOCAL ENABLEDELAYEDEXPANSION.
check
use cmd.exe /v:on /c " .... ".
check
Result is:
Car and" Airplane are Machines
(replacement to prove that it shows the second echo
, not the first)
Upvotes: 1