     『第 3 楼』:
使用 LLM 解释/回答一下
楼上既然这么说,我把一般的符号的作用都发一下,你们好好看看吧,其实论坛里是有的....
你的问题与两个要素有关:
1、%是个ESCAPE字符,通常将之译为转义字符,但也有更形象的译名脱逸字符、逃逸字符等。也就是说%不仅仅将与其相关的特定字符串转义并替换为特定字符串,而且自身也会被“脱逸”。而且类似于C语言中的转义字符"\",双%会转义并脱逸为单%,四%则脱为双%。
2、for本身是一个特殊的命令,类似于一个特化的命令解释器,因为它的功能实现需要执行多条语句,因此它必须也具有对命令行(特指do后的命令行)分析处理的功能。而command/cmd实现for时自然会借用自身原有的命令行分析模块,因此for具有二级转义的特性,for中do后的语句被分两级分析和解释,第一级在command/cmd读入并解释for命令行时,第二级在for读入并解释do命令时,它通常会对同一命令行的进行多次解释。
然后,我们可以注意到,在do中使用命令行参数变量和环境变量时,不需要双%,那是因为,这些变量在经过第一级转义后,被替换成特定的不变的字符串常量,参与for循环的所有执行过程;而替代变量则要求在执行(do后的子命令行中)过程中不断的动态变化,而这个变化自然仍需要通过脱逸字符来实现,因此使用双%就是成了必然的选择。
另外,还需要注意到,在命令行中使用for时不需要双%,这源于命令解释器对命令行与批处理的处理方式不同。在早期的DOS版本中,%在命令行中不被视为转义字符,所以不会被转义和脱逸,所以当时无法在命令行直接引用环境变量。而使用for时,只需要一个%供for进行转义和脱逸就够了。在以后的命令解释器版本中,加入了命令行转义的支持(主要是环境变量的支持),但命令行for使用单%的传统仍然保留了下来。
而 cmd中的变量延迟替换是属于特殊的情况,但不违背以上的转义原则,只是for中的环境变量不再是常量了。
rmdir /S /Q %mhnet% 2>NUL 1>NUL 做简单解释
这句代码的大意是指将 %mhnet% 指定的目录删除,/s 代表删除其中的子目录, /q 表示删除目录树时不提示确认, 1>nul 表示将正确删除目录树的信息禁止输出,2>nul 表示将删除过程中的错误信息禁止输出
其中的1与2都是代表某个数据流输入输出的地址(NT CMD 称之为句柄,MSDOS称之为设备),下表(引自WinXP帮助文档“使用命令重定向操作符 (Redirection Operators”一节))将列出可用的句柄。
句柄 句柄的数字代号 说明
STDIN 0 键盘输入
STDOUT 1 输出到命令提示符窗口
STDERR 2 错误输出到命令提示符窗口
UNDEFINED 3-9 这些句柄由应用程序和各个具体工具单独定义
0 键盘输入
1 输出到命令提示符窗口
2 错误输出到命令提示符窗口
3-9 这些句柄由应用程序和各个具体工具单独定义。
2 > nul 表示程序出错的信息也不显示。
call attrib -r -h c:\autoexec.bat >nul
这句其实是:
call attrib -r -h c:\autoexec.bat 1 > nul
这些1,2,0等等都是句柄,说白了就是代号,你只要知道1是代表输出信息,2是代表出错信息,0是代表键盘输入就行了。
如果有什么不明白的可以多看看帮助与支持。
命令行对重定向符号出现的位置不做过多限定,只要重定向符号后紧随“字符设备”即可,故以下语句等效:
echo Hello World> hello.txt
echo Hello> Hello.txt World
echo> Hello.txt Hello World
> hello.txt echo Hello World
在NT系列命令行中,重定向的作用范围由整个命令行转变为单个命令语句,受到了命令分隔符&,&&,||和语句块的制约限制。
echo Message1> msg1.txt & echo Message2> msg2.txt
if "%target%"=="" (echo message to screen ) else (echo message to file> %target%)
综上所述,>nul 意为将此句命令所产生的标准输出请求重新定向到空设备中,而因为此设备的缄默特性,即相当于将此语句的输出信息屏蔽(并非隐藏);而 2>nul 则是将程序执行错误时的标准错误信息输出请求重定向后屏蔽。它们联合使用,即为将此语句所可能产生的所有输出信息屏蔽。
“重定向”是MSDOS起就存在的命令行特性,负责将指定命令或语句所产生的输入输出请求由缺省的“控制台”转交给其它的“设备”来完成,它的启动标志是“重定向符号”(包括“>,>>,<”三个,其各自意义见[1])出现在句中。
一般的命令行程序输入输出请求都通过内部定义三个“端口”(在NT下称为“句柄”,在DOS下未定义)来完成,分别为标准输入stdin、标准输出stdout、标准错误stderr。它们通常指向的设备为控制台(console,代码为CON),其中stdin指向控制台的键盘,stdout/stderr指向控制台的监视器。因此,控制台通常即指键盘与监视器的联合体,这是在早期大型机的终端机上所体现出来的概念。其中的stdin可被<重定向,stdout可被>、>>重定向,而stderr在DOS下不可直接重定向,只有通过ctty或其它命令将系统控制权转交给其它设备的方式,来间接完成。
“设备”是指可控制PC硬件或端口的设备驱动程序或端口代码,它通常由系统底层或硬件驱动程序实现和支持。比如IO.SYS实现的控制台CON、系统时钟CLOCK$、未知设备CONFIG$、第一串口AUX、第一并口PRN、所有串口COM1~COM4、所有并口LPT1~LPT3、可用盘符A:-X:以及上文提到的空设备NUL。还有许多其它设备,比如HIMEM.SYS实现的XMSXXXX0,EMM386.EXE实现的EMMXXXX0,IFSHLP.SYS实现的IFS$HLP$等。
在这些设备中,可以处理输入输出信息的很少,只有CON、NUL以及连接有输入输出硬件(打印机、MODEM等)的串口或并口设备。它们被称为“字符设备”,而磁盘文件也作为一种特殊的字符设备列选其中,这就大大扩充了重定向的自由度与实用性,以致很多人也将重定向称为“文件重定向”。
空设备NUL是一个特殊的设备,因为它没有可控制的PC硬件或端口,而只是一个虚构的的设备或端口,它仅存在于软件层面。正因为如此,它可以接受所有重定向的输入输出请求而不给出任何回应(在NT下不会给出任何输入信息而结束输入请求,在DOS下则反复填充127个字节0后终止响应),这种特性使它很像天文学上的能吞噬一切物质和信息的“黑洞”,也很类似哲学上能颠转阴阳无中生有的“玄玄之道”。它之所以存在,是因为我们需要一个可以默默无闻地无条件吸纳各种冗余输出信息或输入请求的“回收站”,正如“黑洞”就像一个巨大的“宇宙垃圾场”一样
CMD没有神经错乱,是set处理的整数太大了,set使用双字节存储整数,有32位的存贮范围限制,也就是说它的处理范围是2^-31~2^31-1,你的磁盘空间超过了这个范围溢出了。
对此我没有太好的解决办法,只有舍弃后三位后除以1049的近似算法。
for /f "tokens=3" %%a in ('dir /-c c:\^|find "可用字节"') do set freesize=%%a
set /a freesize=%freesize:~0,-3%/1049>nul
echo Freesize:%freesize%
> 创建一个文件
>> 追加到一个文件后面
@ 前缀字符.表示执行时本行在cmd里面不显示, 可以使用 echo off关闭显示
^ 对特殊符号( > < &)的前导字符. 第一个只是显示aaa 第二个输出文件bbb
echo 123456 ^> aaa
echo 1231231 > bbb
() 包含命令
(echo aa & echo bb)
, 和空格一样的缺省分隔符号.
; 注释,表示后面为注释
: 标号作用
│ 管道操作
; 符号当命令相同的时候可以将不同的目标用;隔离开来但执行效果不变。如执行过程中发生错误则只返回错误报告但程序还是会继续执行
首先, @ 不是一个命令, 而是DOS 批处理的一个特殊标记符, 仅用于屏蔽命令行回显. 下面是DOS命令行或批处理中可能会见到的一些特殊标记符:
CR(0D) 命令行结束符
Escape(1B) ANSI转义字符引导符
Space(20) 常用的参数界定符
Tab(09) ; = 不常用的参数界定符
+ COPY命令文件连接符
* ? 文件通配符
"" 字符串界定符
| 命令管道符
< > >> 文件重定向符
@ 命令行回显屏蔽符
/ 参数开关引导符
: 批处理标签引导符
% 批处理变量引导符
其次, :: 确实可以起到rem 的注释作用, 而且更简洁有效; 但有两点需要注意:
第一, 除了 :: 之外, 任何以 :开头的字符行, 在批处理中都被视作标号, 而直接忽略其后的所有内容, 只是为了与正常的标号相区别, 建议使用 goto 所无法识别的标号, 即在 :后紧跟一个非字母数字的一个特殊符号.
第二, 与rem 不同的是, ::后的字符行在执行时不会回显, 无论是否用echo on打开命令行回显状态, 因为命令解释器不认为他是一个有效的命令行, 就此点来看, rem 在某些场合下将比 :: 更为适用; 另外, rem 可以用于 config.sys 文件中.
也可以使用以下的用法:
if exist command
device 是指DOS系统中已加载的设备, 在win98下通常有:
AUX, PRN, CON, NUL
COM1, COM2, COM3, COM4
LPT1, LPT2, LPT3, LPT4
XMSXXXX0, EMMXXXX0
A: B: C: ...,
CLOCK$, CONFIG$, DblBuff$, IFS$HLP$
具体的内容会因硬软件环境的不同而略有差异, 使用这些设备名称时, 需要保证以下三点:
1. 该设备确实存在(由软件虚拟的设备除外)
2. 该设备驱动程序已加载(aux, prn等标准设备由系统缺省定义)
3. 该设备已准备好(主要是指a: b: ..., com1..., lpt1...等)
可通过命令 mem/d | find "device" /i 来检阅你的系统中所加载的设备
另外, 在DOS系统中, 设备也被认为是一种特殊的文件, 而文件也可以称作字符设备; 因为设备(device)与文件都是使用句柄(handle)来管理的, 句柄就是名字, 类似于文件名, 只不过句柄不是应用于磁盘管理, 而是应用于内存管理而已, 所谓设备加载也即指在内存中为其分配可引用的句柄.
应该够用了吧,对于%这个符号为什么在批处理使用%%这样的形式,是因为批处理和命令的特性来决定的!!!希望这些能够帮助你们...........
Since the person upstairs said so, I'll post the functions of general symbols. You can take a good look. Actually, they are in the forum....
Your question is related to two elements:
1. % is an ESCAPE character, usually translated as an escape character, but there are more vivid translations like escape character, evasion character, etc. That is to say, % not only escapes the specific string related to it and replaces it with a specific string, but also itself will be "escaped". And similar to the escape character "\" in the C language, double % will be escaped and escaped to single %, and four % will be escaped to double %.
2. for itself is a special command, similar to a specialized command interpreter. Because its function implementation requires executing multiple statements, it must also have the function of analyzing and processing the command line (specifically, the command line after do). And when command/cmd implements for, it will naturally borrow its own original command line analysis module. Therefore, for has the characteristic of secondary escape. The statement after do in for is analyzed and interpreted in two levels. The first level is when command/cmd reads and interprets the for command line, and the second level is when for reads and interprets the do command. It usually interprets the same command line multiple times.
Then, we can notice that when using command line parameter variables and environment variables in do, double % are not needed. That is because these variables are replaced with specific constant strings after the first level of escape, and participate in all execution processes of the for loop; while the replacement variables require dynamic changes during execution (in the sub-command line after do), and this change naturally still needs to be realized through the escape character. Therefore, the use of double % becomes a necessity.
In addition, it should also be noted that when using for in the command line, double % are not needed. This stems from the different processing methods of the command interpreter for the command line and the batch processing. In the early DOS versions, % was not regarded as an escape character in the command line, so it would not be escaped and escaped. Therefore, at that time, it was impossible to directly reference environment variables in the command line. And when using for, only one % is needed for for to perform escape and escape. In the later versions of the command interpreter, support for command line escape (mainly support for environment variables) was added, but the tradition of using single % for command line for has been retained.
And the variable delayed expansion in cmd is a special case, but it does not violate the above escape principles. It's just that the environment variables in for are no longer constants.
rmdir /S /Q %mhnet% 2>NUL 1>NUL is simply explained
The general meaning of this code is to delete the directory specified by %mhnet%. /s means to delete the subdirectories in it. /q means not to prompt for confirmation when deleting the directory tree. 1>nul means to prohibit outputting the information of correctly deleting the directory tree. 2>nul means to prohibit outputting the error information during the deletion process
Among them, 1 and 2 are both representing the address of a certain data flow input and output (NT CMD calls it a handle, MSDOS calls it a device). The following table (quoted from the "Using Command Redirection Operators (Redirection Operators" section of the WinXP help document) will list the available handles.
Handle Handle number code Description
STDIN 0 Keyboard input
STDOUT 1 Output to the command prompt window
STDERR 2 Error output to the command prompt window
UNDEFINED 3-9 These handles are individually defined by applications and various specific tools
0 Keyboard input
1 Output to the command prompt window
2 Error output to the command prompt window
3-9 These handles are individually defined by applications and various specific tools.
2 > nul means that the information of the program error is not displayed.
call attrib -r -h c:\autoexec.bat >nul
In fact, this is:
call attrib -r -h c:\autoexec.bat 1 > nul
These 1, 2, 0, etc. are all handles. To put it simply, you just need to know that 1 represents the output information, 2 represents the error information, and 0 represents the keyboard input.
If you don't understand anything, you can read more help and support.
The command line does not make excessive restrictions on the position where the redirection symbol appears. As long as the redirection symbol is immediately followed by a "character device", the following statements are equivalent:
echo Hello World> hello.txt
echo Hello> Hello.txt World
echo> Hello.txt Hello World
> hello.txt echo Hello World
In the NT series command line, the scope of the redirection changes from the entire command line to a single command statement, and is restricted by the command separators &, &&, || and statement blocks.
echo Message1> msg1.txt & echo Message2> msg2.txt
if "%target%"=="" (echo message to screen ) else (echo message to file> %target%)
In summary, >nul means to redirect the standard output request generated by this command to the null device. And because of the silent characteristic of this device, it is equivalent to shielding the output information of this statement (not hiding it); and 2>nul is to redirect and shield the standard error information output request when the program executes in error. When used together, it means shielding all possible output information of this statement.
"Redirection" is a command line feature that has existed since MSDOS. It is responsible for redirecting the input and output requests of the specified command or statement from the default "console" to other "devices" to complete. Its start mark is that the "redirection symbol" (including ">", ">>", "<" three, their respective meanings are in ) appears in the sentence.
Generally, the input and output requests of command line programs are completed through three internally defined "ports" (called "handles" in NT, not defined in DOS), namely standard input stdin, standard output stdout, and standard error stderr. The devices they usually point to are the console (console, code is CON). Among them, stdin points to the keyboard of the console, and stdout/stderr points to the monitor of the console. Therefore, the console usually refers to the combination of the keyboard and the monitor, which is the concept reflected on the terminal of the early mainframe. Among them, stdin can be redirected by <, stdout can be redirected by >, >>, and stderr cannot be directly redirected in DOS. Only by transferring the system control to other devices through commands such as ctty or other commands can it be indirectly completed.
"Device" refers to a device driver or port code that can control PC hardware or ports. It is usually implemented and supported by the system bottom layer or hardware driver. For example, CON, CLOCK$ implemented by IO.SYS, CONFIG$ unknown device, first serial port AUX, first parallel port PRN, all serial ports COM1~COM4, all parallel ports LPT1~LPT3, available drive letters A:-X: and the null device NUL mentioned above. There are many other devices, such as XMSXXXX0 implemented by HIMEM.SYS, EMMXXXX0 implemented by EMM386.EXE, IFS$HLP$ implemented by IFSHLP.SYS, etc.
Among these devices, there are few that can process input and output information. Only the CON, NUL, and serial or parallel port devices connected with input and output hardware (printers, MODEM, etc.). They are called "character devices", and disk files are also listed as a special character device, which greatly expands the freedom and practicability of redirection, so many people also call redirection "file redirection".
The null device NUL is a special device. Because it has no controllable PC hardware or port, but is just a fictional device or port, it only exists at the software level. Because of this, it can accept all redirected input and output requests without giving any response (in NT, it will end the input request without giving any input information, and in DOS, it will repeatedly fill 127 bytes 0 and then terminate the response). This characteristic makes it very much like a "black hole" in astronomy that can devour all matter and information, and is also very similar to the "mysterious way" in philosophy that can reverse yin and yang and generate from nothing. It exists because we need a "recycling bin" that can unconditionally absorb various redundant output information or input requests silently, just as the "black hole" is like a huge "cosmic garbage dump".
CMD is not neurotic. It's that the integer processed by set is too large. set uses two-byte storage for integers and has a 32-bit storage range limit. That is to say, its processing range is 2^-31~2^31-1. Your disk space exceeds this range and overflows.
I don't have a very good solution for this. I can only use an approximate algorithm of discarding the last three digits and then dividing by 1049.
for /f "tokens=3" %%a in ('dir /-c c:\^|find "available bytes"') do set freesize=%%a
set /a freesize=%freesize:~0,-3%/1049>nul
echo Freesize:%freesize%
> Create a file
>> Append to the back of a file
@ Prefix character. Indicates that this line is not displayed in cmd when executing. You can use echo off to turn off the display
^ Leading character for special symbols (> < &). The first one just displays aaa, the second one outputs to file bbb
echo 123456 ^> aaa
echo 1231231 > bbb
() Contains commands
(echo aa & echo bb)
, and space are the default delimiters.
; Comment, indicating that the following is a comment
: Label function
│ Pipe operation
; When the commands are the same, you can separate different targets with ;, but the execution effect remains the same. If an error occurs during the execution process, only the error report is returned, but the program will continue to execute
First of all, @ is not a command, but a special marker in DOS batch processing, only used to shield the command line echo. The following are some special markers that may be seen in the DOS command line or batch processing:
CR(0D) Command line end character
Escape(1B) ANSI escape character guide character
Space(20) Common parameter delimiter
Tab(09) ; = Uncommon parameter delimiter
+ COPY command file connection character
*? File wildcard
"" String delimiter
| Command pipe character
< > >> File redirection character
@ Command line echo shielding character
/ Parameter switch guide character
: Batch processing label guide character
% Batch processing variable guide character
Secondly, :: can indeed play the role of rem comment, and it is more concise and effective; but there are two points to note:
First, except for ::, any character line starting with : is regarded as a label in batch processing, and the content after it is directly ignored. Just to distinguish it from normal labels, it is recommended to use a special symbol that cannot be recognized by goto, that is, a special symbol other than letters and numbers immediately after :.
Second, different from rem, the character line after :: will not be echoed during execution, no matter whether the command line echo state is turned on by echo on or not, because the command interpreter does not think it is a valid command line. In this regard, rem will be more applicable than :: in some occasions; in addition, rem can be used in the config.sys file.
The following usage can also be used:
if exist command
device refers to the device loaded in the DOS system. In win98, there are usually:
AUX, PRN, CON, NUL
COM1, COM2, COM3, COM4
LPT1, LPT2, LPT3, LPT4
XMSXXXX0, EMMXXXX0
A: B: C: ...,
CLOCK$, CONFIG$, DblBuff$, IFS$HLP$
The specific content will be slightly different due to different hardware and software environments. When using these device names, the following three points need to be ensured:
1. The device really exists (except for software virtual devices)
2. The device driver has been loaded (standard devices such as aux, prn are defined by the system by default)
3. The device is ready (mainly refers to a: b: ..., com1..., lpt1...)
You can use the command mem/d | find "device" /i to check the devices loaded in your system
In addition, in the DOS system, the device is also considered a special file, and the file can also be called a character device; because the device (device) and the file are both managed by handles (handle), the handle is the name, similar to the file name. The difference is that the handle is not applied to disk management, but to memory management. The so-called device loading also means allocating a referenceable handle for it in memory.
That should be enough. For why the % symbol is used in the form of %% in batch processing, it is determined by the characteristics of batch processing and commands!!! I hope these can help you...........
此帖被 +23 点积分 点击查看详情 评分人:【 redtek 】 | 分数: +2 | 时间:2006-11-8 04:43 | 评分人:【 sleet1986 】 | 分数: +1 | 时间:2007-1-13 07:17 | 评分人:【 ieutk 】 | 分数: +1 | 时间:2007-3-4 13:35 | 评分人:【 studythedos 】 | 分数: +1 | 时间:2007-3-24 04:22 | 评分人:【 haiou327 】 | 分数: +2 | 时间:2007-5-19 22:09 | 评分人:【 liuyun20 】 | 分数: +1 | 时间:2007-10-7 10:51 | 评分人:【 635635635 】 | 分数: +1 | 时间:2008-1-10 10:28 | 评分人:【 plp626 】 | 分数: +4 | 时间:2008-2-3 02:07 | 评分人:【 ytfy 】 | 分数: +2 | 时间:2008-2-19 18:46 | 评分人:【 jonsonqf 】 | 分数: +2 | 时间:2008-4-29 19:55 | 评分人:【 konkoo 】 | 分数: +2 | 时间:2009-2-8 15:29 | 评分人:【 wangfangjian 】 | 分数: +4 | 时间:2009-10-22 20:08 |
|
|