LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2009, 08:46 AM   #16
Doubi
LQ Newbie
 
Registered: Mar 2009
Location: London, UK
Distribution: Ubuntu 10.04
Posts: 6

Original Poster
Rep: Reputation: 0

Here I am!

I'm afraid that yes, most of the people who asked if they were missing something actually are <:-)

Below is the output of most of the examples given above, first when executing the script in its own process, second when sourcing the script:
Code:
doubi@doubi-newbi ~
$ wxWebKit3/setup-env
Output of "$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")" :
    /home/doubi/wxWebKit3/setup-env
$0: wxWebKit3/setup-env
$PWD: /home/doubi
which $0:  which wxWebKit3/setup-env
$(readlink -f $0): /home/doubi/wxWebKit3/setup-env
scriptDir=$(cd $(dirname $0);pwd); echo $scriptDir : scriptDir=/home/doubi/wxWebKit3
$BASH_SOURCE:  wxWebKit3/setup-env

doubi@doubi-newbi ~
$ . wxWebKit3/setup-env
Output of "$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")" :
    /home/doubi/bash
$0: bash
$PWD: /home/doubi
which $0:  which bash
$(readlink -f $0): /home/doubi/bash
scriptDir=$(cd $(dirname $0);pwd); echo $scriptDir : scriptDir=/home/doubi
$BASH_SOURCE:  wxWebKit3/setup-env
It seems osor's initial reaction was the most helpful. I personally don't mind relying on bash, so I guess this is my solution!

Elsewhere I received an interesting suggestion for this:
Code:
xxx(){ #dummy function should not be a reasonable name
:
}

shopt -s  extdebug #include filename in	output
foo=`typeset -fF xxx` #	get data 3 arg is name
echo hi $foo   #show it
unset xxx  #remove dummy function
shopt -u extdebug  #unset extdebug
The results are like so:
Code:
doubi@doubi-newbi ~
$ wxWebKit3/setup-env
hi xxx 21 wxWebKit3/setup-env

doubi@doubi-newbi ~
$ . wxWebKit3/setup-env
hi xxx 21 wxWebKit3/setup-env
Very tricksy, and apparently effective! Can someone more knowledgeable than myself confirm whether this solution is shell-agnostic? Extracting the 3rd arg from the typeset output should be relatively trivial. A shell-agnostic solution would be groovy in that I could possibly get it included with the package source.
 
Old 06-30-2009, 09:01 AM   #17
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Indeed, most of us overlook from your initial posting that you were sourcing the scripts instead of directly executing them.

The latter solution you posted should work but is bash specific due to "extdebug" and "typeset -F" use.
 
Old 06-30-2009, 10:22 AM   #18
GazL
Senior Member
 
Registered: May 2008
Posts: 4,083
Blog Entries: 1

Rep: Reputation: 1529Reputation: 1529Reputation: 1529Reputation: 1529Reputation: 1529Reputation: 1529Reputation: 1529Reputation: 1529Reputation: 1529Reputation: 1529Reputation: 1529
I have some scripts I run that need to reference some files in a subdirectory ('src') of the directory containing the script itself.

As I want it to work regardless of the current working directory or location the script is installed in itself, the way I do it is as follows...

Code:
SRC=$(readlink -f $(dirname $0)/src)
Then I use ${SRC}/filename whenever I need to reference anything.

I've not tried it with sourcing, but I suspect it would break too as the $0 will still be that of the original script.

edit: actually $0 appears to get set to '-bash' so yes, it doesn't work with sourced files either. Oh well.

Last edited by GazL; 06-30-2009 at 10:42 AM.
 
Old 06-30-2009, 03:19 PM   #19
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
Quote:
Originally Posted by Doubi View Post
It seems osor's initial reaction was the most helpful. I personally don't mind relying on bash, so I guess this is my solution!
Of course BASH_SOURCE is just part of the solution. You have to take into account that this is an array variable, so keep that in mind for multiple sources. Also, you might need to combine this with PWD to find the full dirname of a relative path.
 
Old 07-01-2009, 02:53 AM   #20
vap16oct1984
Member
 
Registered: Jun 2009
Location: INDIA
Distribution: RHEL-5
Posts: 174
Blog Entries: 3

Rep: Reputation: 38
Question

Quote:
Originally Posted by jlliagre View Post
Here is something that should work with bash and ksh.
Code:
...
scriptDir=$(cd $(dirname $0);pwd)
echo $scriptDir
...
Hi,
Can u explain what is the meaning of dirname ?
if possible give us some example so that we can understant what
it does exaclty?
 
Old 07-01-2009, 03:00 AM   #21
vap16oct1984
Member
 
Registered: Jun 2009
Location: INDIA
Distribution: RHEL-5
Posts: 174
Blog Entries: 3

Rep: Reputation: 38
look what dirname exacly does
[root@office-pc ~]# pwd
/root

[root@office-pc ~]# dirname /etc/sysconfig/network-scripts/
/etc/sysconfig

thanks
 
Old 07-01-2009, 04:52 AM   #22
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
"man dirname" and more generally "man <some-command>" is the quickest way to figure out what a command is doing.
 
Old 07-01-2009, 06:50 PM   #23
Doubi
LQ Newbie
 
Registered: Mar 2009
Location: London, UK
Distribution: Ubuntu 10.04
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlliagre View Post
The latter solution you posted should work but is bash specific due to "extdebug" and "typeset -F" use.
Thanks for clarifying that jlliagre
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Location of Gnome auto-mount script? aembleton Linux - Newbie 4 05-27-2009 07:00 PM
Getting location of the script which is currently being executed jiju.jacob Linux - Newbie 4 07-21-2008 06:48 AM
Start up script location aking469 Linux - Laptop and Netbook 1 05-22-2006 01:31 PM
Location for script data storage Skazi Linux - General 1 10-15-2005 10:55 AM
Display Manager Script Location cabinetcrafter Mandriva 1 04-06-2005 09:41 PM


All times are GMT -5. The time now is 01:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration