简体   繁体   English

在不同的shell中执行函数

[英]execute function in different shell

I usually zsh .我通常zsh I want to execute a function in bash and return back to my original shell.我想在 bash 中执行一个函数并返回到我原来的 shell。 I can do this manually, but I want to automate it.我可以手动执行此操作,但我想自动执行此操作。 How can I do that?我怎样才能做到这一点?

For example, I am on zsh.例如,我在 zsh 上。 I want to execute func , which is a function() in bigscript.sh file.我想执行func ,它是bigscript.sh文件中的一个function() I am sourcing the bigscript.sh file in my .zshrc I tried adding shebang !#/bin/bash in the bigscript.sh file, but that does not change the shell.我的采购bigscript.sh文件在我的.zshrc我尝试添加家当!#/bin/bashbigscript.sh文件,但不改变外壳。 I tried another approach of putting function in a standalone function.sh file and added shebang to it.我尝试了另一种将function放入独立function.sh文件的方法,并向其中添加了shebang。 It worked.有效。 However, I do not want to break the files into smaller .sh files.但是,我不想将文件分成较小的.sh文件。

So, any suggestions on changing the shell for a single function..?那么,关于更改单个函数的外壳的任何建议..?

If the function is written in bash, you can not - in general - execute it from Zsh, since bash and Zsh are different languages.如果函数是用 bash 编写的,您通常不能从 Zsh 执行它,因为 bash 和 Zsh 是不同的语言。 Of course since there are some similarities, it is possible to write functions which work the same on bash on Zsh, I wouldn't rely on it.当然,由于存在一些相似之处,因此可以在 Zsh 上编写在 bash 上工作相同的函数,我不会依赖它。 Even if your function happens to be a "compatible" one at the moment, if someone modifies this function later, he possibly won't be aware that this function will be used outside of the bash context and change it in a way so that it doesn't run or - even worse - behaves differently under Zsh.即使你的函数目前碰巧是一个“兼容”的函数,如果有人稍后修改了这个函数,他可能不会意识到这个函数将在 bash 上下文之外使用并以某种方式改变它,以便它不运行,或者 - 更糟糕的是 - 在 Zsh 下表现不同。

So the only way would be to write an interface bash script, which sources the file where this function is defined, and executes it - something like因此,唯一的方法是编写一个接口 bash 脚本,该脚本获取定义此函数的文件并执行它 - 类似于

#!/bin/bash
# Interface script to call bash function foo from other languages
. file-which-defines-function-foo
foo "$@"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM