RSS Forums RSS
Please support our Shell Scripting advertiser: Programming Forums
Views: 2603 | Replies: 3
Reply
Join Date: Mar 2006
Posts: 2
Reputation: ljegal is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ljegal ljegal is offline Offline
Newbie Poster

call scripts from a different directory

  #1  
Mar 13th, 2006
I have a shell script, /path/adir/script1.sh, that calls another script, /path/bdir/script2.sh. Now, I am in the directory /path/cdir/, and I call the first script like this:

../adir/script1.sh

The problem is that script2.sh cannot be found. In fact, even if script2.sh is in the same directory as script1.sh, the same problem occurs. The file script1.sh looks like this:

...
./script2.sh
...

How can I fix it? Thanks in advance.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2006
Posts: 2
Reputation: ljegal is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ljegal ljegal is offline Offline
Newbie Poster

Re: call scripts from a different directory

  #2  
Mar 13th, 2006
I realized I didn't make myself clear enough: I don't want to call script2.sh in the following way:

../bdir/script2.sh

because script1.sh could be called from any other directory and the above way won't work. However, it can be assumed that these two scripts are in their relative directories (though their parent directory may differ).
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 12
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: call scripts from a different directory

  #3  
Mar 18th, 2006
Just add a PATH variable into your script:

PATH=$PATH:/my_script1dir:/my_script2dir

That way, you don't have to worry about using things like ./ and ../ -- just call them by their name, and if they're in the search paths that you specified, then you'll be in good shape.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Apr 2006
Posts: 1
Reputation: datrinite is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
datrinite datrinite is offline Offline
Newbie Poster

Re: call scripts from a different directory

  #4  
Apr 5th, 2006
You can get the path of your script with $0 :
SCRIPT_DIR=`dirname $0` #relative path

or, to get absolute path :
INITIAL_DIR=`pwd` # Save current dir
cd `dirname $0` # Go to script dir
SCRIPT_DIR=`pwd` # Save script dir
cd ${INITIAL_DIR} # Go back to initial directory


So, put the few lines above in script1.sh
And call script2 with :
${SCRIPT_DIR}/script2.sh
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:53 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC