有多个命令作为参数xargs的 [英] xargs with multiple commands as argument

查看:172
本文介绍了有多个命令作为参数xargs的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 猫A.TXT | xargs的-I%回声%

在上面xargs的例子发生回声%作为命令参数。但在某些情况下,我需要多个命令来处理,而不是一个,例如:

 猫A.TXT | xargs的-I%{命令1;命令2; ...}

不过xargs的不接受这种形式。一个解决方案,我知道的是,我可以定义一个函数来包装的命令,但它不是管道,我不preFER它。是否有其他解决办法?


解决方案

 猫A.TXT | xargs的-I%的sh -c'命令1;命令2; ......

请注意,这是一个使用无用猫中。我会写为:

 < A.TXT xargs的-I%的sh -c'命令1;命令2; ......

(是,重定向可以在命令的开头。)

presumably 命令1 和/或命令2 将包含一个或多个字符;否则就不会有太大的指向 -I%选项的xargs

cat a.txt | xargs -I % echo %

In the example above xargs takes echo % as the command argument. But in some cases, I need multiple commands to process instead of one, for example:

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

But xargs doesn't accept this form. One solution I know is that I can define a function to wrap the commands, but it's not pipeline, I don't prefer it. Is there other solution?

解决方案

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

Note that this is a Useless Use Of cat. I'd write it as:

< a.txt xargs -I % sh -c 'command1; command2; ...'

(Yes, the redirection can be at the beginning of the command.)

Presumably command1 and/or command2 will contain one or more % characters; otherwise there wouldn't be much point to the -I % option to xargs.

这篇关于有多个命令作为参数xargs的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆