Re wind_7th:
首先,纯DOS平台和NTs'CMD都不具有简单、直接而且强有力的保存程序输出结果到变量(这里指环境变量)的特性或者命令。
其次,我们可以通过一些命令的简单组合,完成最简单的保存任务,即保存输出结果的第一行内容到环境变量。
在 NTs'CMD 下,可以使用以下代码(令人疑惑的是,set /p 支持重定向而不支持管道,不知有谁可以解惑?),当然无奈和兄提供的 for /f 的方案也是很好的。
command arguments > output.txt
set /p output= < output.txt
del output.txt
在纯DOS下,我最早采用的是预先编写环境声明头文件的方案,具体细节见;不久,鉴于此方案必须事先编辑头文件的缺点,我就改用 debug 来动态生成这个头文件,实现了脚本的自给自足的完全自动化,代码见中1、2、3楼的程序,它们均是实现对 dir 命令输出的保存和分析来完成相应的变量操作。其中的 :init 段便是此原理的集中应用。后来我又尝试过其它 edlin/date/prompt等诸多方案,但均有局限,使用于不同的应用场合,都不如 debug 来的通用。
最后,如果要实现更复杂的变量保存,比如保存指定行、指定单词、指定列的文本,或者过滤诸如 管道、重定向等一些命令行特殊符号,就需要使用更复杂的代码。
在 NTs'CMD下,可以使用 set、for、find 等内部命令对文本内容进行控制。而在纯DOS下,这种控制的实现是通过更复杂的 debug 脚本再加上 find 对文本内容(见中的最后两个代码);或者使用诸如 LMOD、NSET 、strings、awk、sed 等第三方工具来实现这些任务。
以上提供了诸多方案,但是请记住,没有一个程序或者方案能完善无缺、简洁有效的处理所有问题。我们应该首先熟悉这些方案以及它们的优缺点,然后根据再具体环境斟酌选择。
参考资料:
请问如何将一个命令的输出赋给一个变量(SET头文件)
http://www.cn-dos.net/forum/viewthread.php?tid=6318
请教如何让一个变量来存放一个命令的结果(LMOD)
http://www.cn-dos.net/forum/viewthread.php?tid=14250
如何用echo命令输出另外一个命令的执行结果(LMOD)
http://www.cn-dos.net/forum/viewthread.php?tid=16748
如何将txt文件中的内容赋值给变量?(NSET)
http://www.cn-dos.net/forum/viewthread.php?tid=12413
三个进行文本遍历操作的批处理程序(后增补为6个)
http://www.cn-dos.net/forum/viewthread.php?tid=9588
Re wind_7th:
First of all, both the pure DOS platform and NTs' CMD do not have the simple, direct, and powerful feature or command to save the output result of a program to a variable (here refers to an environment variable).
Secondly, we can complete the simplest saving task through a simple combination of some commands, that is, save the content of the first line of the output to an environment variable.
Under NTs' CMD, the following code can be used (what is confusing is that set /p supports redirection but not pipeline, I wonder if anyone can explain? ), of course, the for /f solution provided by Nai Wai He is also very good.
command arguments > output.txt
set /p output= < output.txt
del output.txt
Under pure DOS, the earliest solution I adopted was the scheme of pre-writing an environment declaration header file, and the specific details are in ; soon, in view of the shortcoming that this scheme must edit the header file in advance, I switched to using debug to dynamically generate this header file, realizing the fully automated self-sufficiency of the script. The code is in the programs on floors 1, 2, and 3 in . They all implement the corresponding variable operations by saving and analyzing the output of the dir command. The :init segment is the concentrated application of this principle. Later I also tried many other schemes such as edlin/date/prompt, but they all have limitations and are used in different application scenarios, and none is as universal as debug.
Finally, if you want to implement more complex variable saving, such as saving the text of a specified line, specified word, specified column, or filtering some command line special symbols such as pipeline, redirection, etc., more complex code is required.
Under NTs' CMD, internal commands such as set, for, find can be used to control the text content. And under pure DOS, the realization of this control is through more complex debug scripts plus find for the text content (see the last two codes in ); or using third-party tools such as LMOD, NSET, strings, awk, sed to implement these tasks.
The above provides many solutions, but please remember that no program or solution can perfectly and concisely and effectively handle all problems. We should first be familiar with these solutions and their advantages and disadvantages, and then choose according to the specific environment.
References:
How to assign the output of a command to a variable (SET header file)
http://www.cn-dos.net/forum/viewthread.php?tid=6318
How to let a variable store the result of a command (LMOD)
http://www.cn-dos.net/forum/viewthread.php?tid=14250
How to use the echo command to output the execution result of another command (LMOD)
http://www.cn-dos.net/forum/viewthread.php?tid=16748
How to assign the content in a txt file to a variable? (NSET)
http://www.cn-dos.net/forum/viewthread.php?tid=12413
Three batch programs for text traversal operations (later supplemented to 6)
http://www.cn-dos.net/forum/viewthread.php?tid=9588