Skip to content

Commit

Permalink
Updating eachdir to work with aliases/functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy committed Oct 19, 2012
1 parent 6ed9e77 commit 6ef96fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
36 changes: 24 additions & 12 deletions bin/eachdir
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/bin/bash

function _eachdir() {

if [[ "$1" == "-h" || "$1" == "--help" ]]; then cat <<HELP
eachdir
http://benalman.com/
Usage: $(basename "$0") [dirs --] commands
Usage: eachdir [dirs --] commands
Run one or more commands in one or more dirs.
Expand All @@ -14,34 +16,37 @@ before it. All remaining args are the command(s) to be executed for each dir.
Multiple commands must be specified as a single string argument.
In bash, aliasing like this allows you to specify aliases/functions:
alias eachdir=". eachdir"
Both of these print the working directory of every subdir of the current dir:
$(basename "$0") pwd
$(basename "$0") * -- pwd
eachdir pwd
eachdir * -- pwd
Perform a "git pull" inside all subdirs starting with repo-:
$(basename "$0") repo-* -- git pull
eachdir repo-* -- git pull
Perform a few git-related commands inside all subdirs starting with repo-:
$(basename "$0") repo-* -- 'git fetch && git merge'
eachdir repo-* -- 'git fetch && git merge'
Copyright (c) 2012 "Cowboy" Ben Alman
Licensed under the MIT license.
http://benalman.com/about/license/
HELP
exit; fi
return; fi

if [ ! "$1" ]; then
echo 'You must specify one or more commands to run.'
exit 1
return 1
fi

# For underlining headers.
h1="$(tput smul)"
h2="$(tput rmul)"
local h1="$(tput smul)"
local h2="$(tput rmul)"

# Store any dirs passed before -- in an array.
dashes=
dirs=()
local dashes d
local dirs=()
for d in "$@"; do
if [[ "$d" == "--" ]]; then
dashes=1
Expand All @@ -54,7 +59,7 @@ done
# If -- wasn't specified, default to all subdirs of the current dir.
[[ "$dashes" ]] || dirs=(*/)

nops=()
local nops=()
# Do stuff for each specified dir, in each dir. Non-dirs are ignored.
for d in "${dirs[@]}"; do
# Skip non-dirs.
Expand All @@ -77,3 +82,10 @@ if [[ ${#nops[@]} > 0 ]]; then
echo "${h1}no output from${h2}"
for d in "${nops[@]}"; do echo "$d"; done
fi

}

# By putting the above code inside a function, if this file is sourced (which
# is required for external aliases/functions to be used as commands), vars
# can be local and return can be used to exit.
_eachdir "$@"
3 changes: 3 additions & 0 deletions source/50_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ alias df="df -h"
# Recursively delete `.DS_Store` files
alias dsstore="find . -name '*.DS_Store' -type f -ls -delete"

# Aliasing eachdir like this allows you to use aliases/functions as commands.
alias eachdir=". eachdir"

# Create a new directory and enter it
function md() {
mkdir -p "$@" && cd "$@"
Expand Down

0 comments on commit 6ef96fc

Please sign in to comment.