I have this script which supposed to read file with numbers in each line, and show the largest number, the smallest one and the sum. During the loop variables change their values but after they return their primary value and I can't fix it.
#!/bin/bash
if [ ! $# -eq 1 ]; then
echo "Invalid number of arguments"
elif [ ! -e $1 ]; then
echo "File doesn't exist"
elif [ ! -s $1 ]; then
echo "File is empty"
else
min=$(head -1 $1)
max=$(head -1 $1)
sum=0
(while read i; do
(( sum+=i ))
if [ $min -gt $i ]; then
min=$i
elif [ $max -lt $i ]; then
max=$i
fi
done
)<$1
fi
echo $min $max $sum