Skip to content

Instantly share code, notes, and snippets.

@pvalena
Created April 1, 2019 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvalena/89746f18a328de2f5dad11bf4a386fe5 to your computer and use it in GitHub Desktop.
Save pvalena/89746f18a328de2f5dad11bf4a386fe5 to your computer and use it in GitHub Desktop.
Run arbitrary commands on multiple folders or prefixed (interactive)
#!/bin/bash
#
# Run arbitrary commands on multiple folders or prefixed (interactive)
#
# ./icom [options] [command]
# -c clear before every command
# -d debug output
# -f [dirs] list folders (args for input for `ls -d`)
#
# Examples:
# Prefix every command with `oc` (openshift executable)
# ./icom oc
#
# Run comands in folders matching wildcard
# ./icom *-container
#
# Issues:
# [command] does not work well in combinatin with `-f`
#
myd="`pwd`"
C=
X=
runcx () {
bash -c "$C$@$X"
echo
}
[[ "$1" == "-c" ]] && { CLEAR="$1" ; shift ; }
[[ "$1" == "-d" ]] && { C="${C}set -x;" ; shift ; }
[[ "$1" == "-f" ]] && { shift ; FOLDERS="`bash -c "ls -d $1 | xargs readlink -e"`" ; shift ; } || FOLDERS=
[[ "$1" ]] && C="$C$@ "
while IFS="" read -r -e -d $'\n' -p ">>> $C" X; do
[[ "$X" ]] || break
history -s "$X"
[[ -n "$CLEAR" ]] && { clear ; echo ">>> $C$X" ; echo ; }
[[ -n "$FOLDERS" ]] && {
while read f; do
cd "$f" || exit 1
runcx "echo -n '> ';pwd;"
cd "$myd" || exit 1
done <<< "$FOLDERS"
:
} || {
runcx
}
done
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment