NASutils Knowledge BaseUnofficialPolski

Bash · lesson 15 of 30 · Data, functions and the command lineCount with names

declare -A makes an array with names for its values.

Programming on the NAS › Bash in 30 lessons

15-maps.sh Download 15-maps.sh

#!/usr/app/TurboShell/bin/bash
# declare -A makes an array with names instead of numbers
declare -A count=()
while read -r _ _ level _; do
  count[$level]=$((${count[$level]:-0} + 1))
done <nas.log

for level in INFO WARN ERROR DEBUG; do
  echo "$level: ${count[$level]:-none}"
done

Terminal

$ bash 15-maps.sh
INFO: 6
WARN: 3
ERROR: 3
DEBUG: none

Exercise

Count the lines of each day.