?
Solved

Determine the name of a calling Bash script .

Posted on 2010-01-06
9
Medium Priority
?
2,280 Views
Last Modified: 2013-12-26
I have a Bash setup script which is called by many other scripts.  For example I have a script named myScript1 that looks like:

#!/bin/bash
. ./setup.sh

How can setup.sh determine the name of the script that called it?
Thanks - Terry
0
Comment
Question by:TerryNJones
9 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 26194216
If you invoke it with .
you can find the name of the caller in $0

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 26194266
The whole path is in $0.

Find the filename alone with

scriptname=$(basename $0)echo $scriptname

0
 
LVL 7

Expert Comment

by:hemmi
ID: 26194510
That's total nonsense: $0 will of course only show setup.sh itself and not the calling script!
The viriable PPID will hold the parents process id.
Under
/proc/$PPID
you will find everything you want to know about the calling process!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 26194542
OK,
 
 it seems that you want to find out the name of the calling ("parent") script!
 
 There is a variable containing the PID of the parent, which you could evaluate in the following way:

parent=$(ps -f -p $PPID | tail -1 | awk '{print $NF}')

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 26194575
... and again for the scriptname alone:

 parentfile=$(basename $(ps -f -p $PPID | tail -1 | awk '{print $NF}'))
0
 

Author Closing Comment

by:TerryNJones
ID: 31673696
Exactly what I needed!  Thanks.
0
 
LVL 7

Expert Comment

by:hemmi
ID: 26194647
It's not that easy, woolmilkporc ... That why I proposed to use all information on the calling process.
If the calling process had been call with some parameters, say
myscript -p1 -p2
then look what your "solution" does ...

0
 
LVL 6

Expert Comment

by:ollfried
ID: 26194738
@hemmi: $0 (better: ${0}) together with basename was the correct answer. Please do not other people the way you do and call solutions nonsense.
0
 
LVL 7

Expert Comment

by:hemmi
ID: 26195158
I'm sorry. I did not see the leading "." in the original post. So it seems guaranteed, that he's always sourcing the script and not calling the script!
Still - in case of actually calling - your basename will not work in all cases.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question