27

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
  • 34,093
  • 11
  • 73
  • 117
pkruk
  • 739
  • 1
  • 5
  • 23
  • For those of us who missed your previous question, can you provide more information for this one. What var are you trying to change? – Andrew Brock May 01 '12 at 18:48
  • 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

40

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

Mat
  • 195,986
  • 40
  • 382
  • 396
  • ahhhh Thank's now i understand this. Thank's :) – pkruk May 01 '12 at 20:01
  • 2
    4th problem: unmotivated backticks in the middle of the script, not explainable by beginner mistake. :) – user unknown May 01 '12 at 20:21
  • 1
    @userunknown: I assumed those were the result of a fight with the markdown editor, not present in the code :) – Mat May 01 '12 at 20:27
  • 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
  • 1
    is there a better answer for this now? – Hossein Kalbasi Dec 07 '19 at 05:05