如何在 xargs 中使用替换? [英] How to use substitution in xargs?

查看:57
本文介绍了如何在 xargs 中使用替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是

  1. 查找所有带有 .txt 扩展名的文件
  2. cp 到 .dat 文件

它可以这样做:

for f in `find .-type f -name "*.txt"`;做 cp $f ${f%.txt}.dat;完毕

我想用 xargs 做到这一点,我已经试过了:

查找.-type f -name "*.txt" |xargs -i cp {} ${{}%.txt}.dat

我会出现这样的错误:

坏替换

关于这个,我有以下问题:

  1. 如何正确进行替换?
  2. 我很好奇xargs会在for循环一件一件的事情时并行做事吗?

解决方案

  1. 如何正确进行替换?

您不能以您尝试的方式使用替换,因为 {} 不是 bash 变量(只是 xargs 语法的一部分),因此 bash 无法对其进行替换.

更好的方法是创建一个完整的 bash 命令并将其作为参数提供给 xargs(例如 xargs -0 -i bash -c 'echo cp "$1" "${1%.txt}.dat"' - '{}' - 这样你可以进行 bash 替换.

<块引用>

  1. 我很好奇 xargs 会在 for 循环一件一件地做事情时并行做事吗?

是的,for 循环会依次思考,但默认情况下 xargs 总是会.但是,您可以使用 xargs-P 选项来并行化它,来自 xargs 手册页:

<块引用>

 -P max-procs, --max-procs=max-procs一次最多运行 max-procs 进程;默认值为 1.如果 max-procs 为 0,xargs 将一次运行尽可能多的进程.使用 -n 选项或 -L 选项用 -P;否则很有可能只执行一名执行官.当 xargs 运行时,您可以将其进程发送到

SIGUSR1 信号增加命令的数量同时运行,或 SIGUSR2 以减少数量.您不能将其增加到实现定义的限制(即用 --show-limits 显示).您不能取消将其折叠到 1 以下. xargs 从不终止其命令;当被要求减少时,它只是等待一个以上的存在命令在启动另一个之前终止.

请注意,正确管理对共享资源的并行访问取决于被调用进程.例如,如果

其中不止一个尝试打印到标准输出,输出将以不确定的顺序生成(并且很可能会混淆),除非这些过程在某些方面进行协作防止这种情况的方法.使用某种锁定方案是防止此类问题的一种方法.一般来说,使用锁定方案将有助于确保正确的输出,但降低性能.如果你不想忍受性能差异,只需安排每个进程产生一个单独的输出文件(或否则使用单独的资源).

What I want to do is

  1. find all file with .txt extension
  2. cp them to .dat file

it could do like this:

for f in `find . -type f -name "*.txt"`; do cp $f ${f%.txt}.dat; done

I want to do this with xargs, I have tried this:

find . -type f -name "*.txt" | xargs -i cp {} ${{}%.txt}.dat

I go error like this:

bad substitution

About this, I have these questions:

  1. how to do the substitution rightly?
  2. I am curious about that xargs will do things parallel when for loop do things one by one?

解决方案

  1. How to do the substitution rightly?

You cannot use substitution in the way you are trying to do because {} is not a bash variable (only part of xargs syntax), therefore bash cannot do substitution on it.

A better way to it would be to create a full bash command and provide it as and argument to xargs (e.g. xargs -0 -i bash -c 'echo cp "$1" "${1%.txt}.dat"' - '{}' - this way you can do bash substitution).

  1. I am curious about that xargs will do things parallel when for loop do things one by one?

Yes, for loop will do thinks sequently but by default xargs always will. However, you can use -P option of xargs to parallelize it, from xargs man pages:

   -P max-procs, --max-procs=max-procs
          Run up to max-procs processes at a time; the default is 1.  If max-procs is 0, xargs will run as many processes as possible at a time.  Use the -n option or the -L  option
          with  -P;  otherwise  chances are that only one exec will be done.  While xargs is running, you can send its process a

SIGUSR1 signal to increase the number of commands to run simultaneously, or a SIGUSR2 to decrease the number. You cannot increase it above an implementation-defined limit (which is shown with --show-limits). You cannot de‐ crease it below 1. xargs never terminates its commands; when asked to decrease, it merely waits for more than one existing command to terminate before starting another.

Please  note that it is up to the called processes to properly manage parallel access to shared resources.  For example, if

more than one of them tries to print to stdout, the ouptut will be produced in an indeterminate order (and very likely mixed up) unless the processes collaborate in some way to prevent this. Using some kind of locking scheme is one way to prevent such problems. In general, using a locking scheme will help ensure correct output but reduce performance. If you don't want to tolerate the performance difference, simply arrange for each process to produce a separate output file (or otherwise use separate resources).

这篇关于如何在 xargs 中使用替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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