KSH - Sourced file location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH - Sourced file location
# 8  
Old 07-01-2009
Quote:
Originally Posted by Al262
The sample scripts below, I am looking for the location of the sourced b.sh file.

Why? There is rarely a good reason for doing that.
Quote:
The b.sh file is source by multiple files, so it is not feasible to rely on a "global" variable or passed argument.

Why do you need to rely on anything? Put the script in a directory that is in your $PATH.
Quote:
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`


There's no need for an external command:

Code:
ScriptDir=${0%/*}

# 9  
Old 07-02-2009
The script is untouched as it migrates throught the life cycle regions with various file sub-systems, such as /common/dev, /common/test, and so on. Specific applications /app1 or /app2 will be invoked from an external scheduler using a parameter. So with in the /app1 script, they run $1/setCommonEnv.sh where $1 is /common/dev or /common/test ...

Within setCommonEnv.sh, i would like to derive which environment I am in, as well as which script or subscript.

Seems very reasonable to be able to walk a call stack.
# 10  
Old 07-02-2009
If you are using ksh93 it can be easily done using the .sh.file builtin parameter
Code:
$ cat a.sh
#!/bin/ksh93

echo "a: $PWD"

. ./ksh93/b.sh
$

Code:
$cat ksh93/b.sh
#!/bin/ksh93

echo "b: $PWD"
echo "b: ${.sh.file}"
echo "b: ${.sh.file##*/}"
echo "b: ${.sh.file%/*}"
$

Code:
$ ./a.sh
a: /home/fpm
b: /home/fpm
b: /home/fpm/ksh93/b.sh
b: b.sh
b: /home/fpm/ksh93
$

# 11  
Old 07-02-2009
Exactly what I am looking for. However, the scripts are not running ksh93. I did run through a test cycle here on AIX 5.3, the results are below.
Code:
 
/test> cat a.sh
#!/bin/ksh93
echo "a: $0"
echo "a: $PWD"
ScriptDir=`dirname $0`
. ./sub/b.sh
/test> cat sub/b.sh
#!/bin/ksh93
echo "b: $PWD"
echo "b: ${.sh.file}"
echo "b: ${.sh.file##*/}"
echo "b: ${.sh.file%/*}"
/test> ./a.sh
a: ./a.sh
a: /test
b: /test
b:
b:
b:
/test>

Thanks for the time and response.
# 12  
Old 07-02-2009
looks like the '.sh.file' is not implemented on AIX's version of ksh93 (among others).
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