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

时光总嘲笑我的痴心妄想 提交于 2019-12-05 01:43:54

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.

In c shell, try like this:

set rootdir = `dirname $0`
set abs_rootdir = `cd $rootdir && pwd`
echo $abs_rootdir

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 excution 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/

#!/bin/tcsh
echo "I am $0."
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!