开发者

How to use mod operator in bash?

开发者 https://www.devze.com 2023-02-25 19:48 出处:网络
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:

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:

wget http://example.com/search/link0
wget http://example开发者_Go百科.com/search/link1
wget http://example.com/search/link2
wget http://example.com/search/link3
wget http://example.com/search/link4
wget http://example.com/search/link0

But what I'm actually getting is just:

    wget http://example.com/search/link


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.


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


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


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]


Math in bash: how to use all bash operators, and arithmetic expansion, in bash

Of the 346k visitors to this question thus far, I'd be willing to bet 344.9k of them just want the title of this question answered

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号