在python中,你可以检测脚本是否被另一个脚本执行import
而不是
if __name__ == '__main__':
# We are run directly.
有没有办法在shell脚本中执行相同的操作? 我在脚本中有几个函数,我希望能够在不执行它们的情况下获取它们。
答案 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
是否有任何脆弱的解决方案?