path of the running script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting path of the running script
# 15  
Old 11-14-2008
I see, I think this should work then:

Code:
#!/bin/bash
dirname $(which $0)

If you require the path including the filename, this should suffice:
Code:
#!/bin/bash
which $0

# 16  
Old 11-15-2008
I had this idea and thought it was great for a certain time untill I found a failure.
Consider the following script named test in your home directory:
Code:
#!/bin/bash
which $0

And now type the following commands:
Code:
$ ./test
/home/santiago/test
$ bash test
/usr/bin/test

The problem comes from the fact that there is another exe named test in the PATH.

Last edited by radoulov; 11-15-2008 at 05:47 AM..
# 17  
Old 11-15-2008
Quote:
Originally Posted by chebarbudo
What I want is to be able to know the address (full path) of the script that is currently running without knowing how the user called it.

Thank you for your proposal Scrutinizer but once again, this is a script that will fail in some cases :
Code:
# /bin/s
/bin
# s
/bin
# bash s
/root

In the last case, this command fails.
So far the only command I found that works all the time is my own insanely complicated one.
It seems you can use the non-standard command realpath.
If I'm not missing something again it's quite easy in this case ...

Code:
% ./s
/home/radoulov/t
% bash s
/home/radoulov/t
% /home/radoulov/t/../../radoulov/t/s
/home/radoulov/t
% . s
/home/radoulov/t
% cat s
#! /bin/sh

p="$(realpath "$0")"

printf "%s\n" "${p%/*}"

# 18  
Old 11-15-2008
Quote:
Originally Posted by chebarbudo
I had this idea and thought it was great for a certain time untill I found a failure.
Consider the following script named test in your home directory:
Code:
#!/bin/bash
which $0

And now type the following commands:
Code:
$ ./test
/home/santiago/test
$ bash test
/usr/bin/test

The problem comes from the fact that there is another exe named test in the PATH.
This is correct, if it's in the path before your script, it IS the real running program when invoked as you describe.

Last edited by radoulov; 11-15-2008 at 06:04 AM..
# 19  
Old 11-15-2008
Quote:
Originally Posted by radoulov
This is correct, if it's in the path before your script, it IS the real running program when invoked as you describe.
Not quite true:
Code:
~# cat /root/s
#!/bin/bash
echo "I'm a user created script stored in /root/"
which $0
~# cat /usr/local/bin/s
#!/bin/bash
echo "I'm a user created script stored in /usr/local/bin/"
which $0
~# bash s
I'm a user created script stored in /root/
/usr/local/bin/s

# 20  
Old 11-15-2008
Quote:
Originally Posted by radoulov
It seems you can use the non-standard command realpath.
If I'm not missing something again it's quite easy in this case ...

Code:
% ./s
/home/radoulov/t
% bash s
/home/radoulov/t
% /home/radoulov/t/../../radoulov/t/s
/home/radoulov/t
% . s
/home/radoulov/t
% cat s
#! /bin/sh

p="$(realpath "$0")"

printf "%s\n" "${p%/*}"

Counter example:
Code:
~# cat /usr/local/bin/s
#! /bin/sh
echo "I'm a user created script stored in /usr/local/bin/"
p="$(realpath "$0")"
printf "%s\n" "${p%/*}"
~# bash s
I'm a user created script stored in /usr/local/bin/
s: No such file or directory

# 21  
Old 11-15-2008
Quote:
Not quite true: ...
Quote:
Counter example: ...
Good points. You're right. So, as far as your initial question is concerned, _I_ have not a simpler solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

2. Linux

Cmd is not running , eventhough its in right path

I can not run iwmodelc from command line. It says file not found. Please look at the command line output below-ksh: /app/Autonomy/Interwoven/TeamSite/bin: cannot execute $ pwd /app/Autonomy/Interwoven/TeamSite/bin $ ls email_to.ipl iwfilestate iwrecentusers iwabort ... (2 Replies)
Discussion started by: paramshamnani
2 Replies

3. HP-UX

upgrade path for Digital Alphastation 200 running openVMS 6.1?

I am not sure here is the right place to post this question. as openvms is under HP's belt now, i guess here might be the right place to try my luck. we have very old machines here which are still running quite well. (APLHASTATION 200 WITH OPENVMS 6.1) the only problem is difficulty of... (2 Replies)
Discussion started by: Cheong
2 Replies

4. UNIX for Dummies Questions & Answers

running script without a path

can someone tell me how could I run a script in an environment that does not include a path? like the environment provided by cron? (1 Reply)
Discussion started by: ikeQ
1 Replies

5. Shell Programming and Scripting

absolute path for a script ran with relative path

I have a script in which i want to print absolute path of the same script irrespective of path from where i run script. I am using test.sh: echo "pwd : `pwd`" echo "script name: $0" echo "dirname: `dirname $0`" when i run script from /my/test/dir/struct as ../test.sh the output i... (10 Replies)
Discussion started by: rss67
10 Replies

6. Programming

get the path of current running program

How does the program know the full path of itself when the program is running in certain diretory? BTW, I have no "argv" information of main() functino. (The program is written in C++ on linux platform) (1 Reply)
Discussion started by: princelinux
1 Replies

7. Shell Programming and Scripting

Finding path of a running script

Hi, I just want to know any code by which i can get the path of the script which i am running. This is required to store the output in the same directory from where the script is running. pwd fails if I give absolute path of script from some other directory. Thanks in advance Puneet (3 Replies)
Discussion started by: puneet
3 Replies

8. Solaris

Running from Shell Vs running from RC script

Hi, i have a script which need to do behave differently when run as a startup process from init.d/ rc2.d script and when run manually from shell. How do i distinguish whether my script is run by init process or by shell?? Will the command /proc/$$/psinfo | grep "myscript" work well???... (2 Replies)
Discussion started by: vickylife
2 Replies

9. Shell Programming and Scripting

running a command without having to provide the path

ok, i am aware of adding the directory where the files are located into the .profile file but the problem is, its not working for me. can someone please spell out how to properly edit these configuration files so i can run a command anywhere on the system without having to be in the... (2 Replies)
Discussion started by: Terrible
2 Replies

10. Programming

find the fully-qualified path for the app my module is running in

Hi- I need the cpp call that will tell me the full path to the app I'm running in. For example, I'm running in a loaded library for either mozilla or firefox, but would like to know the full path to the executable /usr/bin/firefox /usr/bin/mozilla /usr/local/firefox1_5 etc... (For... (4 Replies)
Discussion started by: erwinfletch
4 Replies
Login or Register to Ask a Question