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?
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"
This is hard to read, but is actually works:
If your script is named test.csh
/usr/sbin/lsof +p $$ | \grep -oE /.\*test.csh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With