views:

1202

answers:

6

I am running some executables while connected to a local unix server box.

Say, I'm trying to run an executable 'abc'. Now the server might have provided an alias for 'abc'.. How do I get to know of this path? As in, if I invoke 'abc', it might actually run it from, say, /opt/corp/xyz/abc .. How do I get to know from what path I'm invoking the executable?

By the way I'm running on HP-UX :D

+6  A: 

"which abc" to show which abc you would be calling

or "alias" to list aliases

perhaps "echo $0" from inside a script, or retrieving argv[0] some other way.

Joe Koberg
+1  A: 

Does HP-UX have the "which" command? Run:

which abc

If you have it, the which command will tell you which abc program will run from your $PATH.

jhs
+1  A: 

If you are running using the PATH environment variable, you could try:

$ which abc
or
$ whereis abc
If there is a symbolic link for the command and you want to know the *"real"* target, you can use something like:
$ readlink /opt/corp/xyz/abc

I do not have access to an HPUX system in front of me right now, but this should work:

$ ls -l /opt/local/bin/wish
lrwxr-xr-x  1 root  admin  22 Feb  3 21:56 /opt/local/bin/wish@ -> /opt/local/bin/wish8.5
$ readlink /opt/local/bin/wish
/opt/local/bin/wish8.5

If the command is based on an alias, the following will reveal the alias definition.

$ alias abc

depending on how your system is configured, the above commands should provide answers to multiple variations of your question.

in Perl:

$running_script = $0;

from Python, see SO http://stackoverflow.com/questions/606561/how-to-get-filename-of-the-main-module-in-python

popcnt
+1  A: 

From a command line terminal:

$ which abc

/opt/corp/xyz/abc

+1  A: 

Thanks all! 'which' was the commmand I was after! I'm facepalming myself now as I had already known the command (in Ubuntu).. And it does work like a charm in HP-UX!

EDIT : 'whereis' suggested by popcnt is even more appropriate! Thanx a lot man!

halluc1nati0n
if you like mine, go ahead and +1 it, and if satisfactory, you could mark it as the answer to your question. That is typically what you'll see here on SO, instead of an "answer" from the original poster.
popcnt
A: 

The correct way to get the path of a script on Unix is:

dir=$(cd $(dirname "$0"); pwd)

Background: $0 is the filename+path of the script relative to the current directory. It can be absolute (/...) or relative (../, dir/...). So the $(dirname "$0") returns the path (without the filename). Mind the quotes; "$0" can contain spaces and other weird stuff.

We then cd into that directory and pwd will return the absolute path where we end up.

Works for ksh and bash.

In a C program, you should check argv[0]. I'm not sure whether the shell will put the complete path in there. If you have trouble, I suggest to wrap your executable in a small script which prepares the environment and then invoke your executable with:

exec "$dir/"exe "$@"
Aaron Digulla