Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Telling where the script is

Status
Not open for further replies.

peterneve

Programmer
Jan 24, 2002
50
GB
Hi,

I have a script which greps itself. This work great when the user is in the same directory, but falls down when run from a different folder. How can I get the script to find where it actually is (not where the user is)?

Thanks,
 
If $0 don't hold the full pathname you may try to play with the type builtin or alias.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If you only have one instance of your script, it can be found by:
Code:
whereis myscript
but manuals, rc-files, ... are found to.
So use which:
Code:
which myscript

If the user calls it with full path, and it isn't in THE path, or another version is found earlier in the path, which will not work correctly.

seeking a job as java-programmer in Berlin:
 
fullPath=$(whence $0)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
to get the absolute path, in bash
Code:
#!/usr/sbin/bash
SCRIPTSELF=$0
FIRST_CHAR=${SCRIPTSELF:0:1}
if [[ '/' == $FIRST_CHAR ]]
then
        echo $0
else
        echo $PWD/$0
fi

and the output:
Code:
l1000db2 # /tmp/get_myself.sh
/tmp/get_myself.sh
l1000db2 # cd /tmp
l1000db2 # ./get_myself.sh
/tmp/./get_myself.sh

___
____
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top