1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #!/bin/bash
-
-
- BASEDIR=`pwd`
-
-
-
-
-
- set -o errexit -o pipefail -o noclobber -o nounset
-
-
-
- ! getopt --test > /dev/null
- if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
- echo 'I’m sorry, `getopt --test` failed in this environment.'
- exit 1
- fi
-
- OPTIONS=phe:
- LONGOPTS=print-subdir,help,exclude:
-
-
-
-
-
- ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
- if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
-
-
- exit 2
- fi
-
- eval set -- "$PARSED"
-
- p=n
- exc=""
-
- while true; do
- case "$1" in
- -p|--print-subdir)
- p=y
- shift
- ;;
- -h|--help)
- echo -e "NAME\n\tineach - executes a command in each subdirectory"
- echo -e "SYNOPSIS\n\tineach [-p, --print-subdir] [-e, --exclude dir1,dir2,...,dirn] \"command\""
- exit 0
- shift
- ;;
- -e|--exclude)
- exc="$2"
- shift 2
- ;;
- --)
- shift
- break
- ;;
- *)
- echo "Programming error"
- exit 3
- ;;
- esac
- done
-
-
- if [[ $# -ne 1 ]]; then
- echo "$0: A single command is required."
- exit 4
- fi
-
-
-
- for subdir in `find $BASEDIR -maxdepth 1 -type d`
- do
- bn=`basename $subdir`
- if [[ $subdir != $BASEDIR ]] && [[ ",$exc," != *",$bn,"* ]]; then
- cd $subdir
- if [[ $p = "y" ]]; then
- echo -en "\n\n\033[1;33m╔"
- for ((i=1;i<=$((${#bn} + 2));i++)); do echo -n "═"; done ; echo "╗"
- echo "║ $bn ║"
- echo -n "╚"
- for ((i=1;i<=$((${#bn} + 2));i++)); do echo -n "═"; done ; echo "╝"
- echo -e "\033[00m"
- fi
- eval $1
- fi
- done
|