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.