使用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 $>  
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
xargs 命令可以将标准输入中的一行或多行文本转换成命令行参数,然后将这些参数传递给其他命令。因此,如果你想要执行多个命令,可以通过 xargs多个命令的参数传递给一个 shell 子进程,从而实现执行多个命令的效果。 以下是一个示例,假设你想要查找当前目录下所有的 .txt 文件,并对每个文件执行两个命令:cat 和 wc -l。你可以使用下面的命令: ``` find . -name "*.txt" | xargs -I {} sh -c 'cat {} | wc -l' ``` 这个命令首先使用 find 命令查找所有的 .txt 文件,然后将它们的路径传递给 xargs 命令xargs 命令使用 -I 选项指定替换字符串为 {},然后将每个文件的路径作为参数传递给 sh 命令。sh 命令使用 cat 命令读取文件内容并将其传递给 wc -l 命令,以计算文件中的行数。 你也可以通过在 xargs 命令行中使用多个命令来执行多个命令。以下是一个示例,假设你想要通过 ping 命令测试多个主机的可达性,并在每个主机上执行 ls 命令: ``` echo "www.baidu.com" "www.google.com" | xargs -I {} sh -c 'ping -c 3 {} && ssh user@{} ls' ``` 这个命令首先使用 echo 命令输出要测试的主机名,并通过管道将它们传递给 xargs 命令xargs 命令使用 -I 选项指定替换字符串为 {},然后将每个主机名作为参数传递给 sh 命令。sh 命令使用 ping 命令测试主机的可达性,如果主机可达,则使用 ssh 命令连接到主机并执行 ls 命令

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值