Script calling hierarchy


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script calling hierarchy
# 1  
Old 08-27-2008
Question Script calling hierarchy

If a.sh calls b.sh, how can we know inside b.sh that it was called by a.sh?
# 2  
Old 08-27-2008
The variable $PPID contains the PID of the parent process. You need to parse the output of ps (or know how to fish it out of the /proc file system if your OS has that) to find the name of the command of that PID.
# 3  
Old 08-27-2008
As long as this isn't for security reasons, finding out the name isn't so tough. Check your ps command, which might contain the option to spit out the command name of a given pid. For instance, on Linux, I can do this:

ps --no-header -o cmd $PPID
# 4  
Old 08-27-2008
Thanks both of you who've replied. I've successfully verified its utility. See below:

Code:
[root@IMITS174 perl_scripts]# cat a.sh
#!/bin/sh

sh b.sh
[root@IMITS174 perl_scripts]#
[root@IMITS174 perl_scripts]#
[root@IMITS174 perl_scripts]# cat b.sh
#!/bin/sh

echo "I am b"
parent_name=`ps --no-header -o ucmd $PPID`
echo "Parent PID is $PPID"
echo "Parent is $parent_name"
[root@IMITS174 perl_scripts]#
[root@IMITS174 perl_scripts]# ./a.sh
I am b
Parent PID is 18251
Parent is a.sh
[root@IMITS174 perl_scripts]#
[root@IMITS174 perl_scripts]#
[root@IMITS174 perl_scripts]# ./b.sh
I am b
Parent PID is 2014
Parent is bash

Note however: I've changed "cmd" to "ucmd" to make sure I don't get the annoying "/bin/sh" prefix to the calling program a.sh.

Thanks once again! Hope the above snippet helps anyone who wants to use it.
# 5  
Old 08-27-2008
By the way, what would you do in case of Windows? Assume instead of .sh, you are dealing with .pl....?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

DB2 Query to pick hierarchy values

Dear Team I am using DB2 v9 . I have a condition to check roles based on hierarchies like below example. 1.Ramesh has Roles as "Manager" and "Interviewer" 2.KITS has Roles as "Interviewer" 3.ANAND has Roles as "Manager" and "Interviewer" select * FROM TESTING NAME ... (6 Replies)
Discussion started by: Perlbaby
6 Replies

2. UNIX for Beginners Questions & Answers

Building hierarchy with the list

Hi All, Sorry for more question today. I am having a text file . Like below 704925680_TOTAL->MANUAL->TT IOR GSB 775116444_TOTAL->POO TO->TT -572275295_TOTAL->MANUAL->MTO -611408278_TOTAL->PRIE LEL 456690129_TOTAL->BTT TOO 475919266_TOTAL->MANUAL->COM -172680236_TOTAL->BTT TOO->MTO... (15 Replies)
Discussion started by: arunkumar_mca
15 Replies

3. Shell Programming and Scripting

Running a unix script(which is calling another script inside that) in background

Hi all, I am having a script ScriptA which is calling a script ScriptB in the same server and copying files to second server and have to execute one script ScriptC in the second server. THis First script ScriptA is the main script and i have to execute this process continously. for Keeping... (2 Replies)
Discussion started by: rohithji
2 Replies

4. Solaris

Why are so many dirs used in solaris hierarchy?

Hi all, I would like to know the difference between the different dir structures present in solaris!!! Meaning what does /usr contain, /etc ,/opt/ ,so on... I know what /usr and /etc are used for. But why are /opt /bin /sbin /var and many more that i have missed I would appreciate if... (1 Reply)
Discussion started by: wrapster
1 Replies

5. Shell Programming and Scripting

Need to search and replace in multiple files in directory hierarchy

Hello all I need to search and replace in multiple files that are in directory hierarchy Im using the : find . -name "*.dsp" -print | xargs grep -n -o Test.lib" , I like to be able to replace every instance of Test.lib with empty space . how can I write one liner that does this ? (3 Replies)
Discussion started by: umen
3 Replies

6. Tips and Tutorials

Linux Filesystem Hierarchy

Hi, Please have a look this: http://tldp.org/LDP/Linux-Filesystem-Hierarchy/Linux-Filesystem-Hierarchy.pdf I think this can be very useful for a beginner/intermediate level user to understand the filesystem hierarchy and as well as it can be used as a reference to various linux commands and... (0 Replies)
Discussion started by: tayyabq8
0 Replies

7. UNIX for Dummies Questions & Answers

cp without maintaining the soucre directory tree hierarchy

Hi guys. I'm willing to copy a specific file system hierarchy, but I would not like to maintain the directory tree organization. For example: Let's say /a/b/c is the fs I'm wanting to copy to my destination, and that c is a directory with 30 files, 10 on /a/b/c , 10 on a/b/c/c1 and 10... (2 Replies)
Discussion started by: 435 Gavea
2 Replies

8. Shell Programming and Scripting

script to archive certain folders in a hierarchy

I'm new to shell scripting and I'm having a tough time figuring out how to script something. Can anyone help? Here is my setup and what I want to do: A directory contains a list of projects by year (2000, 2001, etc) and customers (01-001) all of which have the same internal directory setup... (3 Replies)
Discussion started by: medazinol
3 Replies
Login or Register to Ask a Question