Skip to Main Content

Infrastructure Software

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Finding path to source executed shell script

Dude!Apr 5 2011 — edited Apr 13 2011
Hello,

I have shell script A which executes shell script B. The scripts A and B reside in the same directory.

Following requirements:

Scripts A and B need to be source executed.
The directory where A and B resides should be anywhere.
The present working directory from where A is executed should be anywhere.
The directory of scripts A and B may not be defined in $PATH.
The current shell could be any Bourne or Korn shell, Linux or Unix.

Problem:

Let's assume scripts A and B are installed in "/custom".
The users working directory is /home/oracle and he executes "source /custom/A".

If script A executes "source ./B", the result would be "source /home/oracle/B", which will fail.

Possible Solution:

Since script A is being sourced, $0 will always be the name of the current shell and not the name and path to script A.

I have not found a universal solution to locate the absolute path of a shell script that is being source executed.

So I was wondering about defining a variable in script A. e.g. SCRIPT_HOME=./custom. When script A executes "source $SCRIPT_HOME/B" it will work, but then script A needs to be customized by the user.

I could write a script C that edits script A to search and edit the SCRIPT_HOME variable. For this to work I will have to verify that script C is not source executed, and that script A is in the same directory as C based on $0. This is not a problem..

The question however is whether I need to create script C, or could simply execute script A with -config argument to edit itself and exit. I would prefer the later case and from what I understand, Unix and Linux do not lock files by default and this should be possible.

Any comments or suggestions please?

Thanks!

Edited by: Dude on Apr 5, 2011 3:59 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 11 2011
Added on Apr 5 2011
10 comments
1,749 views