LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-04-2015, 11:21 AM   #1
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Post 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

Last edited by Lsatenstein; 02-04-2015 at 12:24 PM.
 
Old 02-05-2015, 03:53 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I do not really understand your problem, but probably readlink -m can help you
 
Old 02-05-2015, 07:05 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,692

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
$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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a shell script present on another server using perl script. anandg111 Linux - Server 4 07-18-2012 12:23 AM
[SOLVED] Help calling a shell script inside a python script WyoChuck Linux - Newbie 4 05-28-2012 09:39 PM
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 08:53 PM
how can a shell script determine its own location when being sourced? DeuceNegative Linux - Software 5 02-21-2008 01:57 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration