#!/bin/sh -eu
#
# Usage: each-partition EXPRESSION [ARGUMENT]...
#
# Evaluates EXPRESSION (which may use "$1" to refer to each ARGUMENT in turn)
# for each ARGUMENT, printing it to either (1) stdout if the evaluation was
# successful or (2) stderr if the evaluation returned a nonzero exit status.
#
# ---
# source: https://github.com/sunaku/home/blob/master/bin/each-partition

expression="$1"
shift

while test $# -gt 0; do
  if eval "${expression}" >/dev/null 2>&1; then
    echo "$1"
  else
    echo "$1" >&2
  fi
  shift
done
