本文介绍了xargs仅使用内置函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加快数据库的处理速度.我向xargs迁移.但是我被严重卡住了.如果xargs调用的命令不是内置命令,则无法向xargs传递参数列表.我不知道原因.这是我的代码:

I am trying to speed up the processing of a database. I migrated towards xargs. But I'm seriously stuck. Piping a list of arguments to xargs does not work if the command invoked by xargs isn't a built in. I can't figure out why. Here is my code:

#!/bin/bash

list='foo
bar'

test(){
    echo "$1"
}

echo "$list" | tr '\012' '\000' | xargs -0 -n1 -I '{}' 'test' {}

所以根本没有输出.测试功能永远不会执行.但是,如果我将"xargs"命令中的"test"替换为"echo"或"printf",则效果很好.

So there is no output at all. And test function never gets executed. But if I replace "test" in the "xargs" command with "echo" or "printf" it works fine.

推荐答案

xargs将可执行文件作为参数(包括自定义脚本)而不是环境中定义的函数.

xargs takes an executable as an argument (including custom scripts) rather than a function defined in the environment.

将代码移至脚本或使用 xargs 将参数传递给外部命令.

Either move your code to a script or use xargs to pass arguments to an external command.

这篇关于xargs仅使用内置函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 18:26