How to use mod operator in bash?

后端 未结 4 746
悲哀的现实
悲哀的现实 2020-12-22 23:05

I\'m trying a line like this:

for i in {1..600}; do wget http://example.com/search/link $i % 5; done;

What I\'m trying to get as output is:

相关标签:
4条回答
  • 2020-12-22 23:40

    You must put your mathematical expressions inside $(( )).

    One-liner:

    for i in {1..600}; do wget http://example.com/search/link$(($i % 5)); done;
    

    Multiple lines:

    for i in {1..600}; do
        wget http://example.com/search/link$(($i % 5))
    done
    
    0 讨论(0)
  • 2020-12-22 23:47
    for i in {1..600}
    do
        n=$(($i%5))
        wget http://example.com/search/link$n
    done
    
    0 讨论(0)
  • 2020-12-22 23:55

    This might be off-topic. But for the wget in for loop, you can certainly do

    curl -O http://example.com/search/link[1-600]
    
    0 讨论(0)
  • 2020-12-23 00:03

    Try the following:

     for i in {1..600}; do echo wget http://example.com/search/link$(($i % 5)); done
    

    The $(( )) syntax does an arithmetic evaluation of the contents.

    0 讨论(0)
提交回复
热议问题