有没有更好的方法在bash中运行N次命令?
我偶尔运行一个像这样的bash命令行:
n=0; while [[ $n -lt 10 ]]; do some_command; n=$((n+1)); done  要some_command运行some_command多次 – 在这种情况下是10次。 
  some_command通常是一个命令链或一个pipe道。 
有没有更简洁的方法来做到这一点?
 for run in {1..10} do command done 
 for ((n=0;n<10;n++)); do some_command; done 
另一个简单的方法来破解它:
 seq 20 | xargs -Iz echo "Hi there" 
运行回声20次。
 注意seq 20 | xargs -Iz echo "Hi there z"  seq 20 | xargs -Iz echo "Hi there z"会输出: 
你好1
你好2
…
如果您使用的是zsh shell:
 repeat 10 { echo 'Hello' } 
其中10是命令将被重复的次数。
使用GNU并行你可以这样做:
 parallel some_command ::: {1..1000} 
如果您不想将该号码作为参数,并且一次仅运行一个作业:
 parallel -j1 -N0 some_command ::: {1..1000} 
观看介绍video快速介绍: https : //www.youtube.com/playlist?list=PL284C9FF2488BC6D1
浏览教程( http://www.gnu.org/software/parallel/parallel_tutorial.html )。 你命令爱你。
你的例子的另一种forms:
 n=0; while (( n++ < 10 )); do some_command; done 
xargs和seq将有所帮助
 function __run_times { seq 1 $1| { shift; xargs -i -- "$@"; } } 
风景 :
 abon@abon:~$ __run_times 3 echo hello world hello world hello world hello world 
  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} 
在现代的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 
 这是有道理的,因为xargs命令是多次生成/usr/bin/true命令的单个本机进程,而不是在Bash中解释的for和while循环。 当然这只适用于一个单一的命令; 如果你需要在每个迭代循环中执行多个命令,它将会比传递sh -c 'command1; command2; ...'更快或者更快sh -c 'command1; command2; ...' sh -c 'command1; command2; ...'  sh -c 'command1; command2; ...'到xargs 
  -P1也可以改成,比方说-P8 ,并行产生8个进程,进一步提升速度。 
我不知道GNU为什么如此缓慢。 我会认为这将是可比的xargs。
首先,你可以把它包装在一个函数中:
 function manytimes { n=0 times=$1 shift while [[ $n -lt $times ]]; do $@ n=$((n+1)) done } 
像这样称呼它:
 $ manytimes 3 echo "test" | tr 'e' 'E' tEst tEst tEst 
 for _ in {1..10}; do command; done 
注意下划线而不是使用variables。
在bashconfiguration文件(.bashrc中,我在Mac上)一个简单的function可以很好地工作。
 function xrun() { for ((n=0;n<$1;n++)) do ${*:2} done } 
像这样称呼它。
 $ xrun 3 echo 'Hello world' Hello world Hello world Hello world 
我解决了这个循环, 重复是一个整数,代表循环的数量
 repeat=10 for n in $(seq $repeat); do command1 command2 done 
(bashref)循环构造中提到的替代forms如何?
For循环可能是正确的做法,但这里有一个有趣的select:
 echo -e {1..10}"\n" |xargs -n1 some_command 
如果您需要将迭代编号作为调用的参数,请使用:
 echo -e {1..10}"\n" |xargs -I@ echo now I am running iteration @ 
  编辑:正确地评论说,上面给出的解决scheme将工作顺利,只需简单的命令运行(没有pipe道等)。 你总是可以用sh -c来做更复杂的东西,但是不值得。 
我使用的另一种方法通常是以下function:
 rep() { s=$1;shift;e=$1;shift; for x in `seq $s $e`; do c=${@//@/$x};sh -c "$c"; done;} 
现在你可以把它叫做:
 rep 3 10 echo iteration @ 
 前两个数字给出了范围。  @将被转换为迭代编号。 现在你可以使用这个pipe道了: 
 rep 1 10 "ls R@/|wc -l" 
给你目录R1 .. R10中的文件数量。
如果您可以定期执行此操作,则可以运行以下命令,无限期地每隔1秒运行一次。 您可以放置其他自定义检查来运行n次。
 watch -n 1 some_command 
 如果您希望对变更进行视觉确认,请在ls命令之前追加--differences 。 
根据OSX手册页,也有
– 累积选项使突出显示“粘性”,呈现所有已经改变的位置的运行显示。 -t或–no-title选项会closures显示顶部显示间隔,命令和当前时间的标题以及以下空白行。
Linux / Unix手册页可以在这里find
 所有现有的答案似乎都需要bash ,并且不能使用标准的BSD UNIX /bin/sh (例如OpenBSD上的ksh )。 
下面的代码应该适用于任何BSD:
 $ echo {1..4} {1..4} $ seq 4 sh: seq: not found $ for i in $(jot 4); do echo e$i; done e1 e2 e3 e4 $ 
还有一个答案:在空参数上使用参数扩展:
 # calls curl 4 times curl -s -w "\n" -X GET "http:{,,,}//www.google.com" 
在Centos 7和MacOS上testing。
脚本文件
 bash-3.2$ cat test.sh #!/bin/bash echo "The argument is arg: $1" for ((n=0;n<$1;n++)); do echo "Hi" done 
和下面的输出
 bash-3.2$ ./test.sh 3 The argument is arg: 3 Hi Hi Hi bash-3.2$ 
有点天真,但这是我通常记得我的头顶:
 for i in 1 2 3; do some commands done 
 非常类似于@ joe-koberg的答案。 如果你需要多次重复,他会更好,因为在过去的几年里我没有使用bash 。 我的意思是不是至less有脚本。