按设定的数量在 bash 循环中增加

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

Increment in bash loop by set amount

bash

提问by cianius

I know how to do a loop in bash that increases by one each time, but say I have a range 1 to 773 and I want to output a range from a loop so that I get two variables in each iteration. The first will be 1 and the second will be say 19. In the second iteration the first would be 20 and the second 39.

我知道如何在 bash 中做一个每次增加一个的循环,但是假设我有一个范围 1 到 773 并且我想从循环中输出一个范围,以便我在每次迭代中得到两个变量。第一个是 1,第二个是 19。在第二次迭代中,第一个是 20,第二个是 39。

Ive been playing with something like:

我一直在玩类似的东西:

for start in {1..773}
do    
start=$(($start+20))
end=$(($start+20))
echo $start ## 
echo $end
done

Desired loop outcome:

期望的循环结果:

 1. $start = 1 and $end = 19
 2. $start = 20 and $end = 39
 3. $start = 40 and $end = 59 
etc

But it's not right. I want to output these two variables to a series of scripts to make R run faster, so if non bash (eg awk) solutions are easier then that's cool too if a simple > will send it the file.

但这是不对的。我想将这两个变量输出到一系列脚本以使 R 运行得更快,所以如果非 bash(例如 awk)解决方案更容易,那么如果一个简单的 > 将文件发送给它,那也很酷。

Thanks!

谢谢!

回答by mnagel

your requirement are not perfectly clear, but you are re-using variable names.

您的要求不是很清楚,但您正在重复使用变量名。

if i do this:

如果我这样做:

for index in {1..773}
do    
  start=$(($index+20))
  end=$(($start+20))
  echo $start ## 
  echo $end
done

i get something that resembles your desired result. observe how i renamed the loop variable from start to index.

我得到的东西类似于你想要的结果。观察我如何从开始到索引重命名循环变量。

PS: if you want to change the step size (a.k.a. "increment") in your loop, simply do it like this:

PS:如果您想更改循环中的步长(又名“增量”),只需这样做:

#!/bin/bash
for i in {0..10..2}
  do
     echo "Welcome $i times"
done

This will increment in steps of 2, you would want to use a 20 here. That would give you 1, 21, 41, ...as value. See http://www.cyberciti.biz/faq/bash-for-loop/for more details.

这将以 2 的步长递增,您可能希望在此处使用 20。那会给你1, 21, 41, ...作为价值。有关更多详细信息,请参阅http://www.cyberciti.biz/faq/bash-for-loop/

回答by thefourtheye

If you want to print the ranges within 773, you can do like this

如果你想打印 773 以内的范围,你可以这样做

#!env bash
start=1
end=19
for counter in {1..773}
do
   echo $counter. "$start = " $start " and $end = " $end
   if [[ $start -eq 1 ]];
   then
      start=0
   fi
   start=$(($start+20))
   end=$(($end+20))
   if [[ $end -ge 773 ]];
   then
      break
   fi
done

Output

输出

1. $start =  1  and $end =  19
2. $start =  20  and $end =  39
3. $start =  40  and $end =  59
4. $start =  60  and $end =  79
5. $start =  80  and $end =  99
6. $start =  100  and $end =  119
7. $start =  120  and $end =  139
8. $start =  140  and $end =  159
9. $start =  160  and $end =  179
10. $start =  180  and $end =  199
11. $start =  200  and $end =  219
12. $start =  220  and $end =  239
13. $start =  240  and $end =  259
14. $start =  260  and $end =  279
15. $start =  280  and $end =  299
16. $start =  300  and $end =  319
17. $start =  320  and $end =  339
18. $start =  340  and $end =  359
19. $start =  360  and $end =  379
20. $start =  380  and $end =  399
21. $start =  400  and $end =  419
22. $start =  420  and $end =  439
23. $start =  440  and $end =  459
24. $start =  460  and $end =  479
25. $start =  480  and $end =  499
26. $start =  500  and $end =  519
27. $start =  520  and $end =  539
28. $start =  540  and $end =  559
29. $start =  560  and $end =  579
30. $start =  580  and $end =  599
31. $start =  600  and $end =  619
32. $start =  620  and $end =  639
33. $start =  640  and $end =  659
34. $start =  660  and $end =  679
35. $start =  680  and $end =  699
36. $start =  700  and $end =  719
37. $start =  720  and $end =  739
38. $start =  740  and $end =  759

回答by ztik

You could use seqcommand

你可以使用seq命令

for start in `seq 1 20 700`
do    
  echo $start $(($start+19))
done

The usage of seqis:

的用法seq是:

$ seq --help
Usage: seq [OPTION]... LAST
  or:  seq [OPTION]... FIRST LAST
  or:  seq [OPTION]... FIRST INCREMENT LAST
Print numbers from FIRST to LAST, in steps of INCREMENT.

回答by konsolebox

This is one way to do it with consistent outputs:

这是具有一致输出的一种方法:

for ((i = 1, start = 1, end = 19; i <= 773; ++i, start += 20, end += 20)); do
    echo "$i. $start = $start and $end = $end"
done

Output:

输出:

1. $start = 1 and $end = 19
2. $start = 21 and $end = 39
3. $start = 41 and $end = 59
4. $start = 61 and $end = 79

Or

或者

for ((i = 1, start = 1, end = 19; i <= 773; ++i, start += 20, end += 20)); do
    echo "$i. $start = $start and $end = $end"
done

Output:

输出:

1. $start = 1 and $end = 20
2. $start = 21 and $end = 40
3. $start = 41 and $end = 60
4. $start = 61 and $end = 80

Another:

其他:

for ((i = 1, start = 0, end = 19; i <= 773; ++i, start += 20, end += 20)); do
    echo "$i. $start = $start and $end = $end"
done

Output:

输出:

1. $start = 0 and $end = 19
2. $start = 20 and $end = 39
3. $start = 40 and $end = 59
4. $start = 60 and $end = 79

That way you can have two variables.

这样你就可以有两个变量。

回答by anubhava

Unless I'm missing something you can simply do:

除非我遗漏了一些东西,否则你可以简单地做:

for ((s=0,e=19; e<773; s+=20,e+=20)); do
    echo $s "-" $e
done

OUTPUT:

输出:

0 - 19
20 - 39
40 - 59
60 - 79
80 - 99
100 - 119
120 - 139
140 - 159
160 - 179
180 - 199
200 - 219
220 - 239
240 - 259
260 - 279
280 - 299
300 - 319
320 - 339
340 - 359
360 - 379
380 - 399
400 - 419
420 - 439
440 - 459
460 - 479
480 - 499
500 - 519
520 - 539
540 - 559
560 - 579
580 - 599
600 - 619
620 - 639
640 - 659
660 - 679
680 - 699
700 - 719
720 - 739
740 - 759

回答by glenn Hymanman

Nobody has suggested a whileloop

没有人建议while循环

start=0 step=20 end=$((step - 1))
while (( end < 773 )); do
    echo "$start - $end"
    (( start += step, end += step ))
done

Produces

生产

0 - 19
20 - 39
40 - 59
...
720 - 739
740 - 759

Of course, the while loop can be written as a for loop:

当然,while 循环可以写成 for 循环:

for ((start=0, step=20, end=step-1; end < 773; start+=step, end+=step)); do ...

回答by pgl

Here's one way to do it:

这是一种方法:

step=20
last=773

for ((i = 0; i <= $last; i += $step))
do
    start=$i

    end=$(($i + (($step - 1))))

    if (($end > $last))
    then
        end=$last
    fi

    echo "$start: $start"
    echo "$end: $end"
done

It's basically just a simple for loop.

它基本上只是一个简单的 for 循环。