The sample scripts below, I am looking for the location of the sourced b.sh file. The b.sh file is source by multiple files, so it is not feasible to rely on a "global" variable or passed argument. Is there any way to determine the location of b.sh within b.sh?
Note: this works if a.sh and b.sh reside in the same direct. If b.sh is somewhere else, it returns a.sh location (and rightfully so). Need to think about that.
Thanks for the response, but as you indicate at the end of your post, it only works when a and b are in the same directory. My useCase has to address the notion that a is in a different directory and not necessarily always a.
The useCase is that /app1/a.sh sources /common/b.sh
/app2/c.sh sources /common/b.sh
I need b.sh to know that he is in the common directory.
Not really much help. The link tried to provide workarounds rather than answering the question. There are times when inside the script, it doesn't know what directory it is in. Seems like a $0 equivalent should be available for a sourced file.
You'd think , huh?
But it ($0 in the sourced shell) is not what what it's in the 'called' (not sourced) script - as the 'sourced' script runs in the same process space (there's no newly forked process) as the 'sourcing' script. Therefore, the '$0' in the sourced script is the '$0' of the sourcing script.
I'm not sure if I'm explaining this correctly - you can take a look at this thread or browse this comp.unix.shell user group for similar threads.
The script is untouched as it migrates throught the life cycle regions with various file sub-systems, such as /common/dev, /common/test, and so on. Specific applications /app1 or /app2 will be invoked from an external scheduler using a parameter. So with in the /app1 script, they run $1/setCommonEnv.sh where $1 is /common/dev or /common/test ...
Within setCommonEnv.sh, i would like to derive which environment I am in, as well as which script or subscript.
Seems very reasonable to be able to walk a call stack.