The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Find the geographical location within a shell script eamani_sun Shell Programming and Scripting 1 05-13-2008 10:27 AM
script to check for a condition inside a file kiran1112 Shell Programming and Scripting 11 03-21-2007 09:38 AM
excutable script to copy a file to a different location. nazehcalil UNIX for Dummies Questions & Answers 4 12-21-2006 09:17 AM
running sed inside script file bajaj111 UNIX for Dummies Questions & Answers 4 11-08-2006 06:58 AM
Creating a file inside a script sendhilmani Shell Programming and Scripting 11 03-21-2006 08:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-14-2006
asami asami is offline
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 07:01 AM..
  #2 (permalink)  
Old 03-14-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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 ;
  #3 (permalink)  
Old 03-14-2006
asami asami is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 14
no it doesnt work
  #4 (permalink)  
Old 03-14-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
  #5 (permalink)  
Old 03-14-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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.
  #6 (permalink)  
Old 03-14-2006
asami asami is offline
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
  #7 (permalink)  
Old 03-14-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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 ;
Sponsored Links
Closed Thread

Bookmarks

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 02:52 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