File: runeach.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-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 [options...] [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 case "$1" in
  58     -d|--d|-dry|--dry|-dryrun|--dryrun|-dry-run|--dry-run)
  59         dry_run=1
  60         shift
  61     ;;
  62     -q|--q|-quiet|--quiet|-s|--s|-silent|--silent)
  63         quiet=1
  64         shift
  65     ;;
  66 esac
  67 
  68 [ "$1" = "--" ] && shift
  69 
  70 cmd="$@"
  71 sep=' '
  72 if [ -z "${cmd}" ]; then
  73     sep=''
  74 fi
  75 
  76 IFS="$(printf "\n")"
  77 
  78 while read -r line; do
  79     if echo "${line}" | grep -E -q '^ *\r?$'; then
  80         continue
  81     fi
  82 
  83     if [ "${dry_run}" -eq 1 ]; then
  84         printf "%s%s%s\n" "${cmd}" "${sep}" "${line}"
  85         continue
  86     fi
  87 
  88     if [ "${quiet}" -eq 0 ]; then
  89         printf "\e[7m%s%s%s\e[0m\n" "${cmd}" "${sep}" "${line}" >&2
  90     fi
  91 
  92     ${cmd} "${line}"
  93 done