标题: [文本处理] 批处理为什么要在命令外加双引号? [打印本页]
作者: 悬崖之树 时间: 2012-12-2 21:49 标题: 批处理为什么要在命令外加双引号?
- @echo off
-
- set command=" ping [url]www.a.shifen.com[/url] | findstr /n .* | findstr "^[4-7]:" "
-
- for /f "tokens=1,* delims=:" %%a in (' %command% ') do (
- echo,%%b
- )
-
- pause
复制代码
为什么要在 ping www.a.shifen.com | findstr /n .* | findstr "^[4-7]:" 外加双引号?
可以写成 for /f "tokens=1,* delims=:" %%a in (' " ping www.a.shifen.com | findstr /n .* | findstr "^[4-7]:" " ') do (
echo,%%b
)
吗?
作者: weichenxiehou 时间: 2012-12-2 22:19
%变量解析的优先级会高于批处理命令本身的格式解析,作者的原意是想规避"|"在第一次命令解析时是被当成管道符号,但是同时也犯了一个错误,变量command的实际内容不会包含^,所以要想这种方式无误,需要让command变量包含两个^:- set command=" ping www.a.shifen.com | findstr /n .* | findstr "^^[4-7]:" "
复制代码
我更喜欢直接把命令放在for的括号中,转义|就行了:- for /f "tokens=1,* delims=:" %%a in ('ping [url]www.a.shifen.com[/url]^|findstr /n .*^| findstr "^[4-7]:"') do (
- echo,%%b
- )
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |