Aac*_*ini 18
这对我来说是一个有趣的话题!我想对此做一些观察.
重要的一点是:批处理文件是扩展名为.BAT或.CMD的文件.期.批处理文件除了执行常用的DOS命令外,还可以实现某些特定的批处理文件工具,特别是:
现在有趣的部分:任何文件都可以重定向为CMD.exe的输入,因此其中包含的DOS命令以与批处理文件类似的方式执行,但有一些差异.最重要的一点是以前的批处理文件工具不起作用.另一个差异在下面的NOT-Batch文件中说明(我称之为BATCH.TXT):
@echo off
rem Echo off just suppress echoing of the prompt and each loop of FOR command
rem but it does NOT suppress the listing of these commands!
rem Pause command does NOT pause, because it takes the character that follows it
pause
X
rem This behavior allows to put data for a SET /P command after it
set /P var=Enter data:
This is the data for previous command!
echo Data read: "%var%"
rem Complex FOR/IF commands may be assembled and they execute in the usual way:
for /L %i in (1,1,5) do (
set /P line=
if "!line:~0,6!" equ "SHOW: " echo Line read: !line:~6!
)
NOSHOW: First line read
SHOW: Second line
NOSHOW: This is third line
SHOW: The line number 4
NOSHOW: Final line, number five
rem You may suppress the tracing of the execution redirecting CMD output to NUL
rem In this case, redirect output to STDERR to display messages in the screen
echo This is a message redirected to STDERR >&2
rem GOTO command doesn't work:
goto label
goto :EOF
rem but both EXIT and EXIT /B commands works:
exit /B
:label
echo Never reach this point...
Run Code Online (Sandbox Code Playgroud)
要执行上一个文件,请键入:CMD/V:ON <BATCH.TXT需要/ V开关才能启用延迟扩展.
更专业的差异与NOT-Batch文件中的命令在命令行上下文中执行,而不是批处理文件上下文有关.也许戴夫或杰布可以详细阐述这一点.
编辑: 附加观察(batch2.txt):
@echo off
rem You may force SET /P command to read the line from keyboard instead of
rem from following lines by redirecting its input to CON device.
rem You may also use CON device to force commands output to console (screen),
rem this is easier to write and read than >&2
echo Standard input/output operations> CON
echo/> CON
< CON set /P var=Enter value: > CON
echo/> CON
echo The value read is: "%var%"> CON
Run Code Online (Sandbox Code Playgroud)
以这种方式执行以前的文件:CMD <BATCH2.TXT> NUL
编辑:更多其他观察(batch3.txt)
@echo off
rem Dynamic access to variables that usually requires DelayedExpansion via "call" trick
rem Read the next four lines; "next" means placed after the FOR command
rem (this may be used to simulate a Unix "here doc")
for /L %i in (1,1,4) do (
set /P line[%i]=
)
Line one of immediate data
This is second line
The third one
And the fourth and last one...
(
echo Show the elements of the array read:
echo/
for /L %i in (1,1,4) do call echo Line %i- %line[%i]%
) > CON
Run Code Online (Sandbox Code Playgroud)
以通常的方式执行此文件:CMD <BATCH3.TXT> NUL
有趣!不是吗?
编辑:现在,可以在NotBatch.txt文件中模拟GOTO和CALL命令!看这篇文章.
安东尼奥
只需使用:
type mybat.txt | cmd
Run Code Online (Sandbox Code Playgroud)
打破它...
type mybat.txt作为文本文件读取mybat.txt并打印内容。say|捕获左侧命令打印的任何内容,并将其作为输入传递给右侧命令。然后cmd(正如您可能猜到的那样)将其接收到的任何输入解释为命令并执行它们。
如果您想知道...您可以替换cmd为bash在 Linux 上运行。
| 归档时间: |
|
| 查看次数: |
13007 次 |
| 最近记录: |