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

casperOne
  • 73,706
  • 19
  • 184
  • 253

6 Answers6

10

"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
  • 25,416
  • 6
  • 48
  • 54
4

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 How to get filename of the __main__ module in Python?

Community
  • 1
  • 1
popcnt
  • 4,519
  • 1
  • 16
  • 14
3

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.

JasonSmith
  • 72,674
  • 22
  • 123
  • 149
2

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
  • 893
  • 2
  • 13
  • 18
  • 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 Mar 19 '09 at 16:08
1

From a command line terminal:

$ which abc

/opt/corp/xyz/abc

user79878
  • 775
  • 9
  • 13
1

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
  • 321,842
  • 108
  • 597
  • 820