티스토리 뷰

사내 보안 강화를 위해 패스워드를 변경했습니다.

그러고 나니 특수문자가 먹지 않더군요.

해당 문제를 위해 ^와 "문자열"로, 특수 문자 문제를 해결했습니다.

기본적으로 배치 파일 내에서, 루비, TCP/IP 등 여러 경로를 통해서 스크립트가 실행되는데, 그 중에서도 배치와 루비에서 실행 문자열이 결정되는 구조입니다.
이 과정에서의 문제 해결을 위해 다음 내용을 참고했습니다..


References

http://www.robvanderwoude.com/escapechars.php


Escape Characters

All DOS versions interpret certain characters before executing a command.
Some well know examples are the percent sign ( % ), and the redirection symbols ( < | > ).

Windows 95/98 and NT, and OS/2 too, also interpret double quotes ( " ) and ampersands ( & ), as shown in the Conditional Execution page.

In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ).
That way, a single percent sign will be used as literal within the command line, instead of being furter interpreted.

In Windows 95/98 and NT, and OS/2 too, redirection symbols may be escaped by placing them between double quotes ( ">" ).
However, the quotes themselves will be passed to the command too, unlike the double percent sign.

Windows NT and OS/2 also allow the use of carets ( ^ ) to escape special characters. Even linefeeds can be escaped this way, as is shown in the Useless Tips page.

If you intend to "nest" commands with escaped characters, you may need to escape the escape character itself too.
In general, that won't make it any easier to read or debug your batch files, however.

Since the introduction of delayed variable expansion a new challenge is to escape exclamation marks, the "delayed" version of the percent sign.
Unlike percent signs, that can be escaped by doubling them, exclamation marks cannot be escaped by adding an extra exclamation mark.
Nor does a caret befor the exclamation mark work, unless quoted (i.e. ECHO ^! will fail to display an exclamation mark, whereas ECHO "^!" will display a quoted exclamation mark: "!").

Jaime Ramos sent me this link where the solution can be found: use ^^!.

The trick is that a single caret will be used to escape the exclamation mark in the firt "pass" of command line interpretation, but delayed variable expansion adds a second "pass" where the exclamation mark will be interpreted. If you don't get it, never mind, just remember the double caret before the exclamation mark.

To Be Investigated

Recently I discovered that (in Windows 7) with ECHO commands, an entire line can often be escaped with a single ASCII character 26 (Ctrl+Z or EOF, End Of File) as the first character to be ECHOed.
Only the > character may sometimes still be interpreted as "redirect to ..."

Summary

Escape Characters
Character to be escapedEscape SequenceRemark
%%%May not always be required in doublequoted strings, just try
^^^May not always be required in doublequoted strings, but it won't hurt
&^&
<^<
>^>
|^|
'^'Required only in the FOR /F "subject" (i.e. between the parenthesis), unless backq is used
`^`Required only in the FOR /F "subject" (i.e. between the parenthesis), if backq is used
,^,Required only in the FOR /F "subject" (i.e. between the parenthesis), even in doublequoted strings
;^;
=^=
(^(
)^)
!^^!Required only when delayed variable expansion is active
\\\Required only in the regex pattern of FINDSTR
[\[
]\]
"\"

Syntax : Escape Characters, Delimiters and Quotes

Using "Double Quotes"

If a single parameter contains spaces, you can still pass it as one item by surrounding in "quotes" - this works well for long filenames.

If a parameter is used to supply a filename like this:

    MyBatch.cmd "C:\Program Files\My Data File.txt"

This parameters will be:

   %0 =MyBatch
%1 ="C:\Program Files\My Data File.txt"

To launch a batch script with spaces in the Program Path requiring "quotes"

CMD /k ""c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space""

Removing Quotes

A common method of removing quotes from a variable is the following:

   :: Remove quotes
   SET _string=###%_string%###
   SET _string=%_string:"###=%
   SET _string=%_string:###"=%
   SET _string=%_string:###=%

Unfortunately this will fail if the variable is NULL.
Here are some batch routines to remove quotes.

Working without Quotes

Without surrounding quotes, a long filename will be passed as %1 %2 %3...

   MyBatch C:\Program Files\My Data File.txt

To refer to the pathname above use %* rather than %1 %2 %3 - the %* will cover all parameters - even if there are more than %9

You can apply Extended Filename syntax to %* with the following workaround:

  @ECHO OFF
  SET _params=%*

  :: pass params to a subroutine
  CALL :sub "%_params%" 
  GOTO :eof

  :sub 
  :: Now display just the filename (not path)
  ECHO %~n1

Delimiters

Delimiters separate one parameter from the next ( they split the command line up into words)
Parameters are normally separated by spaces, but any of the following are also valid delimiters.

Comma , 
Semicolon ;
Equals = 
Space 
Tab 

Notice that although / and - are commonly used to separate command options, they are absent from the list above. This is because batch file parameters are passed to CMD.exe which can accept it's own parameters (which are invoked using / and - )

Most commands will accept any of the delimiters above with the exception of FOR - the FOR command assumes a SPACE delimiter - for anything else you need to specify the `delims=' option.

When using the TAB character as a delimiter be aware that many text editors will insert a TAB as a series of SPACEs.

When you use %* to refer to all parameters, the value returned will include the delimiters, under NT 4.0 this will include the leading space, under Win 2000 and above it won't.

Escape Character

  ^  Escape character.

Adding the escape character before a command symbol allows it to be treated as ordinary text. 
When piping or redirecting any of these charcters you should prefix with the escape character: \ & | > < ^

     e.g.  ^\  ^&  ^|  ^>  ^<  ^^ 

The escape character can be used to escape itself ^^ (meaning don’t treat the first ^ as an escape character), so you are escaping the escape character:

The characters in bold get escaped: 
    ^&  =>   &
  ^^^&  =>  ^&
^^^^^&  => ^^& 

Some commands (e.g. REG and FINDSTR) use the standard escape character of \ (as used by C, Python, SQL, bash and many other languages.)
The \ escape can cause problems when combined with NTFS paths that contain a trailing backslash.

For example the string "C:\Scripts\" would be interpreted as "C:\Scripts

Workarounds are to add a second backspace escape character or a space
"C:\Scripts\\"
 or "C:\Scripts\ "

“All the best stories in the world are but one story in reality - the story of escape. It is the only thing which interests us all and at all times, how to escape” - A. C. Benson

Related:

SETLOCAL EnableDelayedExpansion - More examples, particularly for HTML.
Long Filenames and NTFS - Valid characters in filenames
cmd Syntax
Powershell Escape Character


댓글