使用 TCSH 时获取 shell 脚本的完整路径时出现问题

发布于 2024-10-10 13:00:29 字数 203 浏览 0 评论 0原文

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

我的脚本包含:

echo $0

source tmp.csh 返回

/usr/lbin/tcsh

dirname $0

返回 /usr/lbin

I am using tcsh and I am trying to get path of current shell script without success.

My script contains:

echo $0

source tmp.csh returns

/usr/lbin/tcsh

dirname $0

returns /usr/lbin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞别 2024-10-17 13:00:30

使用 $_ (将其保存在变量中,作为脚本执行的第一件事)而不是 $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中,在这种情况下您'你只是让自己变得困难。

Use $_ (save it in a variable as the first thing the script does) instead of $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

Edit:

Perhaps in your .cshrc:

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

and in your base script:

source "$1/subscript"

Unless the base script is in your $PATH, in which case you're just making things difficult on yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文