07-23-2023, 10:53 AM
I am writing a function to execute shell commands and capture its output in a batch script.
:runShellCmd
setlocal EnableDelayedExpansion
SET lf=-
FOR /F "delims=" %%i IN ('%~1') DO if "%out%" == "" (set out=%%i) else (set out=!out!%lf%%%i)
echo "Cmd output: %out%"
SET "funOut=%out%"
ENDLOCAL & IF "%~1" NEQ "" SET %~2=%out%
goto :EOF
I have been successful in passing simple commands and getting output. But for calls like
`CALL :runShellCmd "echo Jatin Kumar | find /c /i "jatin""` it fails with error `unexpected | character`.
I know we need to escape `|` with `^` in for but if I try to pass `^|` in the function argument string, it changes it to `^^|` which again throws error.
Am I missing something?
:runShellCmd
setlocal EnableDelayedExpansion
SET lf=-
FOR /F "delims=" %%i IN ('%~1') DO if "%out%" == "" (set out=%%i) else (set out=!out!%lf%%%i)
echo "Cmd output: %out%"
SET "funOut=%out%"
ENDLOCAL & IF "%~1" NEQ "" SET %~2=%out%
goto :EOF
I have been successful in passing simple commands and getting output. But for calls like
`CALL :runShellCmd "echo Jatin Kumar | find /c /i "jatin""` it fails with error `unexpected | character`.
I know we need to escape `|` with `^` in for but if I try to pass `^|` in the function argument string, it changes it to `^^|` which again throws error.
Am I missing something?