使用xargs调用多个命令

本文翻译自:Calling multiple commands with xargs

cat a.txt | xargs -I % echo %

In the example above, xargs takes echo % as the command argument. 在上面的示例中,xargs将echo %作为命令参数。 But in some cases, I need multiple commands to process the argument instead of one. 但是在某些情况下,我需要多个命令而不是一个来处理参数。 For example: 例如:

cat a.txt | xargs -I % {command1; command2; ... }

But xargs doesn't accept this form. 但是xargs不接受这种形式。 One solution I know is that I can define a function to wrap the commands, but it's not a pipeline, I don't prefer it. 我知道的一个解决方案是,我可以定义一个包装命令的函数,但这不是管道,我不喜欢它。 Is there another solution? 还有其他解决方案吗?


#1楼

参考:https://stackoom.com/question/tcgV/使用xargs调用多个命令


#2楼

One thing I do is to add to .bashrc/.profile this function: 我要做的一件事是将.bashrc / .profile添加到此函数:

function each() {
    while read line; do
        for f in "$@"; do
            $f $line
        done
    done
}

then you can do things like 然后你可以做类似的事情

... | each command1 command2 "command3 has spaces"

which is less verbose than xargs or -exec. 它不如xargs或-exec冗长。 You could also modify the function to insert the value from the read at an arbitrary location in the commands to each, if you needed that behavior also. 您也可以修改该函数,以将读取值插入命令中的任意位置(如果需要),从而将其插入每个命令。


#3楼

With GNU Parallel you can do: 使用GNU Parallel,您可以:

cat a.txt | parallel 'command1 {}; command2 {}; ...; '

Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1 观看介绍性视频以了解更多信息: https : //www.youtube.com/playlist?list=PL284C9FF2488BC6D1


#4楼

Another possible solution that works for me is something like - 对我有用的另一种可能的解决方案是-

cat a.txt | xargs bash -c 'command1 $@; command2 $@' bash

Note the 'bash' at the end - I assume it is passed as argv[0] to bash. 注意最后的“ bash”-我假设它以argv [0]的形式传递给bash。 Without it in this syntax the first parameter to each command is lost. 如果没有使用此语法,则会丢失每个命令的第一个参数。 It may be any word. 可以是任何单词。

Example: 例:

cat a.txt | xargs -n 5 bash -c 'echo -n `date +%Y%m%d-%H%M%S:` ; echo " data: " $@; echo "data again: " $@' bash

#5楼

You can use 您可以使用

cat file.txt | xargs -i  sh -c 'command {} | command2 {} && command3 {}'

{} = variable for each line on the text file {} =文本文件中每一行的变量


#6楼

My current BKM for this is 我目前的BKM是

... | xargs -n1 -I % perl -e 'system("echo 1 %"); system("echo 2 %");'

It is unfortunate that this uses perl, which is less likely to be installed than bash; 不幸的是,它使用了perl,它比bash不太可能被安装。 but it handles more input that the accepted answer. 但它处理的输入要多于可接受的答案。 (I welcome a ubiquitous version that does not rely on perl.) (我欢迎不依赖perl的无处不在的版本。)

@KeithThompson's suggestion of @KeithThompson的建议

 ... | xargs -I % sh -c 'command1; command2; ...'

is great - unless you have the shell comment character # in your input, in which case part of the first command and all of the second command will be truncated. 太好了-除非您在输入中输入了shell注释字符#,否则第一个命令的一部分和第二个命令的所有部分都会被截断。

Hashes # can be quite common, if the input is derived from a filesystem listing, such as ls or find, and your editor creates temporary files with # in their name. 如果输入源于文件系统列表(例如ls或find),并且您的编辑器创建名称为#的临时文件,则哈希号#很常见。

Example of the problem: 问题示例:

$ bash 1366 $>  /bin/ls | cat
#Makefile#
#README#
Makefile
README

Oops, here is the problem: 糟糕,这是问题所在:

$ bash 1367 $>  ls | xargs -n1 -I % sh -i -c 'echo 1 %; echo 2 %'
1
1
1
1 Makefile
2 Makefile
1 README
2 README

Ahh, that's better: 嗯,这样更好:

$ bash 1368 $>  ls | xargs -n1 -I % perl -e 'system("echo 1 %"); system("echo 2 %");'
1 #Makefile#
2 #Makefile#
1 #README#
2 #README#
1 Makefile
2 Makefile
1 README
2 README
$ bash 1369 $>  
### Xargs命令与Exec的区别 Xargs和exec都是Linux系统中用于将参数传递给其他命令的方式,但在具体实现上有所不同。 #### 工作原理差异 Xargs从标准输入读取数据并将其作为参数传递给指定的命令。这种方式允许更灵活地处理大量参数,因为xargs可以自动调整每次调用命令时所使用的参数数量[^2]。相比之下,`find ... -exec`直接在找到文件后立即执行指定的操作,对于每一个匹配项都会启动一个新的进程来运行命令[^1]。 #### 参数传递方式的不同 当使用xargs时,可以通过设置最大参数长度或限制每批次发送到目标命令中的项目数,从而优化性能表现。而-exec则是针对每个单独的结果去执行相应的操作,这意味着如果查找出来的文件非常多,则可能会创建大量的子进程[^3]。 #### 性能对比 由于xargs能够批量处理多个参数,在某些情况下效率更高;特别是当需要对很多小文件进行相同操作的时候,利用xargs通常可以获得更好的性能提升。然而,在简单场景下两者之间的速度差距可能并不明显。 #### Shell脚本示例 下面给出一段简单的shell脚本来展示如何分别使用这两种方法: ```bash #!/bin/bash # 使用 find 和 xargs 的组合 echo "Using find and xargs:" find /path/to/search -name "*.log" | xargs rm -f # 或者也可以这样写以提高兼容性和安全性 find /path/to/search -name "*.log" -print0 | xargs -0 rm -f # 使用 find 和 exec echo "Using find with -exec:" find /path/to/search -name "*.log" -exec rm -f {} \; ``` 这段代码展示了两种不同的删除特定目录下的日志文件的方法。前者先找出所有的`.log`文件并将它们一次性交给rm命令移除;后者则是每当发现符合条件的日志文件就立刻调用一次rm命令对其进行清理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值