windows 在命令行上处理分号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2477554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:09:27  来源:igfitidea点击:

Processing Semicolon on Command line

windowsbatch-filecmd

提问by Joe Simon

I have two batch files which is used to run a large C++ build, the first one starts the processes, creating directories and figuring out what to send to the second script. If certain information is presented to the first script, I have a routine that pops up a window and asks for a password. This is passed to the second script by calling the second script like this

我有两个批处理文件用于运行大型 C++ 构建,第一个启动进程,创建目录并确定要发送到第二个脚本的内容。如果某些信息提供给第一个脚本,我有一个弹出窗口并要求输入密码的例程。这通过像这样调用第二个脚本传递给第二个脚本

call script2.bat -pw:myPassword

where myPassword is something the user entered. now, i have been testing this script and one of my users password contains a semicolon, so we get this

其中 myPassword 是用户输入的内容。现在,我一直在测试这个脚本,我的一个用户密码包含一个分号,所以我们得到了这个

call script2.bat -pw:my;Password

I found by putting in quotes I can get this into the second script OK

我发现通过加引号我可以把它放到第二个脚本中

call script2.bat -pw:"my;Password"

However, the command line parsing breaks when I try to do this

但是,当我尝试执行此操作时,命令行解析会中断

for /F "tokens=1,2 delims=:" %%a in ( "%1" ) DO SET switch=%%a&value=%%b

if I echo %1it shows like this

如果我是echo %1这样显示的

-pw:"my;Password"

But with echo on when the script runs I see

但是随着脚本运行时的 echo on 我看到

for /F "tokens=1,2 delims=:" %%a in ( "-pw:"my Password"" ) DO SET switch=%%a&value=%%b

and it parses as switch=-pwand value="my

它解析为switch=-pwvalue="my

What I eventually need is for value to contain my;Password so I can pass it to another program

我最终需要的是价值包含我的;密码,以便我可以将其传递给另一个程序

Any ideas on how to get this to parse correctly

关于如何正确解析的任何想法

Here re 2 batch file that issulstrate the problem:

这里有 2 个批处理文件来解决问题:

a.bat:

一只蝙蝠:

echo on

call b.bat -pw:eatme
call b.bat -pw:eat;me
call b.bat -pw:"eat;me"
call "b.bat -pw:\"eat;me\""

b.bat:

b.蝙蝠:

echo on

echo %1

for /F "tokens=1,2 delims=: " %%a in ( "%1" ) DO SET switch=%%a&SET value=%%b 

echo switch=%switch% 
echo value=%value%

回答by ewall

I found a little trick to get around the way the shell is interpreting the value of "%1" in the FOR /F loop: instead of parsing the string, parse the output of the command ECHO %1, like this:

我发现了一个小技巧来绕过 shell 在 FOR /F 循环中解释 "%1" 的值的方式:而不是解析字符串,解析命令的输出ECHO %1,如下所示:

FOR /F "tokens=1,2 delims=:" %%a IN ( 'ECHO %1' ) DO ECHO Switch: %%a Value: %%b

This works if you put the password in quotes on the command line (call script2.bat -pw="my;password"), so we'll have to remove the quotes as follows:

如果您在命令行 ( call script2.bat -pw="my;password")上将密码放在引号中,这将起作用,因此我们必须按如下方式删除引号:

SET VALUE=%VALUE:~1,-1%

So this is the code I came up with:

所以这是我想出的代码:

ECHO OFF

ECHO Arguments: %1

FOR /F "tokens=1,2 delims=:" %%a IN ( 'ECHO %1' ) DO (
    SET SWITCH=%%a
    SET VALUE=%%b
)

ECHO SWITCH: %SWITCH%

SET VALUE=%VALUE:~1,-1%
ECHO VALUE: %VALUE%

...which returns the following results:

...返回以下结果:

Arugments: -pw:"my;Password"
SWITCH: -pw
VALUE: my;Password

参数:-pw:“我的;密码”
开关:-pw
值:我的;密码

回答by aphoria

Try escaping the ; with a ^.

尝试逃避 ; 用 ^。

call script2.bat "-pw:my^;Password"

回答by t0mm13b

Try putting the script around in double quotes...the semicolon is a command separator so that you could type in multiple commands on the one line. The old days of DOS, and with DOSKEY, you could separate out the commands by hitting Ctrl+T, which is now the semicolon in today's command line processor. Do not worry about the quotes as the command processor will still be able to parse it, the reason the double quotes are used is to get around the long filename/path conventions.

尝试将脚本放在双引号中……分号是命令分隔符,因此您可以在一行中输入多个命令。在 DOS 的旧时代,使用 DOSKEY,您可以通过按 Ctrl+T 来分离命令,它现在是当今命令行处理器中的分号。不要担心引号,因为命令处理器仍然可以解析它,使用双引号的原因是为了绕过长文件名/路径约定。

call "script2.bat -pw:\"my;Password\""

回答by SkyLined

I've created a batch "function" which does "proper" parsing of arguments and handles equal signs and semicolons correctly. I think you'll find that it can help you solve these problems. Full details and an example can be found on my site: http://skypher.com/index.php/2010/08/17/batch-command-line-arguments/

我创建了一个批处理“函数”,它可以“正确”解析参数并正确处理等号和分号。我想你会发现它可以帮助你解决这些问题。可以在我的网站上找到完整的详细信息和示例:http: //skypher.com/index.php/2010/08/17/batch-command-line-arguments/

回答by flabdablet

Instead of

代替

for /F "tokens=1,2 delims=:" %%a in ( "%1" ) DO SET switch=%%a&value=%%b

use

for /F "tokens=1,2 delims=:" %%a in ( "%~1" ) DO SET switch="%%~a" & SET value="%%~b"

or (preferably, to my way of thinking)

或(最好是我的思维方式)

for /F "tokens=1,2 delims=:" %%a in ( "%~1" ) DO (
    SET switch="%%~a"
    SET value="%%~b"
)

Check HELP CALL and HELP FOR to find out about the %~ syntax for quote removal.

检查 HELP CALL 和 HELP FOR 以了解用于引用删除的 %~ 语法。