Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The following for loop in bash gives an error -

line 42: 1
2
3
4
5 + 1: syntax error in expression (error token is "2
3
4
5 + 1")

line 42 is statement - num1=$[$i1 + 1]

for i1 in `seq 1 5`
    do
    num1=$[$i1 + 1]
        for k1 in `seq $num1 5`
        do
        ky1="${team_two[i1]}_${team_two[k1]}"
        pair_score[$ky1]=$[${pair_score[$ky1]}+1]
        done
    done

What is wrong with the code?

EDIT

I get the following output on debugging

++ seq 1 5
+ for i1 in '`seq 1 5`'
/home/ashwin/bin/calculate_power: line 43: 1
2
3
4
5 + 1: syntax error in expression (error token is "2
3
4
5 + 1")

Again it is the same statement that line 43 - num1=$[$i+1]

share|improve this question

closed as unclear what you're asking by jasonwryan, strugee, Anthon, Gilles, Thomas Nyman Apr 18 '14 at 8:32

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

    
Please pastebin the entire script, rather than expect people to guess what is going on... –  jasonwryan Apr 17 '14 at 20:32
2  
don't put it on Pastebin. what if Pastebin goes down? you should paste the entire script directly into this question. –  strugee Apr 17 '14 at 21:05

2 Answers 2

up vote 3 down vote accepted

Seems like you have some problems with your ${team_two[*]} & ${team_two[k1]} arrays or you've messed with the separation characters in the special variable $IFS. You can turn the Bash debugging on by adding a set -x before you enter the outer for loop. And then disable it afterwards with a set +x, to gain further insights.

set -x
... for loop block ...
set +x

Example

When I run your code through Bash in this manner I get the following output:

$ bash -x for.bash 
++ seq 1 5
+ for i1 in '`seq 1 5`'
+ num1=2
++ seq 2 5
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=1
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=2
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=3
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=4
+ for i1 in '`seq 1 5`'
+ num1=3
++ seq 3 5
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=5
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=6
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=7
+ for i1 in '`seq 1 5`'
+ num1=4
++ seq 4 5
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=8
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=9
+ for i1 in '`seq 1 5`'
+ num1=5
++ seq 5 5
+ for k1 in '`seq $num1 5`'
+ ky1=_
+ pair_score[$ky1]=10
+ for i1 in '`seq 1 5`'
+ num1=6
++ seq 6 5

Edit #1

After further updates it would appear you've reset $IFS. Typically you'll want to save $IFS to an secondary variable prior to reseting it, and the revert it later on back to it's original value.

IFSOLD=$IFS
IFS=,

... do IFS , stuff here ...

IFS=$IFSOLD

IFS

If you try these examples they might help shed additional light on to the impact $IFS can play when used within a script.

Say I have the following variable:

$ var="1,2,3,4,5"

Now let's parse it and print the first variable, $c1:

$ IFS=',' read -r c1 c2 c3 c4 c5 <<< "$var"
$ echo "$c1"
1

However if we changed our $IFS to a space.

$ IFS=' ' read -r c1 c2 c3 c4 c5 <<< "$var"
$ echo "$c1"
1,2,3,4,5

So in the second example, we've configured the read command via $IFS to split on spaces, rather than commas.

share|improve this answer
    
I did the debugging as you told. I have edited the question with the output. –  Ashwin Apr 17 '14 at 8:29
    
If the problem was with the arrays then echo "$num1" should atleast have worked. But even that doesn't work –  Ashwin Apr 17 '14 at 8:37
    
@Ashwin - what version of Bash is this? It looks as if it's trying to add all the elements. 1 2 3 4 5 + 1. –  slm Apr 17 '14 at 13:23
    
Bash version - 4.2.45(1) –  Ashwin Apr 17 '14 at 15:29
    
@Ashwin see updates. –  slm Apr 17 '14 at 17:46

Nothing (fundamentally) wrong with that code, your problem is you've reset "IFS" somewhere else, it's probably empty (so the output of seq is treated as a single token).

Long time since I've seen $[], it's obsolete (and undocumented). Use $(( )).

Instead of seq use for (( )), and instead of adding 1, use let var++, so we have:

for (( i1=1 ; i1<=5; i1++))
    do
        for ((k1=i1+1; k1<=5; k1++))
            do
                ky1="${team_two[i1]}_${team_two[k1]}"
                let pair_score[$ky1]++
            done
    done    

Since you're using a recent bash with associative arrays, may as well use the other nice features too :-)

share|improve this answer
    
you are right about the IFS part. I have set the "IFS=,". That was done because I wanted to split a string into an array separated by comma. What does that have to do with for loop seq? How can I correct it? –  Ashwin Apr 17 '14 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.