#!/bin/sh
#
# last - "last" implementation for Linux
# from networking equipment vendor OS's (JunOS, Cisco IOS, Nokia TiMOS, etc)
# dependencies: isnum
#
# ---
# source: https://github.com/mitchweaver/bin

usage() {
  >&2 cat <<"EOF"
Print last number of lines from piped output

Equivalent to tail -n [num].
Defaults to 5 lines.

Usage: <command> | last [num]
EOF
}

case "$#" in
  0 | 1) ;;
  *)
    usage
    exit 1
    ;;
esac

NUM=
if [ -n "$1" ]; then
  case "$1" in
    -h | --help)
      usage
      exit 0
      ;;
    *)
      isnum "$1" \
        || usage \
        && exit 0
      NUM="$1"
      ;;
  esac
else
  NUM=5
fi
tail -n "${NUM}"
