Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get `__FILE__` when sourcing a csh script

Tags:

csh

tcsh

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

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

Since I'm using this to set vars in the parent shell, I have to use source.

What to do?

like image 849
mmccoo Avatar asked Dec 02 '10 17:12

mmccoo


2 Answers

If you access $_ on the first line of the file, it will contain the name of the file if it's sourced. If it's run directly then $0 will contain the name.

#!/bin/tcsh
set called=($_)
if ($called[2] != "") echo "Sourced: $called[2]"
if ($0 != "tcsh") echo "Called: $0"
like image 111
Dennis Williamson Avatar answered Oct 11 '22 23:10

Dennis Williamson


This is hard to read, but is actually works:

If your script is named test.csh

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

like image 40
engtech Avatar answered Oct 11 '22 23:10

engtech