bash 如何在每个 xargs 命令之间休眠 1 秒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15153240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:42:19  来源:igfitidea点击:

How to sleep for 1 second between each xargs command?

linuxbashshellxargs

提问by WDan

For example, if I execute

例如,如果我执行

ps aux | awk '{print }' | xargs -I {} echo {}

I want to let the shell sleep for 1 second between each echo.

我想让 shell 在每个echo.

How can I change my shell command?

如何更改我的 shell 命令?

回答by kamituel

You can use following syntax:

您可以使用以下语法:

ps aux | awk '{print }' | xargs -I % sh -c '{ echo %; sleep 1; }'

Be careful with spaces and semicolons though. After every command between brackets, semicolon is required (even after last one).

但是要小心空格和分号。在括号之间的每个命令之后,都需要分号(甚至在最后一个之后)。

回答by Basile Starynkevitch

Replace echoby some shell script named sleepechocontaining

替换echo为一些名为sleepecho包含的shell 脚本

 #!/bin/sh
 sleep 1
 echo $*

回答by chepner

If your awksupports it:

如果您awk支持:

ps aux | awk '{ system("sleep 1"); print  }' | xargs -I {} echo {}q

or skip awkand xargsaltogether

或跳过awkxargs

ps aux | while read -r user rest;
    echo $user
    sleep 1;
done