标题: [文本处理] [已解决]批处理里面括号也需要转义吗? [打印本页]
作者: bingxing8000 时间: 2011-9-13 10:06 标题: [已解决]批处理里面括号也需要转义吗?
在这段代码里,括号不转义的话,就会一闪而过- @echo off
- (echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
- echo #
- echo # This file contains the mappings of IP addresses to host names. Each
- echo # entry should be kept on an individual line. The IP address should
- echo # be placed in the first column followed by the corresponding host name.
- echo # The IP address and the host name should be separated by at least one
- echo # space.
- echo #
- echo # Additionally, comments ^(such as these^) may be inserted on individual
- echo # lines or following the machine name denoted by a '#' symbol.
- echo # For example:
- echo #
- echo # 102.54.94.97 rhino.acme.com # source server
- echo # 38.25.63.10 x.acme.com # x client host
- echo 127.0.0.1 localhost ) >E:\1.txt
- pause >nul
复制代码
以下代码中括号又不用转义- echo (dsfdf(sdfsdf)) >>e:\1.txt
复制代码
为什么会这样呢?
作者: CrLf 时间: 2011-9-13 11:08
因为未转义的括号会造成语块的错误划分,除 for 和 if 外(可以把这两个命令看成特殊的带有条件语句的语块,这解释虽不准确,但可以帮助理解…),所有命令中出现的括号都必须转义,因为它们只是命令参数的一部分,而非语块的划分符。
作者: ArdentMan 时间: 2011-9-13 11:23
我理解括号同其他特殊字符一样具有特殊的作用
所以在命令中用到括号就要跟其他特殊字符一样先进行转义
使之先成为普通字符(预处理前)
当cmd预处理时会去掉所有的转义符还原特殊字符原来的作用
从而使得命令正常进行~~~
作者: bingxing8000 时间: 2011-9-13 11:44
回复 2# CrLf
谢谢了。
针对我碰到的这个情况,应该是这样的:如下代码
(echo # Additionally, comments (such as these) may be inserted on individual)>>e:\1.txt
批处理应该是把(echo # Additionally, comments (such as these)看成是一个完整的括号,然后就出错退出了。所以需要转义。
作者: Hello123World 时间: 2011-9-14 18:20
按照2楼的说法,其实就是批处理语法太混乱,( 只跟最近的一个 ) 相结合。——但是for的嵌套 () 为什么没出现这种规律?
所以代码这么修改也行:- @echo off
- (echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
- echo #
- echo # This file contains the mappings of IP addresses to host names. Each
- echo # entry should be kept on an individual line. The IP address should
- echo # be placed in the first column followed by the corresponding host name.
- echo # The IP address and the host name should be separated by at least one
- echo # space.
- echo #
- echo # Additionally, comments (such as these^) may be inserted on individual
- echo # lines or following the machine name denoted by a '#' symbol.
- echo # For example:
- echo #
- echo # 102.54.94.97 rhino.acme.com # source server
- echo # 38.25.63.10 x.acme.com # x client host
- echo 127.0.0.1 localhost ) >11.txt
- pause >nul
复制代码
echo # Additionally, comments (such as these^) may be inserted on individual
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |