采购csh脚本时如何获取__FILE__ [英] How do I get `__FILE__` when sourcing a csh script

查看:85
本文介绍了采购csh脚本时如何获取__FILE__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,用于在调用csh shell中设置一些env var.其中一些变量取决于脚本的位置.

I have a script that is used to set some env vars in the calling csh shell. Some of those variables depend on the location of the script.

如果文件是正确的csh脚本,我可以使用$ 0访问 __ FILE __ ,但是如果我使用source运行脚本,它只会告诉我csh或tcsh.

If the file is a proper csh script, I can use $0 to access __FILE__ but if I run the script using source, it just tells me csh or tcsh.

由于我正在使用它在父shell中设置var,因此我必须使用source.

Since I'm using this to set vars in the parent shell, I have to use source.

该怎么办?

推荐答案

如果在文件的第一行访问 $ _ ,则该文件将包含源文件的名称.如果直接运行,则 $ 0 将包含名称.

If you access $_ on the first line of the file, it will contain the name of the file if it's sourced. If it's run directly then $0 will contain the name.

#!/bin/tcsh
set called=($_)
if ($called[2] != "") echo "Sourced: $called[2]"
if ($0 != "tcsh") echo "Called: $0"

这篇关于采购csh脚本时如何获取__FILE__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆