1

How can I use an if statement such as this to compare the element value to a variable. For example i'm going through an array of numbers, and checking the minimum. I'm having syntax problems.

for ((j=0;j<$i;j++)); do
if [ "$array[$j]" -gt $min ]; then
        min=${array[$j]}
fi
done
echo The minimum number is $min
3
  • 2
    Easier to loop over an array with for el in "${array[i]}". Commented Nov 1, 2013 at 3:44
  • Or, in zsh, echo ${${(n)ary}[1]}. Commented Nov 1, 2013 at 3:59
  • @Kevin typo spotted: for el in "${array[@]}" instead! Commented Nov 1, 2013 at 8:07

2 Answers 2

2

Your if statement lacks then clause:

if [ "$array[$j]" -gt $min ]; then
    min=${array[$j]}
fi

Also, in the true spirit of UNIX tools, consider something more elegant:

min_elem=$(echo ${array[@]} | tr ' ' '\n' | sort -n | head -1)
Sign up to request clarification or add additional context in comments.

8 Comments

Error: syntax error near unexpected token `if' I implemented this syntax and still does not work. Any ideas?
What about for? You need ;do as well
pretty clever way to get a minimum element. Although, I'm trying to get used to the loops and if statement comparisons. I care more about learning the syntax of those. Although, soon enough I plan to get more elegant :)
Updated script at the top, and it's still not working. What do you think is incorrect?
I personally don't find your last subshell-pipe-pipe-pipe elegant at all! it's awful. At least you could have written read min_element < <(printf '%s\n' "${array[@]}" | sort -n) to get rid of two unneeded external tools and two unneeded pipes! Oh dear... you thought your thing was elegant...
|
0

Several comments:

  • See this answer to know when and how to protect your variables ;
  • -gt isn't the desired operator for getting the min

Here's a simple corrected example (just Bash without other commands) :

#!/bin/bash

array=("1" "0" "2")
length="${#array[@]}"
min="${array[0]}"

for (( i=1 ; i < "$length"; i++)); do
    [[ ${array[$i]} -le $min ]] && min="${array[$i]}"
done

echo "The minimum number is $min"

EDIT : Test performed after the comment of gniourf_gniourf (see the comments)

[ ~]$ cat test.sh 
#!/bin/bash

array=("1" "0" "2")
m=${array[0]}; for i in "${array[@]:1}"; do ((i<m)) && m=$i; done
echo "The minimum number is $m"
[ ~]$ ./test.sh 
The minimum number is 0
[ ~]$ vi test.sh
[ ~]$ cat test.sh 
#!/bin/bash

array=("1" "0" "2")
m="${array[0]}"; for i in "${array[@]:1}"; do [[ $i -le $m ]] && m="$i"; done
echo "The minimum number is $m"
[ ~]$ ./test.sh 
The minimum number is 0
[ ~]$ 

2 Comments

Thanks but it doesn't totally work with my laptop. I edited my post for better reading results. With a slight modification it works well.
Sorry there was a typo in the previous (I deleted it). Please read: m=${array[0]}; for i in "${array[@]:1}"; do ((i<m)) && m=$i; done and I edited your post to fix it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.