![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-23 16:03 |
47,812 topics / 349,917 posts / today 0 new / 48,273 members |
| DOS批处理 & 脚本技术(批处理室) » 请问批处理中如何实现命令结果传递给参数? How to pass the result of a command to a parameter in batch processing? |
| Printable Version 2,058 / 9 |
| Floor1 wind_7th | Posted 2005-11-18 22:02 |
| 初级用户 Posts 13 Credits 142 | |
|
How to set parameters similar to the shell script in Unix in a batch file?
For example, define the NUM parameter in a bash shell script: NUM='wc -l test.txt' I'm not quite sure if the batch file of DOS can directly call the result of a certain command when defining parameters? Thanks! |
|
| Floor2 wind_7th | Posted 2005-11-19 17:49 |
| 初级用户 Posts 13 Credits 142 | |
|
Hope experts come to help, thank you
|
|
| Floor3 wind_7th | Posted 2005-11-22 12:39 |
| 初级用户 Posts 13 Credits 142 | |
|
Bump it again...
If you change the requirement, how to assign the content of a text file to a parameter? It's easy under Unix, but it's not easy to implement in DOS scripts. Please guide by DOS experts, thanks! |
|
| Floor4 wind_7th | Posted 2005-11-22 21:16 |
| 初级用户 Posts 13 Credits 142 | |
|
Bump again, please ask experts for guidance, thanks again!
|
|
| Floor5 无奈何 | Posted 2005-11-23 00:31 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
Try using for to parse text under NT. Do you want to get the number of lines in the text and assign it to a variable? This code can do it, which is much more cumbersome than under unix.
|
|
| Floor6 wind_7th | Posted 2005-11-23 08:45 |
| 初级用户 Posts 13 Credits 142 | |
|
Thank you, friend Wu Nai He! But what I mean is not to get the number of lines of text, but to read the text content and assign it to a variable in a batch file.
I learned that the read in the string command can read the content of a specified file and assign it to a variable, but I don't know if there are other better methods. In fact, my final question is to know: How can the batch command under DOS assign the execution result to a variable? (In the unix environment, it is very easy to achieve, just define it directly) |
|
| Floor7 无奈何 | Posted 2005-11-23 09:49 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
I gave a specific example of capturing command output, and the function is to get the number of lines of a file. The original command is findstr/n /r "." list.txt. You can look at the for help yourself, see for /? This can't be explained in a few words.
|
|
| Floor8 wind_7th | Posted 2005-11-23 23:23 |
| 初级用户 Posts 13 Credits 142 | |
|
Thanks to friend Wunaaihe, I was originally more familiar with Unix shell scripts and not very familiar with DOS batch commands. Thank you for your guidance. I'll first look at the usage of for, and hope to communicate again later :)
|
|
| Floor9 willsort | Posted 2005-11-24 17:20 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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 |
|
| Floor10 wind_7th | Posted 2005-11-24 21:27 |
| 初级用户 Posts 13 Credits 142 | |
|
Thanks to the moderator boss for the reply! Studying... : )
Thanks! |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |