Jul*_*ann 10 directory shell path tcsh
假设我在/path/to/my_script.csh中放了一个可执行的tcsh文件
我当前的目录是任何地方,例如我在/ path
所以我输入/ my_script.csh
我想在my_script.csh中有一行返回"/path/to/my_script.csh" - 就像ruby的
__FILE__
Run Code Online (Sandbox Code Playgroud)
在c shell中,尝试这样:
set rootdir = `dirname $0`
set abs_rootdir = `cd $rootdir && pwd`
echo $abs_rootdir
Run Code Online (Sandbox Code Playgroud)
小智 7
如果你想确保相同的结果(完整路径和脚本名称)尝试这样的事情:
...
rootdir=`/bin/dirname $0` # may be relative path
rootdir=`cd $rootdir && pwd` # ensure absolute path
zero=$rootdir/`/bin/basename $0`
echo $zero
...
Run Code Online (Sandbox Code Playgroud)
然后你可以将它称为foo.sh,./ foo.sh,some/lower/dir/foo.sh,无论如何调用它仍然会得到相同的结果.
如果您想要绝对路径,那么这应该可以帮助您:
#!/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"
Run Code Online (Sandbox Code Playgroud)
来源:http : //tipsarea.com/2013/04/11/how-to-get-the-script-path-name-in-cshtcsh/