tcsh : path of sourced file
I am sourcing a file under tcsh. This file could be anywhere on the filesystem. How can I retrieve the path of my sourced file ?
$0 won't work : I don't execute the file, I source it.
Many thanks !
If you do not want to use lsof
, use below for csh only.
set script_path = `ls -l /proc/$$/fd | sed -e 's/^[^/]*//' | grep "/script_name"`
$$
is the pid of the current running thread.
No matter which method (sourced by user or in other script), the script be called, is also be opened. So filter the content of fd will get the real path of script.
This also works when the script is soft linked to other path!
A little bit of grepping gives me what I want.
There is one thing I know for sure : the basename of the file (not the whole path). In my case, source_me.tcsh. So we can query lsof for the current shell PID and grep the absolute path.
$$ gives you the PID.
/usr/sbin/lsof +p $$ | grep -oE /.\*source_me.tcsh