Bil*_*ain 8 parallel-processing bash zsh gnu-parallel
如何从zsh导出函数,以便我可以在gnu parallel中使用它?
例:
function my_func(){ echo $1;}
export -f my_func
parallel "my_func {}" ::: 1 2
Run Code Online (Sandbox Code Playgroud)
在bash中输出
1
2
Run Code Online (Sandbox Code Playgroud)
而在zsh中它将输出错误消息
/bin/bash: my_func: command not found
/bin/bash: my_func: command not found
Run Code Online (Sandbox Code Playgroud)
zsh没有导出功能的概念.export -f somefunc将打印功能定义,它不会导出功能.
相反,您可以依赖于bash函数作为常规变量导出的事实,从以下开始():
export my_func='() { echo "$1"; }'
parallel --gnu "my_func {}" ::: 1 2
Run Code Online (Sandbox Code Playgroud)
根据其他人的答案.您可以编写一个函数来导出已定义为bash的zsh函数
function exportf (){
export $(echo $1)="`whence -f $1 | sed -e "s/$1 //" `"
}
Run Code Online (Sandbox Code Playgroud)
用法
function my_func(){
echo $1;
echo "hello";
}
exportf my_func
parallel "my_func {}" ::: 1 2
Run Code Online (Sandbox Code Playgroud)
自 2014 年以来发生了很多变化。
今天你只需要做:
# Activate env_parallel function (can be done in .zshenv)
. `which env_parallel.zsh`
function my_func(){ echo $1;}
env_parallel "my_func {}" ::: 1 2
Run Code Online (Sandbox Code Playgroud)
如果您的环境很大:
# Activate env_parallel function (can be done in .zshenv)
. `which env_parallel.zsh`
# Record which environment to ignore
env_parallel --session
function my_func(){ echo $1;}
env_parallel "my_func {}" ::: 1 2
Run Code Online (Sandbox Code Playgroud)