KSH - Sourced file location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH - Sourced file location
# 1  
Old 07-01-2009
PHP 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  
Old 07-01-2009
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 11:58 AM..
# 3  
Old 07-01-2009
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  
Old 07-01-2009
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 04:07 PM..
# 5  
Old 07-01-2009
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  
Old 07-01-2009
Quote:
Originally Posted by Al262
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 06:14 PM..
# 7  
Old 07-01-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use find with cp and sed in ksh to copy files to a slightly different location

Hello there wonderful people, I am running on Solaris 10 and with the following ksh version: strings /bin/ksh | grep Version | tail -2 @(#)Version M-11/16/88i Suppose I want to copy files that end in _v2 from underneath /dir1/dir2/save directory to /dir1/dir2. Basically, what I’m... (12 Replies)
Discussion started by: ejianu
12 Replies

2. Shell Programming and Scripting

ksh using input file with output going to same file name and location

I've been asked if I can write a "quick" little ksh script that will do the following: java java_class_file /dir/input_file.xml /dir/output_file.xml I'm a complete newbie with ksh so any help would be appreciated. This is on AIX and java is found in /usr/java5/jre/bin/java (4 Replies)
Discussion started by: newbie_ksh
4 Replies

3. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

4. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

5. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

6. Shell Programming and Scripting

Error Installing Env::Sourced

Hello, I am trying to install Env::Sourced and i get the following error. sh: source: not found sh: include.sh: not found Unable to determine your shells source program, typically 'source' or '.' How can i overcome this? Best Regards, Christos (3 Replies)
Discussion started by: chriss_58
3 Replies

7. Shell Programming and Scripting

Quitting from a script, either sourced or not

This is a very simple problem, I am wondering why I can find no answer anywhere... I have a script that can be run either sourced or not. This script has some place where it needs to quit execution (e.g., when an error is found) If I "exit", the sourced call would exit the parent shell, but... (7 Replies)
Discussion started by: MadMage
7 Replies

8. Shell Programming and Scripting

Put one string from one location to another location in a file

Hi Everyone, I have 1.txt here a b c' funny"yes"; d e The finally output is: here a b c d e' funny"yes"; (1 Reply)
Discussion started by: jimmy_y
1 Replies

9. Shell Programming and Scripting

Exit from sourced script

Hello, I have written a script (say chld.sh). its structure is as follows: ------------------------ #!/bin/sh usage() { echo "chld.sh <param1> <param2>" exit 0 } chk_param_1() { case $param1 in c) export PATH=$PATH:/home_a/bin/execs;; d) export... (2 Replies)
Discussion started by: angad.makkar
2 Replies

10. OS X (Apple)

which file is sourced by bash on login (Mac OS X 10.5.3)?

Hi: So my current Python (2.52) rig is not working as intended. "echo $PATH" in bash gives me 'X'" that's not what i want, so i need to change my path. To do that, there appeared to be four choices (all in my ~/ directory--note: I'm root, it's my Mac, but i'm in a user account): .profile... (2 Replies)
Discussion started by: Alex_Land
2 Replies
Login or Register to Ask a Question