eeval 309 B

123456789101112131415161718192021
  1. #!/usr/bin/env bash
  2. # echo and eval
  3. a=
  4. while getopts a OPT; do
  5. case "$OPT" in
  6. a)
  7. # Append to file instead of overwriting.
  8. a=-a
  9. ;;
  10. ?)
  11. exit 2
  12. ;;
  13. esac
  14. done
  15. shift "$(($OPTIND - 1))"
  16. cmd="$1"
  17. outfile="${2:-/dev/null}"
  18. echo "$cmd" | tee $a "$outfile"
  19. eval "$cmd"
  20. exit "$?"