在命令行处理分号 [英] Processing Semicolon on Command line
问题描述
我用来运行大型C + _ +构建两个批处理文件,第一个启动的过程,创建目录,并找出发送给第二个脚本是什么。如果某些信息是psented的第一个脚本$ P $,我有一个弹出一个窗口,要求输入密码的程序。这是通过调用这样所述第二脚本传递到第二脚本
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
call script2.bat -pw:myPassword
,其中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
call script2.bat -pw:my;Password
我发现通过把在引号我可以进入第二个脚本确定这
调用script2.bat -pw:我的;密码
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
如果我回声%1就这样表示
if I echo %1 it shows like this
-pw:"my;Password"
但随着脚本运行时我看到回声
But with echo on when the script runs I see
for /F "tokens=1,2 delims=:" %%a in ( "-pw:"my Password"" ) DO SET switch=%%a&value=%%b
并将其解析为开关= -pw和值=我的
and it parses as switch=-pw and value="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
下面是再次的issulstrate问题2批处理文件:
一只蝙蝠:
回显
Here re 2 batch file that issulstrate the problem: a.bat: echo on
调用b.bat -pw:eatme
调用b.bat -pw:吃;我
调用b.bat -pw:吃,我
称之为b.bat -pw:\\吃;我\\
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.bat: echo on
回声%1
有关/ F令牌= 1,2 = delims:%%一中(%1)确实设置开关= %% A和设定值= %% b
for /F "tokens=1,2 delims=: " %%a in ( "%1" ) DO SET switch=%%a&SET value=%%b
回声开关=%开关%
回声值=%值%
echo switch=%switch% echo value=%value%
谢谢!
乔
推荐答案
我发现了一个小窍门来解决的方式外壳间$ P $在FOR / F环pting的%1的值:代替解析字符串,解析命令的输出 ECHO%1
,就像这样:
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:
FOR /F "tokens=1,2 delims=:" %%a IN ( 'ECHO %1' ) DO ECHO Switch: %%a Value: %%b
这工作,如果你把密码在命令行上的引号(通话script2.bat -pw =我的;密码
),所以我们必须要删除引号如下:
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:
SET VALUE=%VALUE:~1,-1%
这就是我想出了code:
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:我的;密码结果
开关:-pw结果
VALUE:我的;密码
Arugments: -pw:"my;Password"
SWITCH: -pw
VALUE: my;Password
这篇关于在命令行处理分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!