LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2006, 02:36 PM   #1
rednuht
Member
 
Registered: Aug 2005
Posts: 239
Blog Entries: 1

Rep: Reputation: 31
bash, where am I script


if I have a bash script that references a directory that exists within the folder the script resides in then I call the script from somewhere else how can I know where I am to reference said directory ?

I want the the script to do something like
Code:
installedDir=`pwd`
dataDir=$installedDir"/data"
-do something with $dataDir
If I run it from . the script works fine, but if I change to a different directory and then sh /PATH/TO/SCRIPT/script.sh then $installDir is set to where I am when I ran the script not where the script is and so can not find the data folder.

I have the annoying feeling that this is just not possible and I will have to hardcode $installDir and force people to install the script in a known location.

I tried adding
Code:
cd .
but that uses the initial starting folder not the location of the script
I also examined the results of
Code:
set
executed inside the script, nothing even close to useful.
 
Old 08-14-2006, 03:36 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Since not everyone has a "realpath" Bash loadable module or binary (Busybox for instance has) and readlink (which uses realpath syscall) doesn't work on proc you could try:
Code:
a=($(stat -c %N /proc/$$/fd/255)); s=${a[2]}
n=$[${#s}-2]; echo "I'm here: ${s:1:$n}"
If there's a more elegant solution I would definately be interested.

Last edited by unSpawn; 08-14-2006 at 03:41 PM.
 
Old 08-14-2006, 04:21 PM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Couldn't you just temporarily change directory?
Code:
WD=`pwd`
cd ${0%/*}
installedDir=`pwd`
cd $WD
 
Old 08-14-2006, 04:36 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
No external binaries. Definately way better.
 
Old 08-15-2006, 12:23 PM   #5
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Why do you need to change directory? Wouldn't

Code:
installedDir=${0%/*}
suffice?
 
Old 08-15-2006, 01:33 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
The idea of changing the directory was to guarantee getting the full path incase the script changed directory half way through.
 
Old 08-17-2006, 09:17 AM   #7
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Ah, because ${0%/*} is not necessarily an absolute path. Gotcha.
 
Old 08-17-2006, 09:58 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I'm using that:
Code:
installedDir=$(cd $(dirname $0);pwd)
 
Old 08-23-2006, 03:17 PM   #9
rednuht
Member
 
Registered: Aug 2005
Posts: 239

Original Poster
Blog Entries: 1

Rep: Reputation: 31
I added a condition to check if the script was being run from the same folder otherwise the result ended up being the script name plus "data"
Code:
installDir=${0%/*}
if [ $installDir = $0 ]; then
  installDir="."
fi

Last edited by rednuht; 08-23-2006 at 04:19 PM.
 
  


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
building a bash script from an install script paranoid times Programming 6 07-29-2006 03:24 AM
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 05:05 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:51 PM.

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