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 IFS="$(printf "\n")" 69 70 # with no command/args given, just run lines from the stdin 71 if [ $# -eq 0 ]; then 72 while read -r line; do 73 # ignore empty lines 74 if echo "${line}" | grep -E -q '^ *\r?$'; then 75 continue 76 fi 77 78 # ignore commented lines 79 if echo "${line}" | grep -E -q '^ *#'; then 80 continue 81 fi 82 83 if [ "${dry_run}" -eq 1 ]; then 84 printf "%s\n" "${line}" 85 continue 86 fi 87 88 if [ "${quiet}" -eq 0 ]; then 89 printf "\e[7m%-80s\e[0m\n" "${line}" >&2 90 fi 91 92 sh -c ${line} 93 done 94 95 exit 0 96 fi 97 98 cmd="$1" 99 shift 100 101 if [ $# -eq 0 ]; then 102 pre="${cmd}" 103 else 104 pre="$(echo "${cmd}" $@)" 105 fi 106 107 while read -r line; do 108 # ignore empty lines 109 if echo "${line}" | grep -E -q '^ *\r?$'; then 110 continue 111 fi 112 113 # ignore commented lines 114 if echo "${line}" | grep -E -q '^ *#'; then 115 continue 116 fi 117 118 if [ "${dry_run}" -eq 1 ]; then 119 printf "%s %s\n" "${pre}" "${line}" 120 continue 121 fi 122 123 if [ "${quiet}" -eq 0 ]; then 124 printf "\e[7m%-80s\e[0m\n" "${pre} ${line}" >&2 125 fi 126 127 "${cmd}" "$@" ${line} 128 done