bash脚本中调用与xargs的功能 [英] Calling functions with xargs within a bash script
本文介绍了bash脚本中调用与xargs的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用xargs的调用并行更复杂的功能。
I am trying to use xargs to call a more complex function in parallel.
#!/bin/bash
echo_var(){
echo $1
return 0
}
seq -f "n%04g" 1 100 |xargs -n 1 -P 10 -i echo_var {}
exit 0
这将返回错误
xargs: echo_var: No such file or directory
这是我如何使用xargs的任何想法要做到这一点,或任何其他解决方案(S)将受到欢迎。
谢谢!
Any ideas on how I can use xargs to accomplish this, or any other solution(s) would be welcome. Thanks!
推荐答案
导出函数应该这样做(未经测试):
Exporting the function should do it (untested):
export -f echo_var
seq -f "n%04g" 1 100 | xargs -n 1 -P 10 -I {} bash -c 'echo_var "$@"' _ {}
您可以使用内置的的printf
,而不是外部 SEQ
:
You can use the builtin printf
instead of the external seq
:
printf "n%04g\n" {1..100} | xargs -n 1 -P 10 -I {} bash -c 'echo_var "$@"' _ {}
此外,使用返回0
和退出0
这样的口罩可能受该命令所产生的任何误差值preceding它。此外,如果没有错误,这是默认的,因此有些多余。
Also, using return 0
and exit 0
like that masks any error value that might be produced by the command preceding it. Also, if there's no error, it's the default and thus somewhat redundant.
这篇关于bash脚本中调用与xargs的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文