rsk82
rsk82

Reputation: 29387

is it possible to distinguish when shell is run from command line or from other shell?

For example I can directly call myscript.cmd or in other script I can put a line to myscript.

The reason is that if a script is run on it's own it dissapears as soon as it stop executing, so I can't see the result, so at the end I must add @pause but when I run it from another shell this causes annoyance since console window wouldn't exit that way.

So I look for some kind of 'if' condition to address this issue.

Upvotes: 1

Views: 162

Answers (3)

newby2000
newby2000

Reputation: 142

To get your script paused when double-clicked (or by dropping files on it), but terminating the usual way when invoked from console:

@echo off

echo Hello World!

:: ...your ScriptCode...

(((echo.%CMDCMDLINE%)|find /I "%~0")>NUL)&&pause

Upvotes: 1

Carey Gregory
Carey Gregory

Reputation: 6846

Unless you create an environment variable like Stu suggested, you're not going to find any that do what you want. You're going to need to write a small program that queries the parent process programmatically and returns a value your script can check. If you're being run from Start->run your parent will be explorer.exe. Otherwise it will be cmd.exe or some other exe.

Sample code to find the parent process can be found here.

Upvotes: 1

Stu
Stu

Reputation: 15769

Why not set it yourself?

SET RUNNINGFROMOTHERSHELL=YES
CALL MYSCRIPT.CMD
SET RUNNINGFROMOTHERSHELL=

In MyScript.Cmd:

IF "%RUNNINGFROMOTHERSHELL%"=="" GOTO NOPAUSE
PAUSE
:NOPAUSE

Upvotes: 0

Related Questions