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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find the geographical location within a shell script eamani_sun Shell Programming and Scripting 1 05-13-2008 07:27 AM
script to check for a condition inside a file kiran1112 Shell Programming and Scripting 11 03-21-2007 05:38 AM
excutable script to copy a file to a different location. nazehcalil UNIX for Dummies Questions & Answers 4 12-21-2006 05:17 AM
running sed inside script file bajaj111 UNIX for Dummies Questions & Answers 4 11-08-2006 02:58 AM
Creating a file inside a script sendhilmani Shell Programming and Scripting 11 03-21-2006 04:24 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-14-2006
Registered User
 

Join Date: Mar 2006
Posts: 14
how to find Script file location inside script

I have to find out the file system location of the script file inside script. for example a script "abc.sh" placed anywhere in the file system when executed shold tell by itself the location of it.

example

#pwd
/
#./abc
this is /
#cd /root
#./abc
this is /root
#cd /
#/root/abc
this is /root

Last edited by asami; 03-14-2006 at 03:01 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-14-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
This should work.

Code:
[/tmp]$ cat pwd.ksh 
#! /bin/ksh

self="${0#./}"
base="${self%/*}"
current=$(pwd)

if [ "$base" = "$self" ] ; then
echo "$current"
else
echo "$current/$base"
fi ;
Reply With Quote
  #3 (permalink)  
Old 03-14-2006
Registered User
 

Join Date: Mar 2006
Posts: 14
no it doesnt work
Reply With Quote
  #4 (permalink)  
Old 03-14-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
Quote:
Originally Posted by asami
no it doesnt work
What doesnt work ?

Care to explain/show what is the output that you are getting ?

self="${0#./}"

The 0 you see is the digit 0 instead of the alphabet O
Reply With Quote
  #5 (permalink)  
Old 03-14-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
I see what you mean.

What you actually need is the contents of /proc/pid/exe. But in your case since you need the actual path of script that is run, /proc/pid/exe it wouldnt help because, the shell i.e. /bin/sh or /bin/ksh or /bin/bash et al., picks up your script and runs it. So a /proc/pid/exe inside a script would give you /bin/sh or /bin/ksh likewise.

What you can do is analyze $0 and see if it is a path starting from / and gets its basename. Else use it along with pwd of the current directory.
Reply With Quote
  #6 (permalink)  
Old 03-14-2006
Registered User
 

Join Date: Mar 2006
Posts: 14
you are using $0 which is what is used to execute that script, if i used

../../../abc.sh such things are appearing the result. i want direct path
Reply With Quote
  #7 (permalink)  
Old 03-14-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
Code:
[/tmp]$ cat pwd.ksh 
#! /bin/ksh

self="${0#./}"
base="${self%/*}"
current=$(pwd)

if [ "$base" = "$self" ] ; then
{ cd "$current" ; echo $(pwd) ;}
else
{cd $current/$base ; echo $(pwd) ; }
fi ;
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0