LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to:? Need affinity of a sourced shell script to the calling script (https://www.linuxquestions.org/questions/linux-software-2/how-to-need-affinity-of-a-sourced-shell-script-to-the-calling-script-4175533059/)

Lsatenstein 02-04-2015 11:21 AM

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
#
SOXBIN=`dirname $0`               
echo "SOXBIN=$SOXBIN"
#
if [ $SOXBIN == '.' ]; then
echo "executing from `pwd`"
SOXBIN=`pwd`
fi
echo "$SOXBIN/soxsetup.sh $1 $2"
. $SOXBIN/soxsetup.sh $1 $2

I tried the above with just
test1
/usr/local/bin/test1
cd /usr/local/bin ; ./test1

cd ~ ; bin/test1
cd ~/bin ; ./test1

I do software work in Baan ERP systems

pan64 02-05-2015 03:53 AM

I do not really understand your problem, but probably readlink -m can help you

michaelk 02-05-2015 07:05 AM

$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.