Q:

Homework about using a file for input in bash

So basically i have been assigned this homework in which i have to make a bash script that gets its input from a file of numbers, and exports the bigger number, the smaller number their sum and their average.
I have tried to find a way to do this in a simple way, he told us to do it in a fashion of ./bashscript < numbers.txt, however i cant seem to know how to search for that neither can i find a clear simple solution to the problem, i guess that finding the bigger and smaller number as well as the average should be simple once i get some stuff going but now i am at a loss...
if i were to input a .txt file that had: 
3
4
5
3
2
1
It should give me the greater : 5
the smaller: 1
the amount:6
and an average:3

A:

This is not too tricky and shows how nicely you can use AWK inside of a shell script.
The BEGIN section is being executed before awk processes the file line by line.
The END section is being executed after awk processed the whole input file.
Important is to assign min and max with the 1st number found in the file. Otherwise:
a) min will i.e. always be zero if you have only numbers >0 in the file
b) to have a valid number for max even if the file has only one number/line.
$_ represents the actual line which AWK is processing.
#! /bin/sh

# script.sh - awk script embedded into shell script

awk 'BEGIN {
        count=0
} {
        if (NR == 1) {
                min=$_
                max=$_
        }
        if ($_ < min) {
                min=$_
        }
        if ($_ > max) {
                max=$_
        }
        sum+=$_
        count++
} END {
        printf ("min=%d\n", min)
        printf ("max=%d\n", max)
        printf ("avg=%d\n", sum/count)
}'

Usage:
$ cat datafile
2
1
4
5
3

$ chmod +x script.sh
$ ./script.sh < datafile
min=1
max=5
avg=3

