2

I've something like this:

declare -r constant1=0
declare -r constant2=0

for xx in 1 2 3 4 5 6 7 8 9 10
do  
    constant1=$((constant1 + 1))
    for yy in 8 7 3 9 3 9 2 8 4 4
    do
        constant2=$((constant2 + 1))
        if [ $constant1 == $constant2 ]; then
            something here bla bla
        fi
    done
done

But it's giving me an error, namely:

line 6: constant1: readonly variable

How do I go about fixing this?

Basically, I want the " do something here bla bla" to run whenever constant1 = constant2. that is, [xx,yy] = [1,8],[2,7], etc

7
  • why have you changed your original question?
    – heemayl
    Mar 8, 2015 at 21:21
  • 1
    Take a look at help declare and option -r.
    – Cyrus
    Mar 8, 2015 at 21:22
  • @heemayl, I changed because I add the "do" after incrementing the constant, which is not what I want, I think.
    – iamatrain
    Mar 8, 2015 at 21:23
  • 2
    -1 You have at least two syntax errors, and your variable names are not consistent with the error you give.
    – muru
    Mar 8, 2015 at 21:25
  • 1
    The answer is in @Cyrus's first comment, check help declare
    – heemayl
    Mar 8, 2015 at 21:36

1 Answer 1

1

If you want to modify constant1 and constant2, you shouldn't declare them as read-only (that's what declare -r does). And if you want to operate of pairs of variables, you should consider using arrays:

array1=(1 2 3 4 5 6 7 8 9 10)
array2=(8 7 3 9 3 9 2 8 4 4)

for i in ${!array1[@]}
do
    echo "${array1[i]}" "${array2[i]}"
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .