how to find Script file location inside script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find Script file location inside script
# 1  
Old 03-14-2006
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  
Old 03-14-2006
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  
Old 03-14-2006
no it doesnt work
# 4  
Old 03-14-2006
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  
Old 03-14-2006
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  
Old 03-14-2006
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  
Old 03-14-2006
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 ;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find whether a particular command has failed inside an sftp script?

hi, how can i know whether a command inside an sftp script has failed or not? i have a sftp expect script #!/usr/bin/expect spawn /usr/bin/sftp abc@ftp.abc.com expect "sftp>" send "cd dir\r" expect "sftp>" send "mput abc.txt\r" expect "sftp>" send "mput def.xls\r" expect "sftp>"... (5 Replies)
Discussion started by: Little
5 Replies

2. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

3. Shell Programming and Scripting

shell script to add input at certain location into a file

Hi, I am using Solaris 10 OS and trying to create shell script that can add input at certain location into a file. The input that I am trying to put is new domain name e.g @newdomain.com the file contains, more test01.out user/zzzz786@st.com/INBOX user/zzzz@po.com/INBOX... (8 Replies)
Discussion started by: Mr_47
8 Replies

4. Shell Programming and Scripting

Write shelll script to read file location

hi all i have a problem how to read file location..I read file as FILE=/home/tmp/new.file.but t is not useful for me.But i want my script read file location where the file is and copy in directory at boot time. Every time of booting files are copied in respective folder.please help !!!!:) (2 Replies)
Discussion started by: shubhig15
2 Replies

5. Shell Programming and Scripting

Bash script to read file location

I'm writing a bash script that reads a file location from a user, and I'm wondering how to get the script to accept tab to auto complete the directories that are input. (8 Replies)
Discussion started by: Prodiga1
8 Replies

6. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

7. Shell Programming and Scripting

deleting files inside shell script - ( using find)

Hi, I am using the command find /apps/qualdb/gpcn/scripts/cab_outbound/archive -name 'z*' -mtime +28 -exec rm {} \; in unix command prompt for deleting the files in my unix system under the specfied folder. It was succesfull. But when i running this command inside a shell script name... (2 Replies)
Discussion started by: Jayaram.Nambura
2 Replies

8. Shell Programming and Scripting

Need script to find errored files inside directories

Hi people. I working on a script to check for files that they are suposed not to be on the directory. I mean, inside of each directory it must have some files but some could be wrong, and i want to move the files that are wrong. Ex: CSPOTGET edpst/CargadoresSPOT Historicos_Spot_MDI.zip... (4 Replies)
Discussion started by: osramos
4 Replies

9. Shell Programming and Scripting

Find the geographical location within a shell script

Hi, I need a shell script that when run should be able to find the geographical location of the system. can anyone help me with this? Thanks, Sundeep (1 Reply)
Discussion started by: eamani_sun
1 Replies

10. UNIX for Dummies Questions & Answers

excutable script to copy a file to a different location.

Hi, I'm try to create an executable file to copy a file to a different location. Help plz. Thanx. (4 Replies)
Discussion started by: nazehcalil
4 Replies
Login or Register to Ask a Question