'Scripts/Batch'에 해당되는 글 1건

  1. 2013.09.10 문자열 내에 특수 문자가 있는 경우

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

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

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

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



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 ..."


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:

  SET _params=%*

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

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


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 = 

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


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

'Scripts > Batch' 카테고리의 다른 글

문자열 내에 특수 문자가 있는 경우  (0) 2013.09.10
Posted by 엘키 엘키

댓글을 달아 주세요

이전버튼 1 이전버튼

블로그 이미지
Software Engineer




 « |  » 2020.8
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          

글 보관함