Bourne: $0 issues when sourcing files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bourne: $0 issues when sourcing files
# 1  
Old 11-07-2012
Bourne: $0 issues when sourcing files

Problem in simple script form:

File 1:
Code:
# cat test.sh
#!/bin/sh
echo "Running test.sh - \$0 is  $0"
echo "Sourcing test2.sh"
. ./test2.sh
exit

File 2 (Sourced file):
Code:
# cat test2.sh
#!/bin/sh
echo "Running test2.sh - \$0 is $0"
exit

Output:
Code:
# ./test.sh 
Running test.sh - $0 is  ./test.sh
Sourcing test2.sh
Running test2.sh - $0 is ./test.sh

In case it matters:
Code:
uname -a
SunOS a70sspalport001 5.10 Generic_147440-25 sun4v sparc SUNW,SPARC-Enterprise-T5120

The problem is that when I'm sourcing another script, that script reports $0 as the name of the parent script. I've found many answers where $BASH_SOURCE is the answer, however bash is not a shell I can use (ksh is an option, however). If this simply isn't something I can do, that's fine, and once my stuff all hits production it won't actually be so important, but in final stages of QA it's becoming a hassle.

Thanks in advance.

Edit: I've had multiple edits, as I thought I had a working hack, but I was incorrect...

Last edited by Vryali; 11-07-2012 at 08:54 PM..
# 2  
Old 11-07-2012
If you have a modern Korn shell installed, ${.sh.file} should contain the name of the file that was sourced.

Have a try with something like this:
Code:
#!/usr/bin/env ksh
echo "Running test2.sh - ${.sh.file} sourced from $0"
exit

I think it goes without saying, but just to be sure, the parent script must be executed by Kshell, not Bourne shell.

The Kshell man page lists all of these useful variables:
man/man1/ksh.html man page

Last edited by agama; 11-07-2012 at 10:16 PM.. Reason: Added link
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bourne returning files based on creation date

I'm wanting to write a bourne shell script that takes in two command line arguments - a directory and a file. With this I want to return a list of files within the directory that are older (based on creation date) than the given file, and print the number of files that have not been listed (they... (4 Replies)
Discussion started by: britty4
4 Replies

2. UNIX for Dummies Questions & Answers

Question On Sourcing Variables

I have 2 scripts first script would call second script. test1.sh #!/bin/bash logfile=`basename $0`.log echo "First File" >> $logfile TIME=`ls -lu array.ksh | awk '{print $6" "$7" "$8}'` . /home/infrmtca/bin/Test/test2.sh #/home/infrmtca/bin/Test/test2.sh test2.sh #!/bin/bash... (1 Reply)
Discussion started by: Ariean
1 Replies

3. Shell Programming and Scripting

Sourcing variables from another script

My manager required that i keep the hostnames and username and password in a separate file when creating my sftp script. (Don't mention passwords and sftp...I've talk to him about this several times) I have a list of hostnames that have to be read in a loop in my main script. I don't know... (3 Replies)
Discussion started by: MJCreations
3 Replies

4. Shell Programming and Scripting

Sourcing as root automatically

Hey everyone! I have my .bash_profile file which is read automatically when I launch Terminal therefore I can run my own functions. BUT. When I do: sudo -s sudo su sudo su - No matter what I do, I can't get the .bash_profile file to be sourced automatically so I end up having to run... (2 Replies)
Discussion started by: dasx
2 Replies

5. Shell Programming and Scripting

List files in the current directory - BOURNE SHELL

i am trying to write a program, that will list .txt files and .png files. it will ask the user what type of files do they want to list! so if the user inputs txt files.. how would you list all the .txt files in the current directory (the directory the program is running)!! thanks (1 Reply)
Discussion started by: bshell_1214
1 Replies

6. Shell Programming and Scripting

sourcing .profile for other users

Hi Team, Thank you for your time. i have a situation where the user IDs of the applicatio users have been locked down to Read only. Hence I am writing a script to invoke their old .profile every time they login. My problem is : when i run . $userpath/.profile from within the ksh script... (9 Replies)
Discussion started by: anitha111
9 Replies

7. UNIX for Dummies Questions & Answers

Bourne shell viewing files in directory

Hi, I have a program that get a directory name from the user, then the program should go through one by one of the file, asking the user whether to move it to another folder. I tried to list the time of the file one by one. But it seems like it doesn't work. The code is as follow: check() {... (10 Replies)
Discussion started by: mInGzaiii
10 Replies

8. Shell Programming and Scripting

Help with Nested Sourcing files.

Hi, I have a script #!/bin/ksh #reading parameters. . FileA Echo ...... ...... File A has all parameters. FileA: Infile=xyz.com outfile=abc.com #Userid file . FileB FileB: (2 Replies)
Discussion started by: pinnacle
2 Replies

9. Shell Programming and Scripting

Permission issues in Files

There are two text files are there in UNIX directory. One file created by informatica that contains the detail records and other file is created by Shell script that contains header record(1 row)but these two files are created in different user. I want to merge these two files with another file... (4 Replies)
Discussion started by: praka
4 Replies

10. UNIX for Dummies Questions & Answers

sourcing the .bashrc

Hello, I am quite new to Linux... I need to set some aliases and I can't get it to work. Can somebody tell me what's wrong? I modified the .bashrc file in my home directory. I added: alias pmv= '/home/vera/MGLTools-1.4.5/share/bin/pmv' saved it and ran source .bashrc The shell... (3 Replies)
Discussion started by: Nusy
3 Replies
Login or Register to Ask a Question