有没有更好的方法在 bash 中运行命令 N 次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3737740/
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-09 19:40:37  来源:igfitidea点击:

Is there a better way to run a command N times in bash?

bashloops

提问by bstpierre

I occasionally run a bash command line like this:

我偶尔会像这样运行 bash 命令行:

n=0; while [[ $n -lt 10 ]]; do some_command; n=$((n+1)); done

To run some_commanda number of times in a row -- 10 times in this case.

some_command连续运行多次——在本例中为 10 次。

Often some_commandis really a chain of commands or a pipeline.

通常some_command实际上是一个命令链或一个管道。

Is there a more concise way to do this?

有没有更简洁的方法来做到这一点?

回答by Joe Koberg

for run in {1..10}
do
  command
done

Or as a one-liner for those that want to copy and paste easily:

或者作为那些想要轻松复制和粘贴的人的单行:

for run in {1..10}; do command; done

回答by BatchyX

Using a constant:

使用常量:

for ((n=0;n<10;n++)); do some_command; done

Using a variable (can include math expressions):

使用变量(可以包括数学表达式):

x=10; for ((n=0; n < (x / 2); n++)); do some_command; done

回答by mitnk

Another simple way to hack it:

另一种简单的破解方法:

seq 20 | xargs -Iz echo "Hi there"

run echo 20 times.

运行 echo 20 次。



Notice that seq 20 | xargs -Iz echo "Hi there z"would output:

请注意,seq 20 | xargs -Iz echo "Hi there z"将输出:

Hi there 1
Hi there 2
...

你好 1
你好 2
...

回答by Wilson Silva

If you're using the zsh shell:

如果您使用的是 zsh shell:

repeat 10 { echo 'Hello' }

Where 10 is the number of times the command will be repeated.

其中 10 是命令重复的次数。

回答by Ole Tange

Using GNU Parallel you can do:

使用 GNU Parallel 您可以执行以下操作:

parallel some_command ::: {1..1000}

If you do not want the number as argument and only run a single job at a time:

如果您不想将数字作为参数并且一次只运行一个作业:

parallel -j1 -N0 some_command ::: {1..1000}

Watch the intro video for a quick introduction: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

观看介绍视频以进行快速介绍:https: //www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial (http://www.gnu.org/software/parallel/parallel_tutorial.html). You command line with love you for it.

完成教程 ( http://www.gnu.org/software/parallel/parallel_tutorial.html)。你爱你的命令行。

回答by steel

A simple function in the bash config file (~/.bashrcoften) could work well.

bash 配置文件中的一个简单函数(~/.bashrc通常)可以很好地工作。

function runx() {
  for ((n=0;n<;n++))
    do ${*:2}
  done
}

Call it like this.

这样称呼。

$ runx 3 echo 'Hello world'
Hello world
Hello world
Hello world

回答by James Scriven

xargsis fast:

xargs

#!/usr/bin/bash
echo "while loop:"
n=0; time while (( n++ < 10000 )); do /usr/bin/true ; done

echo -e "\nfor loop:"
time for ((n=0;n<10000;n++)); do /usr/bin/true ; done

echo -e "\nseq,xargs:"
time seq 10000 | xargs -I{} -P1 -n1 /usr/bin/true

echo -e "\nyes,xargs:"
time yes x | head -n10000 |  xargs -I{} -P1 -n1 /usr/bin/true

echo -e "\nparallel:"
time parallel --will-cite -j1 -N0 /usr/bin/true ::: {1..10000}

On a modern 64-bit Linux, gives:

在现代 64 位 Linux 上,给出:

while loop:

real    0m2.282s
user    0m0.177s
sys     0m0.413s

for loop:

real    0m2.559s
user    0m0.393s
sys     0m0.500s

seq,xargs:

real    0m1.728s
user    0m0.013s
sys     0m0.217s

yes,xargs:

real    0m1.723s
user    0m0.013s
sys     0m0.223s

parallel:

real    0m26.271s
user    0m4.943s
sys     0m3.533s

This makes sense, as the xargscommand is a single native process that spawns the /usr/bin/truecommand multiple time, instead of the forand whileloops that are all interpreted in Bash. Of course this only works for a single command; if you need to do multiple commands in each iteration the loop, it will be just as fast, or maybe faster, than passing sh -c 'command1; command2; ...'to xargs

这是有道理的,因为该xargs命令是一个单独的本地进程,它/usr/bin/true多次生成命令,而不是在 Bash 中全部解释的forwhile循环。当然,这只适用于单个命令;如果您需要在每次迭代循环中执行多个命令,它将与传递sh -c 'command1; command2; ...'给 xargs一样快,或者可能更快

The -P1could also be changed to, say, -P8to spawn 8 processes in parallel to get another big boost in speed.

-P1也可以改变,比方说,-P8产卵并行8个流程,以获得速度的另一大助力。

I don't know why GNU parallel is so slow. I would have thought it would be comparable to xargs.

我不知道为什么 GNU 并行这么慢。我原以为它可以与 xargs 相媲美。

回答by Paused until further notice.

Another form of your example:

您示例的另一种形式:

n=0; while (( n++ < 10 )); do some_command; done

回答by firejox

xargs and seq will help

xargs 和 seq 会有所帮助

function __run_times { seq 1 | { shift; xargs -i -- "$@"; } }

the view :

风景 :

abon@abon:~$ __run_times 3  echo hello world
hello world
hello world
hello world

回答by bta

For one, you can wrap it up in a function:

一方面,您可以将其封装在一个函数中:

function manytimes {
    n=0
    times=
    shift
    while [[ $n -lt $times ]]; do
        $@
        n=$((n+1))
    done
}

Call it like:

像这样称呼它:

$ manytimes 3 echo "test" | tr 'e' 'E'
tEst
tEst
tEst