One line if statement in bash -
i've never programed in bash... yet i'm trying solve problem anchievement in game (codingame.com)
i have following code:
for (( i=0; i<n-1; i++ )); tmp=$(( sorted_array[i+1] - sorted_array[i] )); if [ $tmp < $result ]; result=$tmp fi done
and error:
/tmp/answer.sh: line 42: syntax error near unexpected token `done'at answer.sh. on line 42 /tmp/answer.sh: line 42: `done' @ answer.sh. on line 42
i want compare adjacent values of array , store minimun diference between them... cant figure how if statement in bash
each command must terminated, either newline or semi-colon. in case, need separate assignment of result
keyword fi
. try adding semi-colon;
for (( i=0; i<n-1; i++ )); tmp=$(( sorted_array[i+1] - sorted_array[i] )) if [ $tmp -lt $result ]; result=$tmp; fi done
also, need use lt
rather <
, since <
redirection operator. (unless intend run command named $tmp
input file named variable $result
)
Comments
Post a Comment