转义字符,例如“,”,“>”,“ |”在批处理文件的参数中 [英] Escaping characters like ", <, >, >>, or | in the arguments to a batch file

查看:68
本文介绍了转义字符,例如“,”,“>”,“ |”在批处理文件的参数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试做:

fake-command.bat "ping -n 4 -w 1 127.0.0.1 >NUL"

fake-command.bat ping -n 4 -w 1 127.0.0.1

批处理文件可能类似于:

The batch file could look like:

@echo %*

应返回:

ping -n 4 -w 1 127.0.0.1 >NUL

ping -n 4 -w 1 127.0.0.1

这里是一种解决方法:

@echo off
goto start
------------------------------------------------------

Usage : mystring <command>

  Quotes around the command are required only when the
  command involves redirection via <, >, >>, or |, etc.
  Quotes ensure that the redirection is applied to the 
  command, rather than the bat command itself.

Examples :

  mystring ping -n 4 -w 1 127.0.0.1
  mystring "ping -n 4 -w 1 127.0.0.1 >NUL"

------------------------------------------------------
:start
SETLOCAL ENABLEDELAYEDEXPANSION

SET "MYSTRING=%*"
ECHO My String = !MYSTRING!
SET !MYSTRING=MYSTRING:>=^>!

CALL :BATCH_FUNCTION !MYSTRING!
GOTO :EOF

:BATCH_FUNCTION
SET "ARGS=%~1"
ECHO Arguments = !ARGS!
endlocal
GOTO :EOF

问题出在以下之后: mystring ping -n 1 127.0.0.1> NUL

返回:

My String =
Arguments =

及之后: mystring ping -n 1 127.0.0.1

它返回值:

My String = ping -n 1 127.0.0.1
Arguments = ping

更新:
我用以下代码更新了问题从获取Windows批处理脚本(.bat)中传递的参数列表

@echo off
SETLOCAL DisableDelayedExpansion
SETLOCAL
if exist param.txt (del param.txt)
for %%a in ('%*') do (
    set "prompt="
    echo on
    for %%b in ('%*') do rem * #%~1#
    @echo off
) > param.txt
ENDLOCAL

for /F "delims=" %%L in (param.txt) do (
  set "param1=%%L"
)
SETLOCAL EnableDelayedExpansion
set "param1=!param1:*#=!"
set "param1=!param1:~0,-2!"
echo My string is = !param1!

使用此代码,我可以获取转义符,但引号中未包含的参数将被破坏

With this code, I can get escape characters, but with arguments not in quotes output is broken

什么有效?

mystring "# % $ ` ' ( ) < << >> > >NUL && & || | { } \ / - + = , . : ; ^ " &REM OK
mystring "ping -n 4 -w 1 127.0.0.1 >NUL" &REM OK

它返回:

My string is = # % $ ` ' ( ) < << >> > >NUL && & || | { } \ / - + = , . : ; ^
My string is = ping -n 4 -w 1 127.0.0.1 >NUL 

什么不起作用?

mystring ping -n 4 -w 1 127.0.0.1 &REM NOK
mystring "*" &REM NOK

它返回:

My string is = ping
The system can not find the file param.txt.
My string is = *

我看不到此代码中如何添加 Mofi的诀窍

I do not see how in this code add the trick from Mofi.

推荐答案

批处理代码也适用于单个参数 ping -n 4 -w 1 127.0.0.1> NUL 是:

The batch code working also for single argument "ping -n 4 -w 1 127.0.0.1 >NUL" is:

@echo off
goto start
------------------------------------------------------

Usage : mystring <command>

  Quotes around the command are required only when the
  command involves redirection via <, >, >>, or |, etc.
  Quotes ensure that the redirection is applied to the
  command, rather than the bat command itself.

Examples :

  mystring ping -n 4 -w 1 127.0.0.1
  mystring "ping -n 4 -w 1 127.0.0.1 >NUL"

------------------------------------------------------
:start
SETLOCAL ENABLEDELAYEDEXPANSION

if "%~2" == "" (SET "MYSTRING=%~1") else (SET "MYSTRING=%*")
ECHO My String = !MYSTRING!
SET "!MYSTRING=MYSTRING:>=^>!"

CALL :BATCH_FUNCTION "!MYSTRING!"
GOTO :EOF

:BATCH_FUNCTION
SET "ARGS=%~1"
ECHO Arguments = !ARGS!
endlocal
GOTO :EOF

IF

调用批处理文件时

fake-command.bat ping -n 4 -w 1 127.0.0.1

调用了6个参数,因此%* 是将所有参数分配给变量 MYSTRING 的正确方法。

it is called with 6 arguments and therefore %* is the right method to assign all arguments to the variable MYSTRING.

但使用$ p

But calling the batch file with

fake-command.bat "ping -n 4 -w 1 127.0.0.1 >NUL"

表示仅用双引号括起来的一个参数是

means just 1 argument enclosed in double quotes is passed to the batch file.

该行

SET "MYSTRING=%*"

结果分配给变量 MYSTRING 字符串

"ping -n 4 -w 1 127.0.0.1 >NUL"

带引号的字符串中。其余的引号会导致进一步处理时产生错误的结果。

with the quotes included in string. The remaining quotes causes the wrong result on further processing.

尚不清楚应为变量 ARGS 分配什么。如果传递给批处理文件的所有参数都应分配给此变量,则在调用子例程以传递批处理的所有参数时,还必须在双引号中加上!MYSTRING!。文件作为子例程的1个参数。

It is not clear what should be assigned to variable ARGS. If all arguments passed to batch file should be assigned to this variable, it is necessary to enclose !MYSTRING! also in double quotes on calling the subroutine to pass all arguments of the batch file as 1 argument to the subroutine.

命令 FOR 可用于拆分分配给 MYSTRING的字符串放入命令(ping)及其参数(带有重定向器运算符)。

The command FOR could be used to split up the string assigned to MYSTRING into command (ping) and its arguments (with redirector operators).

更新(在问题更新后):

目前尚不清楚任务到底是什么。 此批处理代码旨在仅将第一个参数传递给批处理文件,这就是的原因#%1#在第二个内部 FOR 循环中。确实,这是批处理代码的功能,尽管它包含一个小错误,对结果没有影响。

It is not really clear what is the task at all. This batch code is designed to get just first parameter passed to the batch file which is the reason for #%1# in second, inner FOR loop. Exactly this is what the batch code does although it contains a small mistake with no effect on result.

set prompt = 没有任何作用,因为不可能将提示文本更改为空。因此,文件 params.txt 仍包含提示文本。更好的方法是使用提示$ _ 设置提示= $ _ 来定义提示文本,只是提示行+ -feed,因此在读取生成的文件 params.txt 时,仅产生一个空行,以后使用的命令 FOR 会忽略该空行。在第一个外部 FOR 循环之后,使用 endlocal 恢复先前的提示定义。

set "prompt=" has no effect as it is impossible to change prompt text to nothing. So the file params.txt still contains the prompt text. Better is using prompt $_ or set "prompt=$_" which defines prompt text being only carriage return + line-feed and therefore producing just an empty line ignored by later used command FOR on reading in the produced file params.txt. The previous prompt definition is restored with endlocal after the first outer FOR loop.

Paul ,您只需将(1)都替换为('%*')的作用仅在于,对于传递给批处理文件的每个参数,第一个也是只有第一个参数被写入文本文件 params。 txt ,然后多次阅读。在使用各种参数运行批处理文件后,只需查看 params.txt 即可查看其中包含的内容。当然,这并不是真的有用。

Paul, you just replaced both (1) by ('%*') which has only the effect that for each parameter passed to the batch file the first and only the first parameter is written to text file params.txt and next read in several times. Just look on params.txt to see what it contains after running the batch file with the various parameters. This is of course not really useful.

这里是修改后的批处理代码,用于输出所有传递给批处理文件的参数。

Here is the batch code modified to output all parameters passed to the batch file.

@echo off
setlocal DisableDelayedExpansion
del param.txt 2>nul
for %%a in (1) do (
    set "prompt=$_"
    echo on
    for %%b in (%*) do rem * #%%~b#
    @echo off
) > param.txt
endlocal

setlocal EnableDelayedExpansion
set "ParaNumber=1"
set "AllParameters="
for /F "delims=" %%L in (param.txt) do (
    set "Parameter=%%L"
    set "Parameter=!Parameter:*#=!"
    set "Parameter=!Parameter:~0,-2!"
    set "AllParameters=!AllParameters! !Parameter!"
    echo Parameter !ParaNumber! = !Parameter!
    set /A ParaNumber+=1
)
set "AllParameters=!AllParameters:~1!"
echo All parameters: !AllParameters!
endlocal

使用以下三个字符串依次调用批处理文件3次

Calling the batch file three times with the following three strings one after the other

"# % $ ` ' ( ) < << >> > >NUL && & || | { } \ / - + = , . : ; ^ "
"ping -n 4 -w 1 127.0.0.1 >NUL"
ping -n 4 -w 1 127.0.0.1

产生以下三个输出

Parameter 1 = # % $ ` ' ( ) < << >> > >NUL && & || | { } \ / - + = , . : ; ^
All parameters: # % $ ` ' ( ) < << >> > >NUL && & || | { } \ / - + = , . : ; ^

Parameter 1 = ping -n 4 -w 1 127.0.0.1 >NUL
All parameters: ping -n 4 -w 1 127.0.0.1 >NUL

Parameter 1 = ping
Parameter 2 = -n
Parameter 3 = 4
Parameter 4 = -w
Parameter 5 = 1
Parameter 6 = 127.0.0.1
All parameters: ping -n 4 -w 1 127.0.0.1 >NUL

现在,使用 * 调用批处理文件会导致将参数1至n列出当前目录中所有非隐藏文件。因此,此批处理文件的修改版本不适用于有效文件名模式的参数。

Calling the batch file with "*" results now in getting listed as parameters 1 to n all non hidden files in current directory. So this modified version of the batch file does not work for parameters which are valid file name patterns.

这篇关于转义字符,例如“,”,“&gt;”,“ |”在批处理文件的参数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆