How to:? Need affinity of a sourced shell script to the calling script
Here is a scenario.
/usr/local/bin/collect.sh contains two shell scripts, collect.sh and soxsetup.sh my home directory ~/bin contains testing versions of collect.sh and soxsetup.sh and point to different locations In both cases I want collect.sh to source the soxsetup.sh script that is in the same directory containing collect.sh Test So, regular use I want always use the two scripts from /usr/local/bin testing use I want always that the two scripts execute from ~/bin It does not always work. Different results for different working directories (pwd) I tested with some bash commands vis... which, dirname, realpath calling bash -x collect.sh from either home ~/bin or /usr/local/bin In some cases the wrong soxsetup.sh gets invoked. I want to avoid that problem. If you can help, please test with bash -x from ~ , ~/bin /usr/local/bin Hints are important. Herein is my test script Code:
#!/bin/bash test1 /usr/local/bin/test1 cd /usr/local/bin ; ./test1 cd ~ ; bin/test1 cd ~/bin ; ./test1 I do software work in Baan ERP systems |
I do not really understand your problem, but probably readlink -m can help you
|
$0 returns the name of the program as it was called.
I assume that /usr/local/bin is in your path environment but have you also added ~/bin? Unlike windows the current working directory path i.e . is not by default in the users path environment. If you execute test1 from any directory it should return /usr/local/bin since that is in the path environment. The same will be returned if you use the entire path i.e. /usr/local/bin/test1 to execute your script. ./test1 will always return . no matter what since that is how the program was called. ~ is a short cut for /home/username. Therefore ~/bin/test1 should always return /home/username/bin If your ~/bin is your path environment then it depends on how the path is searched as to which test1 actually executes which is why adding the current working directory is discouraged. |
All times are GMT -5. The time now is 09:55 PM. |