- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑这两个脚本:
envSetter.sh:
#!/bin/bash
export ENV_VAR=6
return 3
sourceable.sh:
#!/bin/bash
function main()
{
local var=5
source envSetter.sh
}
main "$@"
unset -f main
envSetter.sh
是一个设置一些变量(并可能执行影响当前 shell(环境)的其他操作)的脚本。我需要创建一个可获取源的脚本,该脚本将获取 envSetter.sh 的源代码,但也会自行清理。 IE。运行 source sourceable.sh
后,我希望我的 shell 设置 ENV_VAR
但我不想设置 main
(或任何其他sourceable.sh
使用)定义。
上面的脚本实现了这一点。然而,最重要的是,我希望 sourceable.sh
能够返回 main
返回的任何退出代码。现在,脚本返回 unset -f main
的结果。如果我删除该命令,我的脚本将保留我不想要的定义的 main
函数。如果我尝试使用临时变量:
main "$@"
result=$?
unset -f main
return $result
它将返回我期望的结果,但也会留下我不想要的定义的结果
。我也可以根本不使用 main
函数,并将所有代码放在脚本的顶层,但这会从 main
内部暴露更多垃圾(例如 >local var
),我也不想要。
有没有办法获取 envSetter.sh
的源代码(即让它在当前 shell 中设置变量),返回 main
返回的内容,而不返回 使用 sourceable.sh
使用的任何内容“污染”我当前的 shell?澄清一下:我想要 envSetter.sh
影响我当前的 shell,但我不想 sourceable.sh
到。
最佳答案
如何将 unset
命令隐藏在 main
函数中?像这样:
#!/bin/bash
main() {
unset -f main
local var=5
source envSetter.sh
}
main "$@"
请注意,返回状态只会传播,因为 source envSetter.sh
是 main
中的最后一个命令,它将成为 的返回状态main
,同样,由于 main
是脚本中的最后一个命令,因此它将成为脚本的返回状态。
顺便说一句,在本应使用 source
运行的东西上使用 shebang 没有多大意义。我倾向于使用这样的东西:
#!/bin/echo Run this script with the source or . command.
关于bash - 不 "pollute"环境的可来源脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67902853/
好的,我有一个名为 Window 的类,它旨在满足将窗口组合在一起的所有要求,而且效果非常好。我唯一的问题是我包含 window.h 的任何文件也包含所有窗口。我已经将所有特定于 Windows 的元
考虑这两个脚本: envSetter.sh: #!/bin/bash export ENV_VAR=6 return 3 sourceable.sh: #!/bin/bash function mai
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
在 ruby 中,一些 gems 选择“污染全局命名空间”。 这是什么意思? 我如何才能看到它发生在哪里? 为什么 gem 需要这样做? 当面对污染全局命名空间和冲突的两个 gem 时,当我选择“
我想为要在给定模块中重用的构造函数定义一个类型别名,而不必在它们定义的模块的路径前面加上前缀。我还想通过执行 open 来避免“污染”后一个模块/include这将导入太多定义。 这是我想要的示例(无
我的测试是这样的: const aframeViewer = new VRMaker.AframeViewer() let el let panoramas let panorama beforeEa
我是 Angular.js 的新手,我读到 Controller 不应该污染全局命名空间。 这到底是什么意思 以及为什么 angular.module('SomeApp',[]).controller
我正在考虑在一个新网站中使用 React,但我仍然想知道如何使用 React 组件处理全局命名空间。例如,如果我像这样定义几个 React 组件: var MySlider = React.creat
我想尽可能地限制 AngularJS 应用程序中的“闪烁”。我使用 resolve: from the router (与 ngRouter 和 ui-router 一起使用)加载数据,以便在更改路由
背景 在 JavaScript 中导入模块时,我们会用导入的模块名称污染全局命名空间: foo.js export foo() {..}; export const bar = 3.14; index
tl;dr: 有没有一种方法可以防止更改(本质上锁定)在 include() 之前声明/定义的变量通过包含的文件调用?另外,有点相关question . 我想知道可以采取什么措施来避免包含文件的变量污
我在 React Native 中有一个应用程序,当我运行 npm install 时,我收到此错误“在 1182 个扫描包中发现 15 个漏洞(5 个中等,10 个高)”。运行 npm audit
我正在使用这样的抽象类: @SpringBootTest(classes = MyAppApplication.class, webEnvironment = WebEnvironment.RANDO
这有什么区别: Library1 = function () {}; Library1.prototype.myFunc = function (p) { function helper1(p
尝试绘制风向图,绘制速度和方向,浓度决定颜色。不幸的是,matplotlib 只支持两个变量。可以制作一个漂亮的散点图来显示我想要的内容,但不确定如何将它装箱,以便它像附件中的图像一样出现(Halli
facebook React 教程有以下代码和附注: // tutorial3.js var CommentBox = React.createClass({ render: function()
我重写了这个问题,因为它最初是用更简洁的代码示例发布的: 将具有完全可选分号的语言视为糖,即: ;;富; bar;;;;有效 foo bar foobar 有效 如果(+1); foo 在语义上与 i
我已经在切换 UIView 方面工作了一段时间,并且正在尝试一个基本的 UIView 切换,而不使用 UINavigationController。我研究了 UINavigationControlle
当我执行 npm audit 时,它给了我这个链接,说我必须在我的 NODEJS 应用程序中更新 lodash https://www.npmjs.com/advisories/782但我尝试了 np
我有一个 DataGrid .现在它绑定(bind)到 ObservableCollection在我的模型中很棒。 但是现在我要实现 a user-friendly way to add a new
我是一名优秀的程序员,十分优秀!