The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
full system backup script clement Linux 5 11-26-2007 11:23 PM
script sourcing problem (ksh) rakeshou UNIX for Dummies Questions & Answers 3 09-21-2007 06:29 AM
getting full path from relative path polypus Shell Programming and Scripting 4 03-25-2007 09:08 AM
Full path of executing script in ksh? BriceBu Shell Programming and Scripting 2 09-19-2005 06:29 AM
Full-Screen Script Morcegao30 Shell Programming and Scripting 1 03-01-2005 12:49 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-19-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
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!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-19-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,418
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
Reply With Quote
  #3 (permalink)  
Old 03-19-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
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.
Reply With Quote
  #4 (permalink)  
Old 03-19-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 146
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.
Reply With Quote
  #5 (permalink)  
Old 03-19-2008
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 449
Quote:
Originally Posted by mrbluegreen View Post
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.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0