Hack Forums
[Shell] Escape characters in input? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] Escape characters in input? (/showthread.php?tid=4876810)



Escape characters in input? - Advertisement - Jun 22, 2015

This could come useful in some of my scripts, on phone so ignore typos etc
So take this code as example
Code
@echo off
echo How are you?
echo.
set /p onput=
echo %onput%

Say if you typed, &goto LA&ipconfig | findstr Connection

So on, how do you automatically add like a ^| etc?


* - Hackoo - Jun 22, 2015

Hi 🤝
Try something like that :

Code
@echo off
setlocal EnableDelayedExpansion
:main
cls
echo How are you?
echo.
set /p onput=
set "temp=!%~1!"
set "temp=!temp:^=^^!"
set "temp=!temp:&=^&!"
set "temp=!temp:|=^|!"
set "temp=!temp:<=^<!"
set "temp=!temp:>=^>!"
set "temp=!temp:"=^^"!"
set "!onput!=!temp!"
echo !onput!
pause
goto:main

Or more simple like this :You can take a look at
setlocal EnableDelayedExpansion

Code
@echo off
setlocal EnableDelayedExpansion
:main
cls
echo How are you ?
echo.
set /p onput=
echo !onput!
pause
goto:main

Hope that what do you expecting for and of course this may be improved by other members here !
Good Luck for you ✌


* - annikk.exe - Jun 23, 2015

Hackoo is correct :>

To further explain, we use string substitution to escape any reserved characters.

Consider this simple example.
Code
set "var=Cats are best"
echo %var%
echo %var:best=fluffy%

Code
Cats are best
Cats are fluffy

On the first line, we set a variable to contain the text Cats are best.
On the second line, we echo that variable.
On the third line, we echo the variable, but we replace any instances of "best" with "fluffy". So what actually comes out is "Cats are fluffy".

So it's %var:A=B%
Where A is the thing to be replaced, and B is the thing to replace it with. :>

In this case, we use this technique to replace any instances of | with ^|
:>