如何找到我正在执行的 tcsh shell 脚本的位置?

发布于 2024-08-27 09:29:26 字数 228 浏览 6 评论 0原文

假设我将一个可执行的 tcsh 文件放在 /path/to/my_script.csh 中

,而我的当前目录在任何地方,例如我在 /path 中,

所以我输入 to/my_script.csh

我想在 my_script.csh 中有一行将返回“/path/to/my_script.csh” - 就像 ruby​​ 的一样

__FILE__

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__

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

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

发布评论

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

评论(5

深海蓝天 2024-09-03 09:29:26

在 c shell 中,尝试这样:

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

In c shell, try like this:

set rootdir = `dirname $0`
set abs_rootdir = `cd $rootdir && pwd`
echo $abs_rootdir
嗫嚅 2024-09-03 09:29:26

如果你想确保相同的结果(完整路径和脚本名称),请尝试这样的操作:

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

然后你可以将其称为 foo.sh, ./foo.sh, some/lower/dir/foo.sh 并仍然得到相同的结果结果无论怎么称呼。

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.

叹梦 2024-09-03 09:29:26

如果您想要绝对路径,那么这应该可以帮助您:

#!/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"

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

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/

白云不回头 2024-09-03 09:29:26
#!/bin/tcsh
echo "I am $0."
#!/bin/tcsh
echo "I am $0."
百合的盛世恋 2024-09-03 09:29:26

当从终端和另一个脚本获取数据时,以下方法都有效:(

假设要获取的文件名是“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`

第一个语句列出当前进程的打开文件描述符(包括源脚本)和 grep 查找其完整路径。

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.

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