0

I am trying to run the below code using for loop but i am getting syntax error. Please help.

Input Format: The first line of the input contains an integer N, indicating the number of integers. The next line contains N space-separated integers that form the array A.

read n
sum=0

for (( i=1; i<= "$n" ; i++ ))
do
        read val
        sum ^= $val;
done

echo $sum

Below is the error message

solution.sh: line 4: ((: i<= 1
 : syntax error: invalid arithmetic operator (error token is "
 ")
Kikit
  • 101
  • 4
  • 11
  • 1
    DOS line-endings in your script file? – Etan Reisner Jun 11 '15 at 15:28
  • @EtanReisner Sorry, I did't get what you are asking. – Kikit Jun 11 '15 at 15:29
  • 1
    @User112638726 Using `$` is valid in that context though (despite being unnecessary most of the time) and is not the error. – Etan Reisner Jun 11 '15 at 15:30
  • See the first suggested item in the "Before asking about problematic code" section of the [bash tag info wiki](http://stackoverflow.com/tags/bash/info). – Etan Reisner Jun 11 '15 at 15:31
  • @User112638726 I am get error when I follow you solution.sh: line 4: ((: 1 : syntax error: invalid arithmetic operator (error token is " ") – Kikit Jun 11 '15 at 15:32
  • 1
    @User112638726 Nope. Works just fine. Try it yourself. – Etan Reisner Jun 11 '15 at 15:33
  • @EtanReisner Just checked and neither is an error, but they both still aren't needed (p.s. im dumb). – 123 Jun 11 '15 at 15:34
  • @User112638726 Right. Both "incorrect" in that they aren't necessary or helpful but also not the problem. – Etan Reisner Jun 11 '15 at 15:34
  • As chatraed points out in their answer you also have a spacing problem in the loop body. You **cannot** put spaces around the assignment operator in shell assignments. So `sum ^= $val` is an error and must be written as `((sum^=$val))` but that's not the error being asked about here (the `((...))` is for arithmetic context). – Etan Reisner Jun 11 '15 at 15:37
  • @chepner yep, i've deleted my other comment as after looking it up it was completely wrong :) – 123 Jun 11 '15 at 15:38
  • Further, you'd have to write `((sum^=val))`; it's only valid inside an arithmetic context. – chepner Jun 11 '15 at 15:38
  • 1
    Add `printf "%s" "$n" | hexdump -C` before the while loop; what output does it produce? – chepner Jun 11 '15 at 15:39
  • 1
    @chepner 00000000 31 0d |1.| 00000002 0 – Kikit Jun 11 '15 at 15:48
  • 2
    The `0d` is a carriage return. Your script file contains DOS newlines as suggested by Etan. Run `dos2unix` on your script, or use your favorite text editor to save the file with Unix newlines. – chepner Jun 11 '15 at 15:50
  • @chepner: nice debug technique! – Eugeniu Rosca Jun 11 '15 at 15:53
  • 1
    how can I convert that code in Unix newline?. I am running the code on a online complier. so running as dos2unix solution.sh not possible – Kikit Jun 11 '15 at 15:54
  • @Kikit: check [this](http://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-n-in-bash-script). – Eugeniu Rosca Jun 11 '15 at 15:56
  • 1
    I'm voting to close this question as off-topic because the problem (DOS line endings) is covered in the [bash tag wiki](http://stackoverflow.com/tags/bash/info). – chepner Jun 11 '15 at 15:59

2 Answers2

1

The printf "%s" "$n" | hexdump -C shows that the CR is contained in the input rather than in the script file, so running dos2unix on the script won't help anyway. A simple means to get rid of it is setting IFS=$'\r'.
Then, read val in a loop is unfit to read space-separated integers, since read reads a whole line at a time. But for the task of bitwise exclusive ORing those N space-separated integers we don't need an explicit loop - we can just replace all spaces with the desired operator and evaluate the result.

#!/bin/bash
IFS=$'\r'
read n
read a
((sum = ${a// /^}))
echo $sum
Armali
  • 18,255
  • 14
  • 57
  • 171
0

This will work. However you should insert some helper messages inside:

#!/bin/bash

read n
sum=0

for (( i=1; i<=n; i++ ))
do
        read val
        ((sum^=val))
done
echo $sum
Eugeniu Rosca
  • 5,177
  • 16
  • 45
  • 1
    `^=` is a valid bash arithmetic context assignment operator (though not with the spaces obviously) but this doesn't solve the actual problem the poster is having. – Etan Reisner Jun 11 '15 at 15:36
  • Still getting this error solution.sh: line 4: ((: 1 : syntax error: invalid arithmetic operator (error token is " ") – Kikit Jun 11 '15 at 15:38
  • It look like there is problem with for loop. why it is giving invalid arithmetic operator? – Kikit Jun 11 '15 at 15:39
  • 1
    @Kikit: try `dos2unix solution.sh` and the rerunning my example. Etan Reisner: thanks for the note (corrected). – Eugeniu Rosca Jun 11 '15 at 15:43