How to find the path from the current current process / executable?

I run some executables when connected to a local unix server.

Let's say I'm trying to run the 'abc' executable. Now the server may have provided an alias for "abc". How do I find out about this path? As if I call "abc", it can actually run it, say / opt / corp / xyz / abc. How do I know which path I am running the executable from?

By the way, I work on HP-UX: D

+5
source share
6 answers

" which abc" to show which one abcyou are calling

or " alias" to list aliases

, "echo $0" script, argv [0] .

+10

PATH, :

$ which abc

$ whereis abc

, "" , - :

$ readlink /opt/corp/xyz/abc

HPUX , :

$ 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

, .

$ alias abc

, , .

Perl:

$running_script = $0;

Python, . SO __main__ Python?

+4

HP-UX "which"? Run:

which abc

, which , abc $PATH.

+2

! "which" , ! , ( Ubuntu). HP-UX!

EDIT: "whereis", popcnt, ! Thanx !

+2

:

$, abc

////

+1

script Unix:

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

: $0 - + script . (/...) (../, dir/...). , $(dirname "$0") ( ). ; "$ 0" .

Then we will be cdin this directory and pwdwill return the absolute path in which we will end.

Works for kshand bash.

In a C program, you should check argv[0]. I'm not sure if the shell will put the full path there. If you have problems, I suggest wrapping your executable with a small script that prepares the environment, and then calls your executable with:

exec "$dir/"exe "$@"
+1
source

All Articles