如何在bash中使用mod运算符?

时间:2011-04-16 18:21:47

标签: bash modulo arithmetic-expressions

我正在尝试这样一条线:

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

我想要输出的是:

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

但我实际得到的只是:

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

4 个答案:

答案 0 :(得分:192)

尝试以下方法:

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

$(( ))语法执行arithmetic evaluation内容。

答案 1 :(得分:34)

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

答案 2 :(得分:26)

您必须将数学表达式放在$(())中。

一衬垫:

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

多行:

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

答案 3 :(得分:11)

这可能是偏离主题的。但对于for循环中的wget,你当然可以做到

curl -O http://example.com/search/link[1-600]