Shell脚本:使用xargs的执行shell函数平行实例 [英] Shell Scripting: Using xargs to execute parallel instances of a shell function
问题描述
我想使用的xargs在shell脚本来运行我在同一个脚本中定义一个函数的并行实例。该函数次页面的抓取,所以很重要的是,网页实际上是并行进程并行读取,而不是在后台进程(如果我的这种理解是错误的,还有两者之间的微小差别,只是让我知道)
I'm trying to use xargs in a shell script to run parallel instances of a function I've defined in the same script. The function times the fetching of a page, and so it's important that the pages are actually fetched concurrently in parallel processes, and not in background processes (if my understanding of this is wrong and there's negligible difference between the two, just let me know).
功能是:
function time_a_url ()
{
oneurltime=$($time_command -p wget -p $1 -O /dev/null 2>&1 1>/dev/null | grep real | cut -d" " -f2)
echo "Fetching $1 took $oneurltime seconds."
}
怎样才能用,可以采取的次数并行运行time_a_url作为参数形式的xargs的管做到这一点?是的,我知道GNU平行的,我只是没有安装软件的权限在哪里,我写的。
How does one do this with an xargs pipe in a form that can take number of times to run time_a_url in parallel as an argument? And yes, I know about GNU parallel, I just don't have the privilege to install software where I'm writing this.
推荐答案
下面是你如何能够让你的函数的工作演示:
Here's a demo of how you might be able to get your function to work:
$ f() { echo "[$@]"; }
$ export -f f
$ echo -e "b 1\nc 2\nd 3 4" | xargs -P 0 -n 1 -I{} bash -c f\ \{\}
[b 1]
[d 3 4]
[c 2]
要使得这项工作的关键在于导出
的功能,让庆典
的 xargs的
鱼卵会看到它,并逃脱函数名和逃脱括号之间的空间。您应该能够适应这在您的情况来工作。你需要调整的参数为 -P
和 -n
(或删除),以满足您的需求。
The keys to making this work are to export
the function so the bash
that xargs
spawns will see it and to escape the space between the function name and the escaped braces. You should be able to adapt this to work in your situation. You'll need to adjust the arguments for -P
and -n
(or remove them) to suit your needs.
您也许可以摆脱的grep
和切
。如果你正在使用Bash内建时间
,您可以指定使用 TIMEFORMAT
变量的输出格式。如果您使用GNU 的/ usr / bin / time会
,您可以使用 - 格式
参数。无论这些都将让你放下 -p
也。
You can probably get rid of the grep
and cut
. If you're using the Bash builtin time
, you can specify an output format using the TIMEFORMAT
variable. If you're using GNU /usr/bin/time
, you can use the --format
argument. Either of these will allow you to drop the -p
also.
您可以取代你的 wget的,这部分
命令: 2 - ;&放大器; 1>的/ dev / null的
与 -q
。在任何情况下,你有这些逆转。正确的顺序应该为>的/ dev / null的2 - ;&放大器; 1
You can replace this part of your wget
command: 2>&1 1>/dev/null
with -q
. In any case, you have those reversed. The correct order would be >/dev/null 2>&1
.
这篇关于Shell脚本:使用xargs的执行shell函数平行实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!