I am writing a script that finds the minimum value in a string. The line is passed to me with cat <file> , and then I parse every number inside that line. A string contains only a set of numbers separated by an interval.
This is the code:
echo $FREQUENCIES for freq in $FREQUENCIES do echo "Freq: $freq" if [ -z "$MINFREQ" ] then MINFREQ=$freq echo "Assigning MINFREQ for the first time with $freq" elif [ $MINFREQ -gt $freq ] then MINFREQ=$freq echo "Replacing MINFREQ with $freq" fi done
Here is the result I get:
800000 700000 600000 550000 500000 250000 125000 Freq: 800000 Assigning MINFREQ for the first time with 800000 Freq: 700000 Replacing MINFREQ with 700000 Freq: 600000 Replacing MINFREQ with 600000 Freq: 550000 Replacing MINFREQ with 550000 Freq: 500000 Replacing MINFREQ with 500000 Freq: 250000 Replacing MINFREQ with 250000 Freq: 125000 Replacing MINFREQ with 125000 Freq: : integer expression expected
The problem is that the last line for some reason is empty or contains spaces (I'm not sure why). I tried to check if the variable was set: if [-n "$ freq"], but this test does not seem to work fine, it still passes the if statement for the last line.
Can someone please help me figure out why the last time the loop is executed, $ freq is set to empty or whitespace and how to avoid this?
EDIT:
using od -c feeded with echo "<<$freq>>" 0000000 < < 8 0 0 0 0 0 > > \n 0000013 0000000 < < 7 0 0 0 0 0 > > \n 0000013 0000000 < < 6 0 0 0 0 0 > > \n 0000013 0000000 < < 5 5 0 0 0 0 > > \n 0000013 0000000 < < 5 0 0 0 0 0 > > \n 0000013 0000000 < < 2 5 0 0 0 0 > > \n 0000013 0000000 < < 1 2 5 0 0 0 > > \n 0000013 0000000 < < \r > > \n 0000006
There seems to be an extra \ r (from the file).
Thank you very much!
source share