Batch file to determine if using Command Prompt

前端 未结 4 1727
时光说笑
时光说笑 2021-02-20 09:07

The last line in my batch file is pause. Is there any way to add a if condition to see if the script is run within command prompt or by double clicking to execute?

相关标签:
4条回答
  • 2021-02-20 09:18

    Use the tty command.

    Use the -s option and check the return value.

    0 讨论(0)
  • 2021-02-20 09:31

    By definition, a shell script is always going to be run in a "command prompt". But try using the SESSIONNAME env var - it seems to NOT be present if the script was started by double-clicking instead of manually running it from a prompt.

    0 讨论(0)
  • 2021-02-20 09:36
    CALL :GETMYSWITCH %CMDCMDLINE%
    IF /I "%MYSWITCH%" == "/C" ECHO I WAS STARTED IN THE EXPLORER & PAUSE
    IF /I NOT "%MYSWITCH%" == "/C" ECHO I WAS STARTED IN A DOS SESSION
    
    
    :GETMYSWITCH
    SET MYSWITCH=%2
    
    0 讨论(0)
  • 2021-02-20 09:37

    I know this is a year later but for future people searching you can use

    If /I "%COMSPEC%" == %CMDCMDLINE% Goto SkipPause
    pause
    :SkipPause
    

    It will skip the pause block if running from the command line and pause if running from batch file.

    0 讨论(0)
提交回复
热议问题