0

I'm trying to use the path of the current script to determine the location of the source script ex:

export tenv=$(echo $0 | grep -Pio "(?<=/)\w+" | head -1)
. /${tenv}/common/scripts/globalProfile

If I hardcode tenv to the specific value I want, it works. Also if I call the same grep command to set tenv after sourcing the globalProfile, it works. But the above code doesn't work for some reason.

I'm running this script through a third party tool (AutoSys) which complicates the debugging process. The goal is to extract "stage" from the root of the script path "/stage/myproject/scripts/testScript"

If I do this:

export tenv=stage
. /${tenv}/common/scripts/globalProfile

The job runs successfully, and I can see the output in the logs.

If I do this:

export tenv=$(echo $0 | grep -Pio "(?<=/)\w+" | head -1)
echo $0
echo "tenv: ${tenv}"
. /${tenv}/common/scripts/globalProfile

The job fails and no logs are produced.

If I do this:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
export tenv=$(echo $DIR | grep -Pio "(?<=/)\w+" | head -1)
export alt=$(echo $0 | grep -Pio "(?<=/)\w+" | head -1)
echo $0
echo "DIR: ${DIR}"
echo "tenv: ${tenv}"
echo "alt: ${alt}"
. /${tenv}/common/scripts/globalProfile

The job fails, but it produces a log file containing the following:

/stage/myproject/scripts/testScript
DIR: /mnt/auto/home/myproject
tenv: mnt
alt: stage

So in this case, tenv got set to the wrong value and thus the overall job was a failue, but since we now have a log file, it shows that our original code (now stored in the alt variable) SHOULD have produced the right value.

For one final bit of insanity, if I do this:

export tenv=mnt
export alt=$(echo $0 | grep -Pio "(?<=/)\w+" | head -1)
echo $0
echo "tenv: ${tenv}"
echo "alt: ${alt}"
. /${tenv}/common/scripts/globalProfile

The job fails, and once again does not produce any logs, even though I explicitly set the tenv variable to the same value it used in the previous test.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I can, unfortunately in the context(autosys) I'm running this, the config of the standard output logs is setup in the source script. So anything echoed prior to that is lost. – yoyobroccoli Oct 27 '20 at 19:05
  • Try https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself/246128#246128 – Philippe Oct 27 '20 at 19:13
  • This is bizarre. Using that I can actually read the log. But the directory is wrong. However $0 does give me the correct directory which is "/stage/myproject/scripts/testScript", whereas echo DIR give me "/mnt/auto/home/myproject" what I need is "stage". The above code gives me "mnt". But as soon as I change $DIR back to $0, not only does it not work, but I can't read logs anymore – yoyobroccoli Oct 27 '20 at 19:42
  • @Philippe Please see my update – yoyobroccoli Oct 27 '20 at 20:40
  • The code looks fine; perhaps there is something in your environment or filesystem. Try breaking it down and simplifying. Try making an independent script that prints to stdout instead of a log, and put the script in different directories and invoke it in different ways. It's looking like /stage is a soft link to /mnt/auto/home. This shouldn't make a difference, it doesn't on my system, but maybe it does on yours. – Vercingatorix Oct 27 '20 at 20:43
  • Looks like your third run worked for the variable `alt`. Why not substitute `alt` for `tenv` in the final invocation of `globalProfile` and see if it works? – Vercingatorix Oct 27 '20 at 20:47
  • @eewanco That was the second run. `alt` in the third run is just a rename of what `tenv` was in the second run. So that doesn't work (even, though all reason seems to say it should). Also, as mentioned this script is not being run from a command line, its being run by a third party tool (AutoSys), so I don't have access to a console to view stdout. However the tools logs should be capturing stdout. – yoyobroccoli Oct 27 '20 at 20:50
  • @yoyobroccoli Do you have access to an interactive shell, or only this logged batch job-thingy? – Vercingatorix Oct 27 '20 at 20:57
  • @eewanco Not as far as I know unfortunately.. – yoyobroccoli Oct 27 '20 at 20:59

3 Answers3

0

I say try this:

. /$(echo $0 | grep -Pio "(?<=/)\w+" | head -1)/common/scripts/globalProfile

See how that goes. If it doesn't, perhaps you can further debug by temporarily changing . /${tenv}/common/scripts/globalProfile to . /stage/common/scripts/globalProfile until you get it to work.

Vercingatorix
  • 1,838
  • 1
  • 13
  • 22
  • Tried that. Didn't work. And the original script had the `tenv` value hardcoded. The whole goal of what I'm working on is to adapt theses scripts to get the environment name from the script path, so we don't need to duplicate scripts that are identical except for one hardcoded value. – yoyobroccoli Oct 27 '20 at 21:14
  • I see -- I didn't realize that `tenv` was essential for something other than determining the location of the script; it's important to AutoSys and to the job as well. – Vercingatorix Oct 28 '20 at 09:22
0

The behaviour you saw seems to be related to symbolic links in some paths.

Try this :

DIR="$( ""cd -L "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
export tenv=$(echo $DIR | grep -Pio "(?<=/)\w+" | head -1)
export alt=$(echo $0 | grep -Pio "(?<=/)\w+" | head -1)
echo $0 "${BASH_SOURCE[0]}"
echo "DIR: ${DIR}"
echo "tenv: ${tenv}"
echo "alt: ${alt}"
. /${tenv}/common/scripts/globalProfile

Only the first line is different from the extract in your post.

Philippe
  • 20,025
  • 2
  • 23
  • 32
  • The above code result in the job fails and produces the following output: `/stage/myproject/scripts/script DIR: /mnt/auto/home/myproject tenv: mnt alt: stage` And as soon as I replace the last line of the above code with . `/${alt}/common/scripts/globalProfile` the job fails and no standard output was produced. I just can't make sense of it why `alt` is set to the correct value but once it was plugged in to the source file path, it fails. – yoyobroccoli Oct 28 '20 at 04:22
0

OMG I finally get it working by using a different way of getting the path

THIS=`readlink -f "${BASH_SOURCE[0]}" 2>/dev/null||echo $0`
DIR=`dirname "${THIS}"`
export tenv=$(echo $DIR | grep -Pio "(?<=/)\w+" | head -1)
. /${tenv}/common/scripts/globalProfile

But I still don't get why readlink works while the other ones I have tried in the original post doesn't...