Sponsored Content
Top Forums Shell Programming and Scripting Bourne: $0 issues when sourcing files Post 302728413 by Vryali on Wednesday 7th of November 2012 07:24:33 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Safe::Hole(3pm) 					User Contributed Perl Documentation					   Safe::Hole(3pm)

NAME
Safe::Hole - make a hole to the original main compartment in the Safe compartment SYNOPSIS
use Safe; use Safe::Hole; $cpt = new Safe; $hole = new Safe::Hole {}; sub test { Test->test; } $Testobj = new Test; # $cpt->share('&test'); # alternate as next line $hole->wrap(&test, $cpt, '&test'); # ${$cpt->varglob('Testobj')} = $Testobj; # alternate as next line $hole->wrap($Testobj, $cpt, '$Testobj'); $cpt->reval('test; $Testobj->test;'); print $@ if $@; package Test; sub new { bless {},shift(); } sub test { my $self = shift; $self->test2; } sub test2 { print "Test->test2 called "; } DESCRIPTION
We can call outside defined subroutines from the Safe compartment using share(), or can call methods through the object that is copied into the Safe compartment using varglob(). But that subroutines or methods are executed in the Safe compartment too, so they cannot call another subroutines that are dinamically qualified with the package name such as class methods nor can they compile code that uses opcodes that are forbidden within the compartment. Through Safe::Hole, we can execute outside defined subroutines in the original main compartment from the Safe compartment. Note that if a subroutine called through Safe::Hole::call does a Carp::croak() it will report the error as having occured within Safe::Hole. This can be avoided by including Safe::Hole::User in the @ISA for the package containing the subroutine. Methods new [NAMESPACE] Class method. Backward compatible constructor. NAMESPACE is the alternate root namespace that makes the compartment in which call() method execute the subroutine. Default of NAMESPACE means the current 'main'. This emulates the behaviour of Safe-Hole-0.08 and earlier. new \%arguments Class method. Constructor. The constructor is called with a hash reference providing the constructor arguments. The argument ROOT specifies the alternate root namespace for the object. If the ROOT argument is not specified then Safe::Hole object will attempt restore as much as it can of the environment in which it was constrtucted. This includes the opcode mask, %INC and @INC. If a root namespace is specified then it would not make sense to restore the %INC and @INC from main:: so this is not done. Also if a root namespace is given the opcode mask is not restored either. call $coderef [,@args] Object method. Call the subroutine refered by $coderef in the compartment that is specified with constructor new. @args are passed as the arguments to the called $coderef. Note that the arguments are not currently passed by reference although this may change in a future version. wrap $ref [,$cpt ,$name] Object method. If $ref is a code reference, this method returns the anonymous subroutine reference that calls $ref using call() method of Safe::Hole (see above). If $ref is a class object, this method makes a wrapper class of that object and returns a new object of the wrapper class. Through the wrapper class, all original class methods called using call() method of Safe::Hole. If $cpt as Safe object and $name as subroutine or scalar name specified, this method works like share() method of Safe. When $ref is a code reference $name must like '&subroutine'. When $ref is a object $name must like '$var'. Name $name may not be same as referent of $ref. For example: $hole->wrap(&foo, $cpt, '&bar'); $hole->wrap(sub{...}, $cpt, '&foo'); $hole->wrap($objfoo, $cpt, '$objbar'); root Object method. Return the namespace that is specified with constructor new(). If no namespace was then root() returns 'main'. Warning You MUST NOT share the Safe::Hole object with the Safe compartment. If you do it the Safe compartment is NOT safe. This module provides a means to go from a state where an opcode is denied back to a state where it is not. Reasonable care has been taken to ensure that programs cannot simply manipulate the internals to the Safe::Hole object to reduce the opmask in effect. However there may still be a way that the authors have not considered. In particular it relies on the fact that a Perl program cannot change stuff inside the magic on a Perl variable. If you install a module that allows a Perl program to fiddle inside the magic then this assuption breaks down. One would hope that any system that was running un-trusted code would not have such a module installed. AUTHORS
Sey Nakajima <nakajima@netstock.co.jp> (Initial version) Brian McCauley <nobull@cpan.org> (Maintenance) Todd Rinaldo <toddr@cpan.org> (Maintenance) SEE ALSO
Safe(3). perl v5.14.2 2011-11-15 Safe::Hole(3pm)
All times are GMT -4. The time now is 06:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy