Escape PIPE symbol in the function call window Package script

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 managed to go through simple commands and get a way out. But for challenges like

CALL :runShellCmd "echo Jatin Kumar | find /c /i "jatin""error with error unexpected | character.

I know that we need to escape |with ^in, but if I try to pass ^|in an argument string to a function, it will change it to ^^|, which again throws an error.

Am I missing something?

+4
source share
2 answers

This is a team effect CALL.

CALL .
, .

call echo ^^^^
call call echo ^^^^
call call call echo ^^^^

call echo "^^^^"
call call echo "^^^^"
call call call echo "^^^^"

^^
^^
^^
"^^^^^^^^"
"^^^^^^^^^^^^^^^^"
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"

?
!

.

:runShellCmd
   setlocal EnableDelayedExpansion
   set "param=%~1"
   set param
   set "param=!param:^^=^!"
   for .... ('!param!')

.

set "caret=^"
CALL :runShellCmd "echo Jatin Kumar %%caret%%| find /c /i "

, %%caret%% CALL caret doubling phase.

+11
@echo off
    setlocal enableextensions disabledelayedexpansion 

    call :test "echo(this|find "t""
    exit /b

:test
    set "x=%~1"
    for /f "delims=" %%f in ('%x:|=^|%') do echo [%%f]

, - , .

EDITED - . , .

@echo off
    setlocal enableextensions disabledelayedexpansion 

    call :test "(echo(this&echo(that)|find "t" 2>nul|sort&echo end"
    exit /b

:test
    set "x=%~1"
    set "x=%x:|=^|%"
    set "x=%x:>=^>%"
    set "x=%x:<=^<%"
    set "x=%x:&=^&%"
    set "x=%x:)=^)%"
    for /f "delims=" %%f in ('%x%') do echo [%%f]
+5

Source: https://habr.com/ru/post/1529953/


All Articles