有条件的暂停(而不是在命令行) [英] Conditional PAUSE (not in command line)

查看:80
本文介绍了有条件的暂停(而不是在命令行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个最后的暂停在我的* .BAT脚本,这样我就可以在Windows资源管理器只需双击它们,必须读取输出的机会。不过,最终PAUSE是一个烦恼,当我运行命令行相同的脚本。

有什么方法来检测我们是否在命令提示符下运行脚本(或没有),插入PAUSE(或没有)相应?

(目标环境是Windows XP和更高)。

更新

我已经成功地从安德斯的回答组成的:

 (((回声%CMDCMDLINE%)|找到/ I%〜0)方式> NUL)
如果%ERRORLEVEL%EQU 0(
    设置GUI = 1
)其他(
    设置CLI = 1

然后,我可以做的东西是这样的:

 如果定义GUI暂停


解决方案

 关闭@echo
echo.Hello世界
(((回波%CMDCMDLINE%)|找到/ I%〜0)方式> NUL)及&放大器;暂停

... NT +仅在Win9x的任何%CMDCMDLINE%可能。

为e M在评论中指出,把所有这一切都在一个行会打开你到一些边缘情况下,%CMDCMDLINE%将跳脱出括号。解决方法是使用两行:

 关闭@echo
echo.Hello世界回声%CMDCMDLINE%。|查找/ I%〜0> NUL
如果没有ERRORLEVEL 1暂停

I like to have a final PAUSE in my *.bat scripts so I can just double click on them in Windows explorer and have the chance to read the output. However, the final PAUSE is an annoyance when I run the same script from the command line.

Is there any way to detect whether we are running the script from a command prompt (or not) and insert the PAUSE (or not) accordingly?

(Target environment is Windows XP and greater.)

Update

I've managed to compose this from Anders's answer:

(((echo.%cmdcmdline%)|find /I "%~0")>nul)
if %errorlevel% equ 0 (
    set GUI=1
) else (
    set CLI=1
)

Then, I can do stuff like this:

if defined GUI pause

解决方案

@echo off
echo.Hello World
(((echo.%cmdcmdline%)|find /I "%~0")>nul)&&pause

...NT+ only, no %cmdcmdline% in Win9x probably.

As pointed out by E M in the comments, putting all of this on one line opens you up to some edge cases where %cmdcmdline% will escape out of the parenthesis. The workaround is to use two lines:

@echo off
echo.Hello World

echo.%cmdcmdline% | find /I "%~0" >nul
if not errorlevel 1 pause

这篇关于有条件的暂停(而不是在命令行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆