PDA

View Full Version : Bash: Including a path from one file into another


jf22901
Mar15-11, 05:06 AM
Hi everyone

I'm having trouble working out how to import a specified path into a bash script. Basically, I have numerous scripts, each of which writes their output to a certain directory via the command

set wdir = "/home/...."

However, in the future this directory may change, so rather than having to manually change the path of the directories in each file, I was wanting to create one file that specifies the path, and then have the other files include it. That way I only have to change one file if the path changes.

Initially I thought of having the path specified in a file such as 'path.sh', which would simply look like

FILEPATH="/home/..."

and then I thought that in the other files that require the path I could just put

#!/bin/bash
.
.
.
source path.sh
set wdir = $(FILEPATH)
.
.
.

However, such coding does not work, and I have no idea how to go about fixing it. Could anyone offer any suggestions as to the best way to include a path specified in another file? I'm new to all this bash scripting stuff! :tongue2:

Many thanks

schip666!
Mar15-11, 12:12 PM
try: export FILEPATH="/home/..."

I think the simple set command is only effective for the actual script runtime. I'd have to fiddle with it to prove it, especially when source-ing the file though.

Hurkyl
Mar15-11, 12:20 PM
Have you considered the option of making the path an argument?

How about using an environment variable?

How about a master script that knows the path, and you select which individual script you want to run as an argument to the master one?



If you are set on this approach, you can use "export" as schip mentioned. Alternatively, you could write a script that writes the filename to stdout and use something like

wdir = `path.sh`

jf22901
Mar15-11, 04:01 PM
Thanks for the replies everyone :cool:

I tried putting 'export' into the file, but it still wouldn't work. However, if I leave everything as it is but change $(FILEPATH) to $FILEPATH, it works. I'll stick with this for now, but will look into the other methods suggested.

Thanks once again

Rogerio
Mar24-11, 10:48 PM
Use
FILEPATH=$0
Thats all you need.