File: runeach.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # runeach [option...] [command...] [args...] 27 # 28 # 29 # RUN EACH shows each command about to run on stderr, then runs each command 30 # using stdin lines to add a final argument to the arguments given explicitly 31 # 32 # The options are, available both in single and double-dash versions 33 # 34 # -d, -dry, -dryrun, -dry-run 35 # run nothing, and show would-be completed commands to stdout 36 # 37 # -h, -help 38 # show this help message 39 # 40 # -q, -quiet, -s, -silent 41 # just run the commands, without showing them on stderr 42 43 44 case "$1" in 45 -h|--h|-help|--help) 46 awk '/^# +runeach /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 47 exit 0 48 ;; 49 esac 50 51 quiet=0 52 dry_run=0 53 54 case "$1" in 55 -d|--d|-dry|--dry|-dryrun|--dryrun|-dry-run|--dry-run) 56 dry_run=1 57 shift 58 ;; 59 60 -q|--q|-quiet|--quiet|-s|--s|-silent|--silent) 61 quiet=1 62 shift 63 ;; 64 esac 65 66 [ "$1" = '--' ] && shift 67 68 cmd="$1" 69 if [ $# -eq 0 ] || [ -z "${cmd}" ]; then 70 awk '/^# +runeach /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 71 exit 1 72 fi 73 [ $# -gt 0 ] && shift 74 75 if [ $# -eq 0 ]; then 76 pre="${cmd}" 77 else 78 pre="${cmd} $*" 79 fi 80 81 IFS="$(printf "\n")" 82 83 while read -r line; do 84 if echo "${line}" | grep -E -q '^ *\r?$'; then 85 continue 86 fi 87 88 if [ "${dry_run}" -eq 1 ]; then 89 printf "%s %s\n" "${pre}" "${line}" 90 continue 91 fi 92 93 if [ "${quiet}" -eq 0 ]; then 94 printf "\e[7m%s %s\e[0m\n" "${pre}" "${line}" >&2 95 fi 96 97 "${cmd}" "$@" "${line}" 98 done