如何检测,如果脚本正在采购 [英] How to detect if a script is being sourced

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

问题描述

我有一个剧本,我不希望它叫退出,如果它被采购。起初我以为检查,如果 $ 0 ==庆典但是这有问题,如果脚本是从另一个脚本来源,或从 KSH <用户来源它/ code>。是否存在,如果一个脚本被检测来源可靠的方式?

I have a script where I do not want it to call exit if it's being sourced. Initially I though checking if $0 == bash but this has problems if the script is sourced from another script, or if the user sources it from ksh. Is there a reliable way of detecting if a script is being sourced?

推荐答案

这似乎是Bash和科恩之间移植:

This seems to be portable between Bash and Korn:

[[ $_ != $0 ]] && echo "Script is being sourced" || echo "Script is a subshell"

与此类似或类似`路径分配的行=$ _(与后面的测试和行动)必须在脚本的第一行或在家当(其中,如果使用的话,应该经过线是KSH,以便为它下的大多数情况下工作)。

A line similar to this or an assignment like `pathname="$_" (with a later test and action) must be on the first line of the script or on the line after the shebang (which, if used, should be for ksh in order for it to work under the most circumstances).

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

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