One line if statement in bash
You are missing a semicolon and need to use -lt
instead of <
, as others pointed out.
An alternative to the if
statement would be to use the logical and operator &&
:
for (( i=0; i<N-1; i++ )); do
tmp=$(( sorted_array[i+1] - sorted_array[i] ))
[ $tmp -lt $result ] && result=$tmp
done
Each command must be properly terminated, either by a newline or a semi-colon. In this case, you need to separate the assignment of result
from the keyword fi
. Try adding a semi-colon;
for (( i=0; i<N-1; i++ )); do
tmp=$(( sorted_array[i+1] - sorted_array[i] ))
if [ "$tmp" -lt "$result" ]; then result=$tmp; fi
done
Also, you need to use lt
rather than <
, since <
is a redirection operator. (Unless you intend to run a command named $tmp
with input from a file named by the variable $result
)