bin/ineach
2021-03-08 18:23:46 +01:00

90 lines
2.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#
BASEDIR=`pwd`
# More safety, by turning some bugs into errors.
# Without `errexit` you dont need ! and can replace
# PIPESTATUS with a simple $?, but I dont do that.
set -o errexit -o pipefail -o noclobber -o nounset
# -allow a command to fail with !s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo 'Im sorry, `getopt --test` failed in this environment.'
exit 1
fi
OPTIONS=phe:
LONGOPTS=print-subdir,help,exclude:
# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopts output this way to handle the quoting right:
eval set -- "$PARSED"
p=n
exc=""
# now enjoy the options in order and nicely split until we see --
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
# handle non-option arguments
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