Maintain full path of a script in a var when sourcing it from a different script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Maintain full path of a script in a var when sourcing it from a different script
# 1  
Old 03-19-2008
Maintain full path of a script in a var when sourcing it from a different script

Hi All,

I've searched through the forum for a solution to this problem, but I haven't found anything. I have 2 script files that are in different directories.

My first script, let's call it "/one/two/a.sh" looks like this:

Code:
#!/bin/sh
IN_DIR=`dirname $0`

CUR_DIR=`pwd`
cd $IN_DIR
A_DIR=`pwd`
cd $CUR_DIR

export $A_DIR
echo $A_DIR

Running that, gives me "/one/two/", and A_DIR is set to that value.


Now I have a second script in a different directory that sources this script. Let's call it "/hello/bye/b.sh"

Code:
#!/bin/sh
. /one/two/a.sh

echo $A_DIR

In this case, I DO NOT get "/one/two/", but instead: "/hello/bye/"

I guess this is because the $0 variable inside the first script becomes "b.sh" instead. I want the first script to always source the $A_DIR variable with the path of the script, and I do not want to rely on hard-coding it, nor using the "find" command.

Does anyone have any ideas? Any help would be great!

Thanks in advance!
# 2  
Old 03-19-2008
pwd gives the current directory you are running the script in. It can be anything.

inside a.sh:
Code:
CUR_DIR=`dirname $0`

This ONLY works when you invoke a.sh with a full pathname, e.g. . /one/two/a.sh
# 3  
Old 03-19-2008
Hi Jim, thanks for the reply.

I make some change directory calls inside a.sh, so that the pwd becomes dirname ${0}. The problem I think is that, I'm not invoking a.sh, but sourcing it inside b.sh:

Code:
#!/bin/sh
. /one/two/a.sh

echo $A_DIR

Let's say I invoke b.sh with the following:

/hello/bye/b.sh

Because a.sh makes the following call: dirname ${0}. It gets the path of the current value inside $0, which is actually "/hello/bye/b.sh", since that script is the one being actually invoked.
# 4  
Old 03-19-2008
On the same lines of your last post, just a few recommendations...

Quote:
The problem I think is that, I'm not invoking a.sh, but sourcing it inside b.sh
Yes, that's the problem, you need to invoke a.sh script. Sourcing it, will cause it to get executed in the place of the current shell ( b.sh shell/environment), so every command of a.sh will get place in the b.sh shell /environment.
Secondly, in your situation you don't need export in a.sh. That will export the value of the variable A_DIR in the subshells of a.sh ( ex. if you invoke other scripts within a.sh, etc ... ). So to access the value of A_DIR in the outer script b.sh for further processing, one way of doing it, is through invoking a.sh and put the value of A_DIR in a temporary file inside a.sh :

a.sh script :

Code:
#!/bin/sh
IN_DIR=`dirname $0`

CUR_DIR=`pwd`
cd $IN_DIR
A_DIR=`pwd`
cd $CUR_DIR

echo $A_DIR > /hello/by/temp_file

and b.sh script :

Code:
#!/bin/sh

/one/two/a.sh
my_dir=`cat /hello/by/temp_file`

#To see the result
echo " This is A_DIR " $my_dir

# If you don't need the temp_file 
rm /hello/by/temp_file

Hope this helps.
# 5  
Old 03-19-2008
Quote:
Originally Posted by mrbluegreen
Hi All,

I've searched through the forum for a solution to this problem, but I haven't found anything. I have 2 script files that are in different directories.

There is rarely, if ever, a need to find the path to a script.

Put your scripts in a directory in your PATH and don't worry about where they are.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Resume parent shell after sourcing another script

#! /bin/ksh #first.sh echo "b4 set exit as return" alias exit=return echo "call second" . ./second.sh echo "after second" #. ./third.sh unalias exit echo "ho lanciato il terzo" =================// #second.sh echo "in scond" exit ==============// the above code works in k... (2 Replies)
Discussion started by: mprakasheee
2 Replies

2. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Sourcing variables from another script

My manager required that i keep the hostnames and username and password in a separate file when creating my sftp script. (Don't mention passwords and sftp...I've talk to him about this several times) I have a list of hostnames that have to be read in a loop in my main script. I don't know... (3 Replies)
Discussion started by: MJCreations
3 Replies

4. Red Hat

Crontab sourcing PATH?

Hi, Im trying to run script A which requires path /sbin. I have a crontab entry to run script A every 10 minutes. Script A is executed fine by cron, but because script A requires /sbin in its path it fails to run. My situation is script A get overwritten from time to time so I can't modify... (4 Replies)
Discussion started by: wilsonee
4 Replies

5. UNIX for Dummies Questions & Answers

Script that will Delete and Maintain Files

Hi guys, Good day to all and Happy New Year!!! Cheers!!! I hope anyone could help/suggest me on hot to create a script that will monitor the directory where files are being dumped. I have a script in CRON that runs everyday and collect for logs with a filename of Output_$(date... (3 Replies)
Discussion started by: rymnd_12345
3 Replies

6. Shell Programming and Scripting

Script to maintain file versions

I am developing a script to maintain 'n' number of versions of a file. The script will take a filename as a parameter and the number of versions to maintain. This basically does something like a FIFO. Here is what I developed. But something is not right. I have attached the script. Can u pls help... (2 Replies)
Discussion started by: vskr72
2 Replies

7. Shell Programming and Scripting

How to extract strings from full path when full path is not fixed

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

8. UNIX for Dummies Questions & Answers

maintain database script...

Hi there. i'm new user at here I need help for this. I need to write a script that maintains a database of cryptographic checksums. In other words, I need this script to check the files checksum whether the files has been modified or not. But i got no idea where to start. Hope anyone here can... (8 Replies)
Discussion started by: hihihehe
8 Replies

9. UNIX for Dummies Questions & Answers

script sourcing problem (ksh)

I have a script "abc.sh" in /tmp which has exit 0 as its last line when I run this script from /tmp/xyz/def.sh script as . ../abc.sh then the script executes but the control doesn't return to def.sh script for subsequent commands in def.sh but if I invoke the abc.sh from inside the... (3 Replies)
Discussion started by: rakeshou
3 Replies

10. Shell Programming and Scripting

Full path of executing script in ksh?

Hello all, Here's the scenario: I've got a script, let's call it script1. This script invokes another script, which we'll call set_env, via the dot "." command, like so: File: #!/bin/ksh # region_id=DEV . set_env ${region_id} and so on. Script set_env sets up an... (2 Replies)
Discussion started by: BriceBu
2 Replies
Login or Register to Ask a Question