How to use mod operator in bash?

主宰稳场 提交于 2019-12-03 00:56:22

问题


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.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

回答1:


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.




回答2:


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



回答3:


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



回答4:


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]


来源:https://stackoverflow.com/questions/5688576/how-to-use-mod-operator-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!