A bin folder, holding helpfull scripts and commands
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

eachsubdir 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. #
  3. BASEDIR=`pwd`
  4. usage="Execute a command in each subdirectory:\n\nUsage: $0 [-p | print subdir info] \"command\""
  5. # More safety, by turning some bugs into errors.
  6. # Without `errexit` you don’t need ! and can replace
  7. # PIPESTATUS with a simple $?, but I don’t do that.
  8. set -o errexit -o pipefail -o noclobber -o nounset
  9. # -allow a command to fail with !’s side effect on errexit
  10. # -use return value from ${PIPESTATUS[0]}, because ! hosed $?
  11. ! getopt --test > /dev/null
  12. if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
  13. echo 'I’m sorry, `getopt --test` failed in this environment.'
  14. exit 1
  15. fi
  16. OPTIONS=phe:
  17. LONGOPTS=print-subdir,help,exclude:
  18. # -regarding ! and PIPESTATUS see above
  19. # -temporarily store output to be able to check for errors
  20. # -activate quoting/enhanced mode (e.g. by writing out “--options”)
  21. # -pass arguments only via -- "$@" to separate them correctly
  22. ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
  23. if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
  24. # e.g. return value is 1
  25. # then getopt has complained about wrong arguments to stdout
  26. exit 2
  27. fi
  28. # read getopt’s output this way to handle the quoting right:
  29. eval set -- "$PARSED"
  30. p=n
  31. exc=""
  32. # now enjoy the options in order and nicely split until we see --
  33. while true; do
  34. case "$1" in
  35. -p|--print-subdir)
  36. p=y
  37. shift
  38. ;;
  39. -h|--help)
  40. echo -e "Execute a command in each subdirectory:\n\nUsage: $0 [-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