当前位置: 动力学知识库 > 问答 > 编程问答 >

shell - Windows - preventing console window from closing if errors occured

问题描述:

I'm writing a simple .bat script and I want it to open it by double click from Windows Explorer. If opened from Windows Explorer, console window automatically closes after script is finished.

The problem is that errors may occur from commands that I invoke in a script. In this case these comands print out error messages and return exit status 1. But user don't know about it because Window closes too fast to notice if some error messages popped out (and definitely too fast to read them ;) ).

So is there any way to prevent console window from closing if some command in script failed (so that user can read error message) and still having it automatically closed if everything worked fine? If the second part is not possible, it's okay to just prevent console window to close automatically in both cases (either failure or success).

网友答案:

It would be much easier to go with the latter. To keep the console window open put this at the end of your script:

pause >nul

That will pause the window from closing. Then you can handle any errors you want to display and presumably echo them to the user before the pause.

Example:

echo No errors or Errors found!

pause >nul

Hope this helps!

分享给朋友:
您可能感兴趣的文章:
随机阅读: