Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sourced file - get file name, exit to parent

Status
Not open for further replies.

feherke

Programmer
Aug 5, 2002
9,541
RO
Hi

I want to run a script from another and I would like to [tt]source[/tt] it, but there are small problems :
[ol]
[li]Find out if it was [tt]source[/tt]d or runned[/li]
[li]Get the [tt]source[/tt] file's name and path [/li]
[li]Exit from the [tt]source[/tt]d file only[/li]
[/ol]
As I read the [tt]bash[/tt] man page, all above seems impossible. Do anyone know any solution ?

I prefer [tt]bash[/tt], but I will be happy to read about other shells too. Thanks.

Feherke.
 
Bit of a 'long shot', and you will need to experiment depending on the shell and Operating System, but how about:

Using ps -f, find out the PID and PPID (parent PID) in the running script
Find out what sort of process the PPID is (a shell or another script)
Based on what the PPID is, get the COMMAND from ps -f
Decide whether to exit or not

I hope that helps, if only to spark off some ideas

Mike
 
1. If $0 is not the same as the name of the script (which you could hard-code), then you can assume it was sourced instead of run.

2. I can't think of a way to do this?

3. Easy once you've figured out the answer to question 1, right?

Annihilannic.
 
Hi

Thanks for the replys.

I will think again to see what can I get from the relations of the process with its parent.

The use of hardcoded reference points I will keep for a last solution.

Feherke.
 
what can I get from the relations of the process with its parent
You may find this faq822-4603 of interest (with $$)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Seems to not help. In fact, I do not think that external programs like [tt]ps[/tt] could tell anything usefull. The only one who could have enough information, is the shell, but it keeps them for itself. :-(

Feherke.
 
Another idea... you could do an lsof -p $$ and see whether it has the file referred to by $0 open. If it has it was run, if it hasn't it was sourced.

Annihilannic.
 
Hi

Wow ! This is a nice point. A really interesting difference. Sadly the name of $0 will be allways in the list, so the hammered-in name is also required. But I like the approach.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top