program story

Windows 명령 프롬프트에서 꺾쇠 괄호 이스케이프

inputbox 2020. 9. 7. 08:05
반응형

Windows 명령 프롬프트에서 꺾쇠 괄호 이스케이프


꺾쇠 괄호 (<및>)가 포함 된 문자열을 Windows 컴퓨터의 파일에 에코해야합니다. 기본적으로 내가 원하는 것은 다음과 같습니다.
echo some string < with angle > brackets >>myfile.txt

명령 인터프리터가 꺾쇠 괄호와 혼동되기 때문에 작동하지 않습니다. 다음과 같이 전체 문자열을 인용 할 수 있습니다.
echo "some string < with angle > brackets" >>myfile.txt

하지만 내 파일에 원하지 않는 큰 따옴표가 있습니다.

대괄호 ala unix를 이스케이프해도 작동하지 않습니다.
echo some string \< with angle \> brackets >>myfile.txt

아이디어?


어떤 이유로 Windows 이스케이프 문자는 ^입니다.

echo some string ^< with angle ^> brackets >>myfile.txt

사실, 공식적인 이스케이프 문자는입니다 ^. 그러나 때때로 ^ 문자 가 필요하므로주의하십시오 . 이것은 때때로 다음과 같습니다.

C:\WINDOWS> echo ^<html^>
<html>

C:\WINDOWS> echo ^<html^> | sort
The syntax of the command is incorrect.

C:\WINDOWS> echo ^^^<html^^^> | sort
<html>

C:\WINDOWS> echo ^^^<html^^^>
^<html^>

이 말도 안되는 한 가지 트릭 echo은 출력을 수행하고 큰 따옴표로 인용하는 것 이외의 명령을 사용하는 것입니다 .

C:\WINDOWS> set/p _="<html>" <nul
<html>
C:\WINDOWS> set/p _="<html>" <nul | sort
<html>

이것은 프롬프트 텍스트의 선행 공백을 유지하지 않습니다.


^이스케이프 시퀀스 를 피하는 방법이 있습니다 .

확장이 지연된 변수를 사용할 수 있습니다. 아래는 작은 배치 스크립트 데모입니다.

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
echo !line!

또는 FOR / F 루프를 사용할 수 있습니다. 명령 줄에서 :

for /f "delims=" %A in ("<html>") do @echo %~A

또는 배치 스크립트에서 :

@echo off
for /f "delims=" %%A in ("<html>") do echo %%~A

특정 사업자가 좋아 후 지연 확장과 변수 확장을 위해 모두가 발생하기 때문에 이유는 이러한 방법의 일이다 <, >, &, |, &&, ||구문 분석됩니다. Windows 명령 인터프리터 (CMD.EXE)가 스크립트를 구문 분석하는 방법을 참조하십시오 . 더 많은 정보를 위해서.


sin3.14는 파이프에 다중 이스케이프가 필요할 수 있음을 지적합니다 . 예를 들면 :

echo ^^^<html^^^>|findstr .

The reason pipes require multiple escapes is because each side of the pipe is executed in a new CMD process, so the line gets parsed multiple times. See Why does delayed expansion fail when inside a piped block of code? for an explanation of many awkward consequences of Window's pipe implementation.

There is another method to avoid multiple escapes when using pipes. You can explicitly instantiate your own CMD process, and protect the single escape with quotes:

cmd /c "echo ^<html^>"|findstr .

If you want to use the delayed expansion technique to avoid escapes, then there are even more surprises (You might not be surprised if you are an expert on the design of CMD.EXE, but there is no official MicroSoft documentation that explains this stuff)

Remember that each side of the pipe gets executed in its own CMD.EXE process, but the process does not inherit the delayed expansion state - it defaults to OFF. So you must explicitly instantiate your own CMD.EXE process and use the /V:ON option to enable delayed expansion.

@echo off
setlocal disableDelayedExpansion
set "line=<html>"
cmd /v:on /c echo !test!|findstr .

Note that delayed expansion is OFF in the parent batch script.

But all hell breaks loose if delayed expansion is enabled in the parent script. The following does not work:

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
REM - the following command fails
cmd /v:on /c echo !test!|findstr .

The problem is that !test! is expanded in the parent script, so the new CMD process is trying to parse unprotected < and >.

You could escape the !, but that can get tricky, because it depends on whether the ! is quoted or not.

If not quoted, then double escape is required:

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
cmd /v:on /c echo ^^!test^^!|findstr .

If quoted, then a single escape is used:

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
cmd /v:on /c "echo ^!test^!"|findstr .

But there is a surprising trick that avoids all escapes - enclosing the left side of the pipe prevents the parent script from expanding !test! prematurely:

@echo off
setlocal enableDelayedExpansion
set "line=<html>"
(cmd /v:on /c echo !test!)|findstr .

But I suppose even that is not a free lunch, because the batch parser introduces an extra (perhaps unwanted) space at the end when parentheses are used.

Aint batch scripting fun ;-)


In order to use special characters, such as '>' on Windows with echo, you need to place a special escape character before it.

For instance

echo A->B

will no work since '>' has to be escaped by '^':

 echo A-^>B

See also escape sequences. enter image description here

There is a short batch file, which prints a basic set of special character and their escape sequences.


Escaping the brackets ala unix doesn't work either:

echo some string \< with angle \> brackets >>myfile.txt

The backslash would be considered the start of a absolute pathname.


You can also use double quotes to escape special characters...

echo some string "<" with angle ">" brackets >>myfile.txt

참고URL : https://stackoverflow.com/questions/251557/escape-angle-brackets-in-a-windows-command-prompt

반응형