0

Linux/CSHELL:

How can I get the path to a tcsh script that is being sourced?
This is not $PWD. Rather, the path to the script (file).

Versions/specifics:

% echo $SHELL $PLATFORM
/tool/pandora/bin/tcsh linux_3.10.0_64
ls -l /tool/pandora/bin/tcsh
lrwxrwxrwx 1 pandora pandora 33 Oct 20 2016 /tool/pandora/bin/tcsh -> ../.package/tcsh-6.19.00/bin/tcsh

There are lots of notes in here for this type of thing. But I didn't see one for linux c-shell.

Thanks in Advance !

Example:

I have a file /home/myid/my_cshell.csh...

set a = `readlink -f ${0}`
echo $a
echo ${0}

source /home/myid/my_cshell.csh
/tool/pandora/.package/tcsh-6.19.00/bin/tcsh
/tool/pandora/bin/tcsh

This is giving me the path to tcsh, not /home/myid/my_cshell.csh

The script above works fine if I tcsh it, but not source it. I need the path to the file being sourced.

daveg
  • 1,051
  • 11
  • 24
  • Is this what you are looking for? https://stackoverflow.com/q/192319/2422776 (admittedly it's about bash, but I think it should be applicable to cshell too) – Mureinik Jan 23 '20 at 21:51
  • It's in the `$0` variable. Unlike with the bourne(-again) shell, you can't fake it via `csh -c '...' argv0`: in `csh`, `argv0` will become `$1` instead of `$0`. –  Jan 23 '20 at 21:57
  • You can't, sourcing doesn't exec a new shell to replace the parent ... what's the purpose of this exercise, though? – tink Jan 23 '20 at 22:56
  • Why are you using c-shell? Is sh/bash/ash, or some more "normal" shell not available (if this is the case, what linux distro is it, so I can make sure to avoid it)? Is this some act of self-flagellation (you have sinned, and C-shell is your punishment) ??? – Z4-tier Jan 23 '20 at 23:36
  • set the file name in the main script, before sourcing it. You can use an alias for that: `alias sourcex 'set the_file=\!:1; source \!:1'`, then `sourcex /path/to/file` and in `/path/to/file` `ls $the_file:q`. –  Jan 24 '20 at 04:07
  • @Z4-tier I'm not sure that getting the path of the file being sourced (eg. with `. file`) from inside it is too obvious in sh/bash/ash either. –  Jan 24 '20 at 04:10
  • @mosvy My question was more philosophical: in 2020, is there *any* reason to script in `csh`??? Seems about as popular as `smalltalk` or `pascal` these days... – Z4-tier Jan 25 '20 at 00:45

1 Answers1

0

assuming you have readlink available:

readlink -f "${0}"

should work. ${0} will give you the relative path, and readlink -f will resolve it to the absolute path.

There must already be some way to reference the file... If not, how could it be sourced in the first place? So I suppose the problem is that ${0} referecnes the parent's file system location, and not the sourced file. In that case, readlink -f <file> should still work, but maybe try calling it first, and then pass the result when you run source. Like this:

main.csh:

#!/bin/csh

set p=`readlink -f source.csh`
source source.csh "${p}"

source.csh:

#!/bin/csh
set pmain=`readlink -f "${0}"`
echo "my path is ${1}. main.csh's path is ${pmain}"
Z4-tier
  • 7,287
  • 3
  • 26
  • 42
  • just like in the bourne shell, writing `${var}` instead of `$var` does not add any extra value; you should _quote_ it with `$var:q` (which also works with arrays) if you don't want it to be split and globbed. –  Jan 24 '20 at 04:19
  • @mosvy https://stackoverflow.com/a/8748880/6689725. Agreed on quoting though, I corrected that. – Z4-tier Jan 25 '20 at 00:29