File: runeach.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2025 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 run nothing, and show would-be completed commands to stdout 35 # -dry run nothing, and show would-be completed commands to stdout 36 # -dryrun run nothing, and show would-be completed commands to stdout 37 # -dry-run run nothing, and show would-be completed commands to stdout 38 # 39 # -h show this help message 40 # -help show this help message 41 # 42 # -q just run the commands, without showing them on stderr 43 # -quiet just run the commands, without showing them on stderr 44 # -s just run the commands, without showing them on stderr 45 # -silent just run the commands, without showing them on stderr 46 47 48 case "$1" in 49 -h|--h|-help|--help) 50 awk '/^# +runeach /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 51 exit 0 52 ;; 53 esac 54 55 quiet=0 56 dry_run=0 57 58 case "$1" in 59 -d|--d|-dry|--dry|-dryrun|--dryrun|-dry-run|--dry-run) 60 dry_run=1 61 shift 62 ;; 63 64 -q|--q|-quiet|--quiet|-s|--s|-silent|--silent) 65 quiet=1 66 shift 67 ;; 68 esac 69 70 [ "$1" = '--' ] && shift 71 72 cmd="$1" 73 if [ $# -eq 0 ] || [ -z "${cmd}" ]; then 74 awk '/^# +runeach /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 75 exit 1 76 fi 77 [ $# -gt 0 ] && shift 78 79 if [ $# -eq 0 ]; then 80 pre="${cmd}" 81 else 82 pre="${cmd} $@" 83 fi 84 85 IFS="$(printf "\n")" 86 87 while read -r line; do 88 if echo "${line}" | grep -E -q '^ *\r?$'; then 89 continue 90 fi 91 92 if [ "${dry_run}" -eq 1 ]; then 93 printf "%s %s\n" "${pre}" "${line}" 94 continue 95 fi 96 97 if [ "${quiet}" -eq 0 ]; then 98 printf "\e[7m%s %s\e[0m\n" "${pre}" "${line}" >&2 99 fi 100 101 "${cmd}" "$@" "${line}" 102 done