The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash copy file contents into an existing file at a specific location gshepherd7 Shell Programming and Scripting 6 03-11-2009 09:26 AM
Exit from sourced script angad.makkar Shell Programming and Scripting 2 03-04-2009 06:35 AM
copy files from one location to similar location pharos467 UNIX for Advanced & Expert Users 1 09-21-2008 12:21 PM
which file is sourced by bash on login (Mac OS X 10.5.3)? Alex_Land OS X (Apple) 2 07-11-2008 10:52 PM
What is the file location? Chanakya.m Shell Programming and Scripting 5 07-17-2006 12:42 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-01-2009
Al262 Al262 is offline
Registered User
  
 

Join Date: Jul 2009
Location: Pittsburgh, PA
Posts: 6
Thumbs down KSH - Sourced file location

The sample scripts below, I am looking for the location of the sourced b.sh file. The b.sh file is source by multiple files, so it is not feasible to rely on a "global" variable or passed argument. Is there any way to determine the location of b.sh within b.sh?

a.sh
Code:
 
#!/bin/ksh
echo "a: <$0>"
ScriptDir=`dirname $0`
. ${ScriptDir}/b.sh

b.sh
Code:
 
#!/bin/ksh
WhereAmI="????"
echo "b: <$0> ${WhereAmI}"

  #2 (permalink)  
Old 07-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
b.sh
Code:
#!/bin/ksh

thisFILE="$(whence ${0})"
thisDIR="$(dirname ${thisFILE})"
PROGNAME="${0##*/}"

WhereAmI="${thisDIR}"
echo "b: <${PROGNAME}> ${WhereAmI}"
Note: this works if a.sh and b.sh reside in the same direct. If b.sh is somewhere else, it returns a.sh location (and rightfully so). Need to think about that.

Last edited by vgersh99; 07-01-2009 at 10:58 AM..
Bits Awarded / Charged to vgersh99 for this Post
Date User Comment Amount
07-01-2009 Anonymous N/A 1
  #3 (permalink)  
Old 07-01-2009
Al262 Al262 is offline
Registered User
  
 

Join Date: Jul 2009
Location: Pittsburgh, PA
Posts: 6
RE: KSH - Sourced file location

Thanks for the response, but as you indicate at the end of your post, it only works when a and b are in the same directory. My useCase has to address the notion that a is in a different directory and not necessarily always a.

The useCase is that /app1/a.sh sources /common/b.sh
/app2/c.sh sources /common/b.sh

I need b.sh to know that he is in the common directory.
  #4 (permalink)  
Old 07-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
this lnk might be helpful....
Bit I could not make it work with bash/ksh on Solaris....
Maybe others will have better ideas.

Last edited by vgersh99; 07-01-2009 at 03:07 PM..
  #5 (permalink)  
Old 07-01-2009
Al262 Al262 is offline
Registered User
  
 

Join Date: Jul 2009
Location: Pittsburgh, PA
Posts: 6
RE: KSH - Sourced file location

Not really much help. The link tried to provide workarounds rather than answering the question. There are times when inside the script, it doesn't know what directory it is in. Seems like a $0 equivalent should be available for a sourced file.
  #6 (permalink)  
Old 07-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
Quote:
Originally Posted by Al262 View Post
Not really much help. The link tried to provide workarounds rather than answering the question. There are times when inside the script, it doesn't know what directory it is in. Seems like a $0 equivalent should be available for a sourced file.
You'd think , huh?
But it ($0 in the sourced shell) is not what what it's in the 'called' (not sourced) script - as the 'sourced' script runs in the same process space (there's no newly forked process) as the 'sourcing' script. Therefore, the '$0' in the sourced script is the '$0' of the sourcing script.

I'm not sure if I'm explaining this correctly - you can take a look at this thread or browse this comp.unix.shell user group for similar threads.

Last edited by vgersh99; 07-01-2009 at 05:14 PM..
  #7 (permalink)  
Old 07-01-2009
Al262 Al262 is offline
Registered User
  
 

Join Date: Jul 2009
Location: Pittsburgh, PA
Posts: 6
RE: KSH - Sourced file location

Understand it completely.

Again, I was looking (hoping) for a $0 equivalent for the sourced file. I realize the $0 is the invoked script.

Thanks for the reponses, hopefully someone will provide some insight.
Sponsored Links
Reply

Bookmarks

Tags
ksh file directory

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0