How to ensure a script can only be invoked from another?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to ensure a script can only be invoked from another?
# 1  
Old 03-18-2012
How to ensure a script can only be invoked from another?

Hi All,

I have two scripts - ScriptA and ScriptB

ScriptA has logic to invoke ScriptB :
- with some parameter
- or without any parameter

ScriptB can also be invoked by the user from the command line.

Is there anyway to ensure that when I execute ScriptB from the command line, it does not work and shows an error saying that ScriptB cannot be run automatically but can only be invoked via ScriptA?

Thanks In Advance.
# 2  
Old 03-18-2012
Depending on just how complex you need to get, it could be as simple as setting an environment variable in scriptA and checking for it in scriptB.

Code:
export Xppid=$$               # script A exports it's pid

and in scriptB:

Code:
if [[ "$Xppid" != "$PPID" ]]
then
   echo "cannot run, not invoked by scriptA"
   exit 1
fi

Of course, the user could export their PID just like the script, but if you aren't concerned with users trying to skirt the system, then this is easy and it does the trick. There might be other, more "secure," methods, but my brain isn't coming up with any at the moment.
# 3  
Old 03-18-2012
Please post what Operating System and version you have and what Shell you are using.

Ps. agama's solution is neat.
We use a similar solution with a manadatory parameter $1 such that the script exits with an error if the script is not invoked with that parameter.
Paying proper use of file permissions on script files also helps. Make use of groups.
# 4  
Old 03-18-2012
We could combine these two methods and test for:

Code:
if [ "$1" != "$PPID" ]
then
   echo "cannot run, not invoked by scriptA"
   exit 1
fi

and call scriptB from scriptA like this:
Code:
/path/to/dir/scriptB $$

But this is just a tiny difference of course.

We could also test ps output if the PPID has a command field that shows the script name. but I don't think this is universal across implementations..

Last edited by Scrutinizer; 03-18-2012 at 09:17 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oracle function invoked from shell script doubt

hi gurus, I have tried myself to invoke an oracle function. there are three different function available need to be called for differnt. can you tell me whether the below code is correct to call oracle function from shell script. Any help would be highly appreciated. cat location.sh ... (5 Replies)
Discussion started by: arun888
5 Replies

2. Shell Programming and Scripting

Problem with script invoked from a keyboard shortcut

-EDIT- I have solved my problem below by using a different program. Instead of xsel I am using xclip which basically does the same thing and works fine from a script invoked by a global hotkey. -END EDIT- Hi, I've written a simple script to copy my email address into both the... (0 Replies)
Discussion started by: gencon
0 Replies

3. Shell Programming and Scripting

Script invoked using "sh" is not executing. Urgent help required

Hi , I am new to shell scripting. I am using Linux for doing scripting. Below is my script, which takes 2 parameters as input. test.sh has the below: #!/bin/bash . $HOME/.profile gpg --yes --no-use-agent -r "$(eval echo \$$2_Var)" -e $1 1st parameter is command line... (7 Replies)
Discussion started by: rangarb
7 Replies

4. Shell Programming and Scripting

CRON: Script not getting invoked

Hi, I have the following script - fixpart="/files/myScript # Transfer Script" echo "Specify the transfer frequency in minutes - " echo "every 1, 2, 3, or 5 minutes (default every 1 minute) " echo $nn "Frequency ? :" $cc read ans case $ans in 2) echo... (9 Replies)
Discussion started by: angshuman_ag
9 Replies

5. UNIX for Advanced & Expert Users

why the script name not displayed and not sh invoked?

Say there is a shell script named test.sh. I intentionally omit the #! line in test.sh for testing perpose. I did the following : $ echo $0 -ksh ---> current shell $ echo $$ 12919 ---> PID of the current shell... (4 Replies)
Discussion started by: hongwei
4 Replies

6. UNIX for Advanced & Expert Users

Passing the values to the secondary script when it invoked by primary script

Hi, When I invoke a script s1.sh it will call an another script s2.sh by itself. This script s2.sh will call some java files, so while running the script it asks for a file name to be processed. Which we can see in the screen. The file name to be processed is present in script s1.sh Now I... (2 Replies)
Discussion started by: venu_eie
2 Replies

7. Shell Programming and Scripting

Passing the values to the secondary script when it invoked by primary script

Hi, When I invoke a script s1.sh it will call an another script s2.sh by itself. This script s2.sh will call some java files, so while running the script it asks for a file name to be processed. Which we can see in the screen. The file name to be processed is present in script s1.sh Now... (1 Reply)
Discussion started by: venu_eie
1 Replies

8. Shell Programming and Scripting

determine if the script has been invoked manually or not?

Hi, Is there a way to determine if the script has been invoked manually or not( might be invoked by a schedular or crontab)? Thanks, (8 Replies)
Discussion started by: hitmansilentass
8 Replies

9. Shell Programming and Scripting

script to ensure lines are in sequence

Hello there, I check files containing more than 2000 lines, I try to make sure that the lines are in sequence...for example the first line begins with Av567, second line contains Av568 and so on up to the last line that may contain Av2500. I need a script to check that all lines are in sequence... (5 Replies)
Discussion started by: docaia
5 Replies
Login or Register to Ask a Question