有没有更好的方法在 bash 中运行命令 N 次? [英] Is there a better way to run a command N times in bash?

查看:61
本文介绍了有没有更好的方法在 bash 中运行命令 N 次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I occasionally run a bash command line like this:

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

连续多次运行 some_command -- 在本例中为 10 次.

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

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

Often some_command is really a chain of commands or a pipeline.

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

Is there a more concise way to do this?

推荐答案

如果你的范围有一个变量,使用 seq,像这样:

If your range has a variable, use seq, like this:

count=10
for i in $(seq $count); do
    command
done

简单地说:

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

这篇关于有没有更好的方法在 bash 中运行命令 N 次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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