![]() |
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-08-02 05:43 |
48,037 topics / 350,122 posts / today 2 new / 48,250 members |
| DOS批处理 & 脚本技术(批处理室) » Why does the for loop only display the state of echo? |
| Printable Version 1,731 / 6 |
| Floor1 pcl83 | Posted 2006-10-03 09:44 |
| 初级用户 Posts 13 Credits 32 | |
|
Please see the following code (in a batch file):
------------------------------------------- @echo off FOR /L %%i in (1,1,3) do ( set /a T=%% echo %T%) pause>nul -------------------------------------------- After the program runs, it does not appear as we predicted: 1 2 3 Instead: ECHO is off. ........ What's going on and what's the solution? ──────────────── Moderation Record ──────────────── Performed by: namejm Original title: Expert, come quickly for help Current title: Why does the for loop only display the echo status? Explanation: The original title was too vague, which was not conducive to forum search and management. After the moderator's prompt was issued three days ago, the original poster had not changed the title. Now I, on behalf of the helpless moderator, forcibly modified the title and deducted 4 points from the original poster (because the forum points rules have changed, and the punishment has been reduced appropriately). ──────────────── Moderation Record ──────────────── [ Last edited by namejm on 2006-10-19 at 01:09 ] |
|
| Floor2 pengfei | Posted 2006-10-03 09:47 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
This is a problem of variable delay. In addition, it is found that the code of the original poster is wrong, set /a T=%%. The code modification is as follows:
|
|
| Floor3 pcl83 | Posted 2006-10-03 09:53 |
| 初级用户 Posts 13 Credits 32 | |
|
Thanks, endless gratitude
|
|
| Floor4 KIKOOOO | Posted 2006-10-03 10:01 |
| 初级用户 Posts 12 Credits 34 | |
|
Why use!T! here instead of %T%?
|
|
| Floor5 pengfei | Posted 2006-10-03 10:14 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
This involves the issue of environment variable delay. For a compound statement like this, cmd will preprocess the variables in the statement. Thus, %T% in the for statement will be replaced. And the variable %T% is not defined before the for, so echo %T% shows that echo is in a closed state.
To enable delayed environment variables is to prevent cmd from preprocessing environment variables in compound statements. Thus, the variable T can be displayed normally. Because environment variables are enabled, so change % to !... |
|
| Floor6 无奈何 | Posted 2006-10-03 10:51 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃▓ China DOS Union >> Batch Processing Room Moderation Management _ □ ×┃ ┠──────────────────────────────┬┨ ┃Microsoft Windows XP │┃ ┃(C) Copyright 1985-2001 Microsoft Corp. │┃ ┃ │┃ ┃C:\NaiWaiHe>Moderation Management.cmd │┃ ┃ │┃ ┃ Moderator's Notice │┃ ┃To facilitate forum users' browsing and moderators' management, please modify the title of this topic to briefly describe the content or intention of the topic. If you confirm that the title does not need to be modified, please continue to reply to this topic for defense. If the defense reason is sufficient, this notice will be canceled. If you confirm that the title needs to be modified, please click the "Edit" button below the first post of this topic, and modify the title bar in the editing page. If you still do not appeal or modify within three days after this notice is issued, the moderator will modify it and impose a corresponding penalty on the author. The penalty is to deduct 8 points, including 6 points recovered from the rewards you received for publishing the topic and 2 points deducted punitively for the title violation. After knowing that you have made a positive and appropriate response, the moderator will delete this notice within three working days to eliminate the adverse impact on you.│┃ ┃ │┃ ┃C:\NaiWaiHe>▂ │┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷┛ |
|
| Floor7 不得不爱 | Posted 2006-10-03 21:58 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
Here'stown共同成意义是什么说的是什么意义都公共意义});!T!的成意义是什么说的是什么意义都公共意义});!T!的
``` @echo off FOR /L %%i in (1,1,3) do set a=%%i&call :1 pause>nul goto :eof :1 echo %a% ``` [ Last edited by qwe1234567 on 2006-10-3 at 22:05 ] Wait, there seems to be some confusion in the input. Let's focus on the code part. The code you provided is a batch script. Here's the translation of the code part as is: ``` @echo off FOR /L %%i in (1,1,3) do set a=%%i&call :1 pause>nul goto :eof :1 echo %a% ``` And the rest of the text that doesn't seem to be related to the code is a bit jumbled. But based on the requirements, we mainly handle the code part. So the translated content for the code part remains as above, and the other jumbled text is just as it is presented here. But actually, the correct way is to just output the relevant parts properly. Let's correct and present clearly: The batch code part translated is: ``` @echo off FOR /L %%i in (1,1,3) do set a=%%i&call :1 pause>nul goto :eof :1 echo %a% ``` And the edit information part remains as: [ Last edited by qwe1234567 on 2006-10-3 at 22:05 ] |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |