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
camdixon
  • 852
  • 2
  • 18
  • 33

2 Answers2

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)
Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
  • Error: syntax error near unexpected token `if' I implemented this syntax and still does not work. Any ideas? – camdixon Nov 01 '13 at 03:14
  • What about `for`? You need `;do` as well – Alexander L. Belikoff Nov 01 '13 at 03:15
  • 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 :) – camdixon Nov 01 '13 at 03:18
  • Updated script at the top, and it's still not working. What do you think is incorrect? – camdixon Nov 01 '13 at 03:27
  • "$array[$j]" is not correct, but it's not a syntax error. You need the curly braces for it to work. – rici Nov 01 '13 at 03:29
  • Why do you need {}? New to bash :(. Sry for newbness. I normally do C# and c++ – camdixon Nov 01 '13 at 03:30
  • @camdixon: because you do :). That's the way bash syntax works. Without the {}, it's two variable expansions, first $array and then $j, together in a string with []. If `array` is an array, then `$array` is the same as `${array[0]}`. Why? Because that's the way it was written. – rici Nov 01 '13 at 03:31
  • 1
    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_... – gniourf_gniourf Nov 01 '13 at 07:51
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
[ ~]$ 
Community
  • 1
  • 1
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
  • 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. – Idriss Neumann Nov 01 '13 at 09:20
  • 1
    Sorry there was a typo in the previous (I deleted it). Please read: `m=${array[0]}; for i in "${array[@]:1}"; do ((i – gniourf_gniourf Nov 01 '13 at 09:24