The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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-27-2007 03:23 AM
script sourcing problem (ksh) rakeshou UNIX for Dummies Questions & Answers 3 09-21-2007 09:29 AM
getting full path from relative path polypus Shell Programming and Scripting 4 03-25-2007 12:08 PM
Full path of executing script in ksh? BriceBu Shell Programming and Scripting 2 09-19-2005 09:29 AM
Full-Screen Script Morcegao30 Shell Programming and Scripting 1 03-01-2005 04:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2008
mrbluegreen mrbluegreen is offline
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!
  #2 (permalink)  
Old 03-19-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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 (permalink)  
Old 03-19-2008
mrbluegreen mrbluegreen is offline
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.
  #4 (permalink)  
Old 03-19-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
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 (permalink)  
Old 03-19-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0