A bin folder, holding helpfull scripts and commands
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. #
  3. BASEDIR=`pwd`
  4. # More safety, by turning some bugs into errors.
  5. # Without `errexit` you don’t need ! and can replace
  6. # PIPESTATUS with a simple $?, but I don’t do that.
  7. set -o errexit -o pipefail -o noclobber -o nounset
  8. # -allow a command to fail with !’s side effect on errexit
  9. # -use return value from ${PIPESTATUS[0]}, because ! hosed $?
  10. ! getopt --test > /dev/null
  11. if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
  12. echo 'I’m sorry, `getopt --test` failed in this environment.'
  13. exit 1
  14. fi
  15. OPTIONS=phe:
  16. LONGOPTS=print-subdir,help,exclude:
  17. # -regarding ! and PIPESTATUS see above
  18. # -temporarily store output to be able to check for errors
  19. # -activate quoting/enhanced mode (e.g. by writing out “--options”)
  20. # -pass arguments only via -- "$@" to separate them correctly
  21. ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
  22. if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
  23. # e.g. return value is 1
  24. # then getopt has complained about wrong arguments to stdout
  25. exit 2
  26. fi
  27. # read getopt’s output this way to handle the quoting right:
  28. eval set -- "$PARSED"
  29. p=n
  30. exc=""
  31. # now enjoy the options in order and nicely split until we see --
  32. while true; do
  33. case "$1" in
  34. -p|--print-subdir)
  35. p=y
  36. shift
  37. ;;
  38. -h|--help)
  39. echo -e "NAME\n\tineach - executes a command in each subdirectory"
  40. echo -e "SYNOPSIS\n\tineach [-p, --print-subdir] [-e, --exclude dir1,dir2,...,dirn] \"command\""
  41. exit 0
  42. shift
  43. ;;
  44. -e|--exclude)
  45. exc="$2"
  46. shift 2
  47. ;;
  48. --)
  49. shift
  50. break
  51. ;;
  52. *)
  53. echo "Programming error"
  54. exit 3
  55. ;;
  56. esac
  57. done
  58. # handle non-option arguments
  59. if [[ $# -ne 1 ]]; then
  60. echo "$0: A single command is required."
  61. exit 4
  62. fi
  63. for subdir in `find $BASEDIR -maxdepth 1 -type d`
  64. do
  65. bn=`basename $subdir`
  66. if [[ $subdir != $BASEDIR ]] && [[ ",$exc," != *",$bn,"* ]]; then
  67. cd $subdir
  68. if [[ $p = "y" ]]; then
  69. echo -en "\n\n\033[1;33m╔"
  70. for ((i=1;i<=$((${#bn} + 2));i++)); do echo -n "═"; done ; echo "╗"
  71. echo "║ $bn ║"
  72. echo -n "╚"
  73. for ((i=1;i<=$((${#bn} + 2));i++)); do echo -n "═"; done ; echo "╝"
  74. echo -e "\033[00m"
  75. fi
  76. eval $1
  77. fi
  78. done