Can someone please help me with a script that takes a list of integers and returns a response with the average, total, maximum and minimum values?
I need to be able to pass parameters when I run the script e.g:
./script.sh 1 2 3 4 5
I have an awk script that does the average, total and maximum sorted, I just need to add a bit so that it will read the arguments - so when I run ./script.sh 1 2 3 4 it returns:
Average: 3
Min: 1
Max: 5
Total: 16
Any help would be appreciated.
This is what I have already:
#!/bin/bash
awk 'NR == 1 { max=$1; min=$1; sum=0 }
{ if ($1>max) max=$1; if ($1<min) min=$1; sum+=$1;}
END {printf "Min: %d\nMax: %d\nAverage: %d\nSum %0.0f\n", min, max, sum/NR, sum}'
Thanks in advance!
Like this?
printf '%s\n' "$@" | awk ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With