#!/bin/sh # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # runeach [options...] [command...] [args...] # # # RUN EACH shows each command about to run on stderr, then runs each command # using stdin lines to add a final argument to the arguments given explicitly # # The options are, available both in single and double-dash versions # # -d run nothing, and show would-be completed commands to stdout # -dry run nothing, and show would-be completed commands to stdout # -dryrun run nothing, and show would-be completed commands to stdout # -dry-run run nothing, and show would-be completed commands to stdout # # -h show this help message # -help show this help message # # -q just run the commands, without showing them on stderr # -quiet just run the commands, without showing them on stderr # -s just run the commands, without showing them on stderr # -silent just run the commands, without showing them on stderr case "$1" in -h|--h|-help|--help) awk '/^# +runeach /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac quiet=0 dry_run=0 case "$1" in -d|--d|-dry|--dry|-dryrun|--dryrun|-dry-run|--dry-run) dry_run=1 shift ;; -q|--q|-quiet|--quiet|-s|--s|-silent|--silent) quiet=1 shift ;; esac [ "$1" = "--" ] && shift cmd="$@" sep=' ' if [ -z "${cmd}" ]; then sep='' fi IFS="$(printf "\n")" while read -r line; do if echo "${line}" | grep -E -q '^ *\r?$'; then continue fi if [ "${dry_run}" -eq 1 ]; then printf "%s%s%s\n" "${cmd}" "${sep}" "${line}" continue fi if [ "${quiet}" -eq 0 ]; then printf "\e[7m%s%s%s\e[0m\n" "${cmd}" "${sep}" "${line}" >&2 fi ${cmd} "${line}" done