How to get `__FILE__` when searching for csh script

I have a script that is used to install some env vars in the calling csh shell. Some of these variables depend on the location of the script.

If the file is a valid csh script, I can use $ 0 to access __FILE__ , but if I run the script source using the source, it just tells me csh or tcsh.

Since I use this to install vars in the parent shell, I have to use the source.

What to do?

+5
source share
3 answers

If you go to $_ in the first line of the file, it will contain the name of the file if it was found. If it runs directly, then $0 will contain the name.

 #!/bin/tcsh set called=($_) if ($called[2] != "") echo "Sourced: $called[2]" if ($0 != "tcsh") echo "Called: $0" 
+2
source

This is hard to read, but actually works:

If your script is called test.csh

/usr/sbin/lsof +p $$ | \grep -oE /.\*test.csh

+1
source

This answer describes how lsof and a bit of grep magic is the only thing that seems to have a chance to work with the attached source files in tcsh:

 /usr/sbin/lsof +p $$ | grep -oE /.\*source_me.tcsh 
0
source

All Articles