#!/bin/sh
#
# fnl - run a command and save its output and error to temporary files
# usage: fnl command arg1 ...
#
# runs the command specifies in its arguments, writing any stdout and stderr to
# separate temporary files created with mktd(1d), and then runs wc(1) over them
# to show their statistics and full paths.

# check we have at least one argument
if [ "$#" -eq 0 ]; then
  printf >&2 'fnl: Command needed\n'
  exit 2
fi

# create a temporary directory; note that we *don't* clean it up on exit
dir=$(mktd fnl) || exit

# run the command; keep its exit status; wrap the command in braces so that the
# out files are always opened even if the command is not found or otherwise
# can't be run; some BSD shells require this, I forget which ones
{ "$@"; } >"${dir}"/stdout 2>"${dir}"/stderr
ret="$?"

# run wc(1) on each of the files
wc -- "${dir}"/*

# exit with the wrapped command's exit status
exit "${ret}"
