我有 2 个批处理脚本:
sleep 1.bat
echo sleep 4 seconds
timeout /t 4
exit
sleep 2.bat
echo sleep 3 seconds
timeout /t 3
exit
是否可以连续执行sleep1.bat
和sleep2.bat
,让程序总共休眠7秒?
sleep 主程序
call sleep1.bat
call sleep2.bat
pause
SleepMain.bat
无法执行我想要的操作。它只执行slee1.bat
,因为sleep1.bat
包含exit
。
sleep1.bat
和sleep2.bat
是别人写的,我希望我不必更改它们。
最佳答案
是的,exit
肯定会退出,但它退出的是控制台,而不是当前的批处理脚本。
要防止这种情况发生,请使用exit/b
。
提示:
- Use
exit
only in the main script.- If you're just doing some simple thing in sleep1.bat, you can choose to not to add an
exit /b
command.
Try it yourself : 1.bat -set "you=you"
, 2.bat -set "you=you" & exit /b
In 3.bat, try eithercall 1.bat & echo %you%
orcall 2.bat & echo %you%
, you will still getyou
as the output. This mean either script withexit /b
or none still can access the variables in the previous called batch script. (Safe to try with simple script)
关于windows - 连续执行2个批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33560695/