1

我正在使用 tcsh,我试图获取当前 shell 脚本的路径但没有成功。

我的脚本包含:

echo $0

源 tmp.csh 返回

/usr/lbin/tcsh

dirname $0

返回 /usr/lbin

4

1 回答 1

4

使用$_(将其保存在变量中,作为脚本执行的第一件事)而不是$0.

#!/bin/tcsh
set called=($_)
if ("$called" != "") then
    echo "sourced $called[2]"    # the script was sourced from this location
endif
if ("$0" != "tcsh") then
    echo "run $0"                # the script was run from this location
endif

编辑:

也许在你的.cshrc

set basepath=/path/to/base
source "$basepath/scriptname" "$basepath"

并在您的基本脚本中:

source "$1/subscript"

除非基本脚本在您$PATH的 .

于 2011-01-06T17:46:58.033 回答