0

I'm trying to find the remainder of dividing two numbers, when read from a file.

The input file looks like this:

3 
1 2
100 200
10 40

Expected output:

1
100
10

I'm running the following code:

read T
for ((i=1;i<=$T; i++))
do
read A
read B
echo $((A % B))
done

However, this gives an error like the following:

line 6: 1 2: syntax error in expression (error token is "2")

Why doesn't this work for the question at hand?

1
  • Next time, don't report a CodeChef-specific error ("NZEC"), but actually run this on your own machine, and report the actual underlying bash error. Indeed, if you edited the question to focus on the immediate issue and remove any/all reference to CodeChef, that would do a great deal of good. Commented Jul 14, 2015 at 23:41

1 Answer 1

1
read A
read B

...reads two separate lines into A and B. Thus, your A and B will each be a string with two different numbers, so trying to do math with them will fail.

The syntax error is a big hint: It's trying to use 1 2 -- your first line with two columns -- as a single number, but this isn't a valid number for use in a math expression.

If you want to read two columns of the same line, that would instead be:

read A B
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, you saved me hours of time on this. Newbie to Shell scripting so was quite hard to see that mistake... thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.