Cygwin(或实际的 UNIX shell)是否有一些命令可以将名称从另一个命名空间导入到当前命名空间,就像在 Python 中一样?

发布于 2024-12-02 19:33:15 字数 256 浏览 0 评论 0原文

在Python中,我们可以使用“import”将另一个命名空间的名称导入到当前命名空间中。

同样,UNIX shell 脚本中是否存在像“命名空间”这样的概念?如果是这样,那么 Cygwin(或实际的 UNIX shell)是否有一些命令可以将名称从另一个名称空间导入到当前名称空间,就像在 Python 中一样?谢谢。

具有管理员权限的社区成员请注意:我真的认为这个问题是一个编程问题,而不是一个“超级用户”问题。如果您不同意,请详细说明原因。非常感谢您抽出时间。

In Python, we can use "import" to import the names of another namespace into the current namespace.

Similarly, is there a notion like "namespace" in existence in UNIX shell scripting at all? If so, then does Cygwin (or an actual UNIX shell) have some command to import names from another namespace to the current namespace, as in Python? Thanks.

Note to the community members with admin priviledges: I really think this question IS a programming question instead of a "superuser" question. Please kindly elaborate on why if you disagree with that. Thanks a lot for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-12-09 19:33:15

没有办法完全按照你的要求去做。

source envFile 命令及其替代 。 envFile 可能非常有帮助。

envFile 文件只是环境分配的列表。

FrontOfficeSystem=MyFrontOffice
BackOfficeSystem=myBackOffice

当您在脚本中包含命令来“获取”envFile(任何名称都可以)时,shell 会读取代码,就像直接在主 shell 脚本中一样。就像很多语言中的“包含”一样。但是命名空间,...不。参见下一篇。

更有帮助:请参阅高级 Bash 脚本中的间接引用,这可能比使用更好eval ... (如下),但我还没有机会使用它。

最后,您还可以从 eval 和 varname 间接寻址中受益,即这

 src=FrontOffice
 eval \${src}System="${src} has data"

 src=BackOffice
 eval \${src}System="${src} has data"

不是一个很好的例子,但我无法访问我真正想到这个想法的脚本。它帮助我对每个数据 src 通用化 (sp) 一些代码,否则这些代码必须重复 10 次(我将重复的代码块放入 for 循环中,将 src 名称作为 for(each ),那么 eval 会将 ${src}System 扩展为 FrontOfficeSystem、BackOfficeSystem)。如果您的 src 列表的值中包含空格,那么所有的赌注都会失败。

在终端窗口中使用 set -vx 并复制/粘贴上面的代码以查看其工作原理。这可能有帮助。

我希望这有帮助。

PS,由于您似乎是新用户,如果您得到的答案对您有帮助,请记住将其标记为已接受,和/或给它 +(或 -)作为有用的答案。

There is no way to do exactly what you are asking for.

The source envFile command and it's alternate . envFile can be very helpful.

envFile file will just be a list of environment assingments.

FrontOfficeSystem=MyFrontOffice
BackOfficeSystem=myBackOffice

When you include the command in your script to 'source' the envFile (any name will work), the shell reads the code as if it was directly in your main shell script. Like 'include' in a lot of langauges. But namespaces, ... nope. See next.

More helpful : see indirect references in advanced Bash scripting, this is probably better than using eval ... (per below), but I haven't had the opportunity to work with it.

finally, you may also benefit from eval and varname indirection, i.e.

 src=FrontOffice
 eval \${src}System="${src} has data"

 src=BackOffice
 eval \${src}System="${src} has data"

Not a great example, but I don't have access to the scripts where I really went to town on this idea. It helped me genericize (sp) some code that otherwise would have had to be repeated 10 times, for each data src (I put the repeating block of code in a for loop, with the src names as the element list for the for(each), then the eval would expand ${src}System as FrontOfficeSystem, BackOfficeSystem). If you windup with spaces in your values for your src list, then all bets are off.

use set -vx in your terminal window and copy/paste above code to see how it works. It might help.

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文