Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Question: what does this line do? Can someone please explain?

 if [ $((${array[$i]}+1)) -ne ${array[$(($i + 1))]} ] ;then foo; fi
share|improve this question
    
same as if ((array[i] + 1 != array[i+1])); then foo; fi (as long as $i and array[i+1] contain decimal integers). – Stéphane Chazelas 2 days ago
    
It produces the value at position [$i] in $array, adds one to it - inside an arithmetic context, then returns (via command substitution) that as the LHS operand to the -ne conditional operator, then evaluates the RHS operand, $(($i + 1)) is variable $i + 1 then the result of this arithmetic operation is used as the key/position in the array ${array[]} - if the LHS and the RHS results are -ne "not equal` the then block runs – the_velour_fog 2 days ago
    
And also the same as if [[ array[i]+1 -ne array[i+1] ]] ;then foo; fi as both sides of an integer operator are processed as arithmetic expansions. In math: test for A[i]+1 = A[i+1]. Or, in words: is the next array element the consecutive numeric value of the present one? – BinaryZebra 2 days ago
up vote 6 down vote accepted

[ ... -ne ... ] - test for inequality
$(( ... + 1)) - add one, arithmetic expansion
${array[$i]} - reference to element $i of an array variable

Or in other words the test is A[i] + 1 != A[i + 1]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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