如何检测源代码而不是在shell脚本中运行?

时间:2017-06-09 13:59:34

标签: python bash shell

在python中,你可以检测脚本是否被另一个脚本执行import而不是

if __name__ == '__main__':
    # We are run directly.

有没有办法在shell脚本中执行相同的操作? 我在脚本中有几个函数,我希望能够在不执行它们的情况下获取它们。

1 个答案:

答案 0 :(得分:1)

到目前为止,我已经提出了这些:

if [ "$0" != "bash" ]
then
    # We are run directly because
    # $0 == our filename
    # Or is it? It could be a different shell!
fi

if [ "$(basename $0)" = "foo.sh" ]
then
    # We are run directly, because $0 == our filename.
    # But what if we get mv'ed to some other filename???
fi

是否有任何脆弱的解决方案?