Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 685 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Escape PIPE character in a function call windows Batch script

#1
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?
Reply

#2
@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]

I think i'm missing something, because this works for me.

EDITED - This should be a more general solution. Not bulletproof but a skeleton.

@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]


Reply

#3
This is an effect of the `CALL` command.

The `CALL` command doubles all carets in one of the batch parser phases.
Normally you wouldn't see this, as the carets will be used as an escape charater directly after the doubling.

See this

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

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

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

But how do you can escape your pipe character?
**You can't!**

But you can add a caret remover in your function.

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


Or you could use an escaping trick when calling your function.

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

This works, as the `%%caret%%` will be expanded after the `CALL caret doubling phase`.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through