Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mod operator in bash?

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 
like image 863
Eric Avatar asked Apr 16 '11 18:04

Eric


People also ask

How do you mod in bash?

To use modulo in the shell, we have to utilize the “expr” command to evaluate its value. So, we have consecutively added three “expr” commands to find out the modulo of two integer values each time by using the “%” operator between them and got three remainder values.

How does mod () work?

The modulo operation (abbreviated “mod”, or “%” in many programming languages) is the remainder when dividing. For example, “5 mod 3 = 2” which means 2 is the remainder when you divide 5 by 3.

How do you use modulus operator?

In most programming languages, modulo is indicated with a percent sign. For example, "4 mod 2" or "4%2" returns 0, because 2 divides into 4 perfectly, without a remainder. "5%2", however, returns 1 because 1 is the remainder of 5 divided by 2 (2 divides into 5 2 times, with 1 left over).

What does $() mean in bash?

Dollar sign $ (Variable) The dollar sign before the thing in parenthesis usually refers to a variable. This means that this command is either passing an argument to that variable from a bash script or is getting the value of that variable for something.


1 Answers

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.

like image 164
Mark Longair Avatar answered Sep 22 '22 15:09

Mark Longair