Bash · lesson 14 of 30 · Data, functions and the command lineKeep many values
An array keeps its values apart even with spaces; mapfile reads a file into one.
14-arrays.sh Download 14-arrays.sh
#!/usr/app/TurboShell/bin/bash
temps=(38 39 53 44)
temps+=(41)
echo "${#temps[@]} values, the first is ${temps[0]}"
sum=0
hot=${temps[0]}
for t in "${temps[@]}"; do
sum=$((sum + t))
((t > hot)) && hot=$t
done
echo "average $((sum / ${#temps[@]})), highest $hot"
for i in "${!temps[@]}"; do
echo "disk $((i + 1)): ${temps[i]}"
done
mapfile -t lines <words.txt
echo "the file has ${#lines[@]} lines, the last is: ${lines[-1]}"
Terminal
$ bash 14-arrays.sh
5 values, the first is 38
average 43, highest 53
disk 1: 38
disk 2: 39
disk 3: 53
disk 4: 44
disk 5: 41
the file has 3 lines, the last is: Disks fail, so keep a copy of the files.Exercise
Find the lowest temperature too.