Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass the pipe character from a DOS batch script in a CALL statement

within a DOS batch script (that needs to run in the Win 200x and Win7 environments) I need to pass a particular character, in quotes, to another executable; e.g.

 doparse -delimeter "$"

In general this works:

 CALL CMD /C "doparse -delimeter "$""

Unfortunately, I need to specify the pipe character as the delimeter (this is a requirement). I expected the following would work:

 CALL CMD /C "doparse -delimeter "^|""

But when I run the script I don't see any output at all for this line (e.g. no error message if I replace "doparse" with some non-existent name).

I've tried various combinations of escape chars but cannot get it to work. Is it possible? (Has to be done via a batch script, unfortunately).

Thanks

like image 567
Dan Avatar asked Oct 11 '22 09:10

Dan


2 Answers

The following works for me:

call delim -delimiter "|"

and then using it like this in the called batch:

setlocal enabledelayedexpansion
set "delim=%~2"
echo Delimiter: !delim!
like image 56
Joey Avatar answered Nov 03 '22 02:11

Joey


These versions should work cmd /c "test2.bat "^|"" and cmd /c ^"test2.bat "|"".
Both starts the test2.bat.

Also the version of Joey works.

The problem in your case is the call and the number of quotes, it's not possible to escape special characters in a call statement (outside of quotes), at least without the special caret trick.

Example

set "caret=^"
call echo &
call echo ^&
call echo ^^&
call echo ^^^&
call echo "&"
call echo %%caret%%^&

Only the last two works, the first is inside of quotes and the second escapes the & in the second parser run with the special caret trick.

In your case the | is outside of the quotes (it's after the second quote).
And there is a bug in cmd.exe so it immediatly stops, if the parser founds a unescaped & or |<> after a call phase.

like image 38
jeb Avatar answered Nov 03 '22 02:11

jeb