China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 02:22
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 请问批处理中如何实现命令结果传递给参数? How to pass the result of a command to a parameter in batch processing? View 1,807 Replies 9
Original Poster Posted 2005-11-18 22:02 ·  中国 广东 深圳 南山区 电信
初级用户
Credits 142
Posts 13
Joined 2004-07-04 00:00
21-year member
UID 27830
Gender Male
Status Offline
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!
Floor 2 Posted 2005-11-19 17:49 ·  中国 广东 深圳 南山区 电信
初级用户
Credits 142
Posts 13
Joined 2004-07-04 00:00
21-year member
UID 27830
Gender Male
Status Offline
Hope experts come to help, thank you
Floor 3 Posted 2005-11-22 12:39 ·  中国 广东 深圳 电信
初级用户
Credits 142
Posts 13
Joined 2004-07-04 00:00
21-year member
UID 27830
Gender Male
Status Offline
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!
Floor 4 Posted 2005-11-22 21:16 ·  中国 广东 深圳 南山区 电信
初级用户
Credits 142
Posts 13
Joined 2004-07-04 00:00
21-year member
UID 27830
Gender Male
Status Offline
Bump again, please ask experts for guidance, thanks again!
Floor 5 Posted 2005-11-23 00:31 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
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.

for /f "tokens=1 delims=:" %i in ('findstr/n /r "." list.txt') do @set NUM=%i
Floor 6 Posted 2005-11-23 08:45 ·  中国 广东 深圳 南山区 电信
初级用户
Credits 142
Posts 13
Joined 2004-07-04 00:00
21-year member
UID 27830
Gender Male
Status Offline
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)
Floor 7 Posted 2005-11-23 09:49 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
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.
Floor 8 Posted 2005-11-23 23:23 ·  中国 广东 深圳 电信
初级用户
Credits 142
Posts 13
Joined 2004-07-04 00:00
21-year member
UID 27830
Gender Male
Status Offline
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 :)
Floor 9 Posted 2005-11-24 17:20 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 10 Posted 2005-11-24 21:27 ·  中国 广东 深圳 南山区 电信
初级用户
Credits 142
Posts 13
Joined 2004-07-04 00:00
21-year member
UID 27830
Gender Male
Status Offline
Thanks to the moderator boss for the reply! Studying... : )

Thanks!
Forum Jump: