I'm dealing with a lot of scripts so, I try to put repetitive tasks in differents files and scripts that I'll include on demand.
NO bash, just POSIX shell, the interpreter will be /bin/sh, script will be run from anywhere, not only from his own folder, includes can be anywhere too, but can be relative.
.
-- folder1
| \ main.sh
-- folder2
| \ incl1.sh
-- folder3
\ incl2.sh
main.sh:
THIS_FILE=$(readlink -f "${0}");
BASEDIR=$(dirname "${THIS_FILE}");
. "${BASEDIR}/incl1/incl1.sh";
incl1.sh:
. pathThatIDontKnow/incl2.sh
incl2.sh, content is not relevant, just needs to be included in incl1.sh
The problem is in incl1, i cant include incl2 without knowing my current path, and is not possible to include incl2 on main.sh.
Is important to note that can be a new file main2.sh that tries to include same files in same way, this includes are not exclusive for main.sh and is not necesary to be in same folder as main.sh.
Any thoughts? this is technically possible ?
Thanks in advance!
Well, after thinking this a little, in my opinion, I can't solve this in the way I think in first place, so I attack the problem from other point of view: defining in every system a a scripts' root path variable as global.
So I export a global variable like this:
export SCRIPT_ROOT=path/to/root/scripts
It can be defined in different places such a profile.d.
Then in incl1 I'll put the import line like this:
. "$SCRIPT_ROOT/folder3/incl2.sh";
$SCRIPTS_ROOT can be used from everywhere.
I hope this helps someone else.