Board logo

标题: [文本处理] 批处理如何原样输出百分号(变量不扩展) [打印本页]

作者: hothbo    时间: 2018-2-23 16:04     标题: 批处理如何原样输出百分号(变量不扩展)

本帖最后由 pcl_test 于 2018-3-28 05:36 编辑

请教一个很小白的问题
  1. echo netsh i i show in>>temp.bat
  2. echo ^@set /p idx=请输入“本地连接”的Idx编号:>>temp.bat
  3. echo netsh interface ipv4 set dns name=^%idx^% static 192.168.1.230 >>temp.bat
  4. echo netsh interface ipv4 add dns name=^%idx^% 192.168.1.223 index=2 >>temp.bat
  5. echo ping 127.1 -n 3 >null>>temp.bat
复制代码
运行后的结果是:
netsh i i show in
@set /p idx=请输入“本地连接”的Idx编号:
netsh interface ipv4 set dns name= static 192.168.1.230
netsh interface ipv4 add dns name= 192.168.1.223 index=2
ping 127.1 -n 3

可是我需要的运行结果是:
netsh i i show in
@set /p idx=请输入“本地连接”的Idx编号:
netsh interface ipv4 set dns name=%idx% static 192.168.1.230
netsh interface ipv4 add dns name=%idx% 192.168.1.223 index=2
ping 127.1 -n 3

我单独把第三和第四句在dos下运行,运行结果是我需要的结果。可是为什么组合到一起却不是我想得到的结果?
作者: ivor    时间: 2018-2-23 17:09

☛☛☛☛
replace %idx% to %%idx%%

作者: hothbo    时间: 2018-2-23 17:25

回复 2# ivor


谢谢!!按照您说的运行结果达到预期效果了!

只是能否说明一下为什么要这样改么?
作者: ivor    时间: 2018-2-23 17:37

Escape Characters
  1. Escape Characters
  2. Character to be escaped Escape Sequence Remark
  3. % %% May not always be required in doublequoted strings, just try
  4. ^ ^^ May not always be required in doublequoted strings, but it won't hurt
  5. & ^&
  6. < ^<
  7. > ^>
  8. | ^|
  9. ' ^' Required only in the FOR /F "subject" (i.e. between the parenthesis), unless backq is used
  10. ` ^` Required only in the FOR /F "subject" (i.e. between the parenthesis), if backq is used
  11. , ^, Required only in the FOR /F "subject" (i.e. between the parenthesis), even in doublequoted strings
  12. ; ^;
  13. = ^=
  14. ( ^(
  15. ) ^)
  16. ! ^^! Required only when delayed variable expansion is active
  17. " "" Required only inside the search pattern of FIND
  18. \ \\ Required only inside the regex pattern of FINDSTR
  19. [ \[
  20. ] \]
  21. " \"
  22. . \.
  23. * \*
  24. ? \?
复制代码

作者: hothbo    时间: 2018-2-23 17:40

回复 4# ivor


    万分感谢!!!收藏之!




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2