The UNIX and Linux Forums  

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 01: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 11:52 PM
What is the file location? Chanakya.m Shell Programming and Scripting 5 07-17-2006 01:42 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-01-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Al262 View Post
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%/*}
  #2 (permalink)  
Old 07-02-2009
Al262 Al262 is offline
Registered User
  
 

Join Date: Jul 2009
Location: Pittsburgh, PA
Posts: 6
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.
  #3 (permalink)  
Old 07-02-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,930
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
$
  #4 (permalink)  
Old 07-02-2009
Al262 Al262 is offline
Registered User
  
 

Join Date: Jul 2009
Location: Pittsburgh, PA
Posts: 6
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.
  #5 (permalink)  
Old 07-02-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
looks like the '.sh.file' is not implemented on AIX's version of ksh93 (among others).
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 09:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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