How can I find the location of the tcsh shell script I'm executing?
Asked Answered
B

5

11

Say I put an executable tcsh file in /path/to/my_script.csh

and my current directory is anywhere, for example I'm in /path

So I type to/my_script.csh

I want to have a line in my_script.csh that will return "/path/to/my_script.csh" - like ruby's

__FILE__
Butanol answered 1/4, 2010 at 20:4 Comment(0)
B
11

In c shell, try like this:

set rootdir = `dirname $0`
set abs_rootdir = `cd $rootdir && pwd`
echo $abs_rootdir
Buffon answered 24/7, 2014 at 23:2 Comment(3)
Unfortunately when I do this in my #!/bin/csh script, and run it with a relative path (./myscript.csh), it always returns my home directory. Does not matter where I put this script. dirname $0 is '.' and pwd returns my $HOMEHautbois
@Hautbois The problem you described doesn't happen to me. Do you still see the same issue? You may need check your .cshrc file and see if there are any special configurations related to directory path setting.Buffon
@Buffon No, strange (and good). I don't see this behavior anymoreHautbois
L
6

If you want to ensure the same result (full path and script name) try something like this:

...
rootdir=`/bin/dirname $0`       # may be relative path
rootdir=`cd $rootdir && pwd`    # ensure absolute path
zero=$rootdir/`/bin/basename $0`
echo $zero
...

Then you can call it as foo.sh, ./foo.sh, some/lower/dir/foo.sh and still get the same result no matter how it is called.

Lightly answered 4/1, 2012 at 21:52 Comment(1)
Wrong shell -_-Strawworm
C
6

If you want an absolute path then this should help you out:

#!/bin/tcsh -f 
set called=($_)

if ( "$called" != "" ) then  ### called by source 
   echo "branch 1"
   set script_fn=`readlink -f $called[2]`
else                         ### called by direct execution of the script
   echo "branch 2"
   set script_fn=`readlink -f $0`
endif

echo "A:$0"
echo "B:$called"
set script_dir=`dirname $script_fn`

echo "script file name=$script_fn"
echo "script dir=$script_dir"

Source: http://tipsarea.com/2013/04/11/how-to-get-the-script-path-name-in-cshtcsh/

Coseismal answered 10/7, 2017 at 13:0 Comment(2)
Thanks for saving me any more frustration on how to differentiate between the source and a calling scriptLassie
This gives the correct result if sourced manually but not if sourced by another script. Is there a way, where it works in this case as well?Stowage
E
1
#!/bin/tcsh
echo "I am $0."
Eyecatching answered 1/4, 2010 at 20:31 Comment(1)
This gives the path from the current directory to the script, not the absolute path.Rein
S
1

The following method works both when sourced from terminal and from another script:

(Assuming the filename to be sourced is "script.cshrc")

set sourced=`ls -l /proc/$$/fd | sed -e 's/^[^/]*//' | grep "/script.cshrc"`
set source_dir="`dirname $sourced`"
set script_dir=`realpath $source_dir`

The first statement lists open file descriptors of the current process (which includes the sourced script) and grep for its full path.

Swaney answered 30/4 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.