#!/bin/zsh if [ "$#" -lt "2" ] ; then << EOF >&2 Usage: list LISTFILE 'COMMAND' Pass each line from LISTFILE into COMMAND, from which it's available by the variable \$LISTLINE. If COMMAND returns code 0, remove the line from LISTFILE in-place, otherwise leave it be. This script expects that lines in LISTFILE are not modified or removed since it started. Appending new lines with >> is ok, but they will not be processed until the next time it runs. EOF exit 1 fi LISTFILE="$1" shift # Verify list file exists if ! [ -f "$LISTFILE" ]; then <<< "No such file \"$LISTFILE\"" >&2 exit 1 fi # Remove blank lines sed -ir "/^$/d" "$LISTFILE" LISTLINEN=1 < "$LISTFILE" while read LISTLINE; do # Run command on line if eval "$@"; then # If ok, remove processed line sed -i "${LISTLINEN}d" "$LISTFILE" else # Only increment line number if not removed, # otherwise lines will be skipped (( LISTLINEN ++ )) fi done