Do not allow a script to run individually

Hi All,

I am having two scripts say scriptA.ksh and scriptB.ksh.

The scriptB.ksh should be executed only if it is called from scriptA.ksh. If someone tries to execute it individually from command prompt, the script should exit gracefully with appropriate message.

Any workarounds?

Thanks in advance.

in scriptA.ksh call the scriptB.ksh with the some argument and check for that argument in scriptB.ksh and exit stating call the scriptA.ksh first..

$ cat a.sh
./b.sh M
$ cat b.sh
if [ "$1" != "M" ] ; then
        echo "Not called from A"
else
        echo "Called from A"
fi
$ ./a.sh
Called from A
$ ./b.sh
Not called from A
$

This is a good solution. Thanks. However, I am looking for a solution where I don't have to change the parameters.

$PPID contains the parent PID.

ps -p $PPID  -o "comm="
 

gives you the calling process name and path.

Thanks Tony. This is what I was looking for. Can you please help me understanding -o "comm=" in below command?

Please state what Operating System and version you have and what shell you use. Commands such as "ps" have different syntax on different Operating Systems ... and shell techniques vary from shell to shell.

An idea for a gash solution to your issue in any environment:
Create a flag file in the calling script and test for the presence of that file in the called script. If the flag file is not present the called script should exit gracefully, otherwise immediately delete the flag file and continue processing. (Purtists will note that there is a moment in time when someone could try to execute the called script from the command line. Also, someone with knowledge of the flag file name could create the file).
I depends on whether this is a security issue or basic accident prevention?
There will be better solutions using unix permissions.

Personally I'd pass an additional "activate" parameter as described by "expert". This method certainly protects against those who type "*" at the command line or those who type random commands.