32

How do I change this var ?

max=0;
min=20000000;
cat |while read
do
    read a
    if [[ $a -gt $max ]]
    then
        max=a`
    fi
    `if [[ $a -lt $min ]]
    then
        min=a
    fi
done
echo $max 
echo $min

My min and max are still the same, 0 and 2000000. Can anybody help me with this ? I have no idea.

user unknown
  • 35,537
  • 11
  • 75
  • 121
pkruk
  • 809
  • 1
  • 7
  • 24
  • 3
    also `max=a` should be `max=$a`, same with `min=a` should be `min=$a` – Andrew Brock May 01 '12 at 18:49
  • What should be `min` if every values are greater than 20000000 ? I guess that `min` and `max` should be your first value... – Luc M May 01 '12 at 20:25
  • Possible duplicate of [A variable modified inside a while loop is not remembered](https://stackoverflow.com/questions/16854280/a-variable-modified-inside-a-while-loop-is-not-remembered) – melpomene Jan 18 '18 at 02:33

1 Answers1

43

The (main) problem with your script is that setting min and max happens in a subshell, not your main shell. So the changes aren't visible after the pipeline is done.

Another one is that you're calling read twice - this might be intended if you want to skip every other line, but that's a bit unusual.

The last one is that min=a sets min to a, literally. You want to set it to $a.

Using process substitution to get rid of the first problem, removing the (possibly) un-necessary second read, and fixing the assignments, your code should look like:

max=0
min=20000000
while read a
do
    if [[ $a -gt $max ]]
    then
        max=$a
    fi
    if [[ $a -lt $min ]]
    then
        min=$a
    fi
done < <(cat)    # careful with the syntax
echo $max 
echo $min

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 2
    4th problem: unmotivated backticks in the middle of the script, not explainable by beginner mistake. :) – user unknown May 01 '12 at 20:21
  • In Bash (and Ksh and Zsh), I recommend this form: `if (( a > max ))`. Also, if `cat` is not a placeholder for something else, then the whole redirection and process substitution can be eliminated since the script will read from `stdin` by default anyway. – Dennis Williamson May 01 '12 at 21:28