Skip to content
This repository
Browse code

Updating eachdir to work with aliases/functions.

  • Loading branch information...
commit 6ef96fcb1444e05def2f7a8d0c34a6789716b623 1 parent 6ed9e77
Ben Alman authored October 19, 2012
36  bin/eachdir
... ...
@@ -1,10 +1,12 @@
1 1
 #!/bin/bash
2 2
 
  3
+function _eachdir() {
  4
+
3 5
 if [[ "$1" == "-h" || "$1" == "--help" ]]; then cat <<HELP
4 6
 eachdir
5 7
 http://benalman.com/
6 8
 
7  
-Usage: $(basename "$0") [dirs --] commands
  9
+Usage: eachdir [dirs --] commands
8 10
 
9 11
 Run one or more commands in one or more dirs.
10 12
 
@@ -14,34 +16,37 @@ before it. All remaining args are the command(s) to be executed for each dir.
14 16
 
15 17
 Multiple commands must be specified as a single string argument.
16 18
 
  19
+In bash, aliasing like this allows you to specify aliases/functions:
  20
+  alias eachdir=". eachdir"
  21
+
17 22
 Both of these print the working directory of every subdir of the current dir:
18  
-  $(basename "$0") pwd
19  
-  $(basename "$0") * -- pwd
  23
+  eachdir pwd
  24
+  eachdir * -- pwd
20 25
 
21 26
 Perform a "git pull" inside all subdirs starting with repo-:
22  
-  $(basename "$0") repo-* -- git pull
  27
+  eachdir repo-* -- git pull
23 28
 
24 29
 Perform a few git-related commands inside all subdirs starting with repo-:
25  
-  $(basename "$0") repo-* -- 'git fetch && git merge'
  30
+  eachdir repo-* -- 'git fetch && git merge'
26 31
 
27 32
 Copyright (c) 2012 "Cowboy" Ben Alman
28 33
 Licensed under the MIT license.
29 34
 http://benalman.com/about/license/
30 35
 HELP
31  
-exit; fi
  36
+return; fi
32 37
 
33 38
 if [ ! "$1" ]; then
34 39
   echo 'You must specify one or more commands to run.'
35  
-  exit 1
  40
+  return 1
36 41
 fi
37 42
 
38 43
 # For underlining headers.
39  
-h1="$(tput smul)"
40  
-h2="$(tput rmul)"
  44
+local h1="$(tput smul)"
  45
+local h2="$(tput rmul)"
41 46
 
42 47
 # Store any dirs passed before -- in an array.
43  
-dashes=
44  
-dirs=()
  48
+local dashes d
  49
+local dirs=()
45 50
 for d in "$@"; do
46 51
   if [[ "$d" == "--" ]]; then
47 52
     dashes=1
@@ -54,7 +59,7 @@ done
54 59
 # If -- wasn't specified, default to all subdirs of the current dir.
55 60
 [[ "$dashes" ]] || dirs=(*/)
56 61
 
57  
-nops=()
  62
+local nops=()
58 63
 # Do stuff for each specified dir, in each dir. Non-dirs are ignored.
59 64
 for d in "${dirs[@]}"; do
60 65
   # Skip non-dirs.
@@ -77,3 +82,10 @@ if [[ ${#nops[@]} > 0 ]]; then
77 82
   echo "${h1}no output from${h2}"
78 83
   for d in "${nops[@]}"; do echo "$d"; done
79 84
 fi
  85
+
  86
+}
  87
+
  88
+# By putting the above code inside a function, if this file is sourced (which
  89
+# is required for external aliases/functions to be used as commands), vars
  90
+# can be local and return can be used to exit.
  91
+_eachdir "$@"
3  source/50_file.sh
@@ -32,6 +32,9 @@ alias df="df -h"
32 32
 # Recursively delete `.DS_Store` files
33 33
 alias dsstore="find . -name '*.DS_Store' -type f -ls -delete"
34 34
 
  35
+# Aliasing eachdir like this allows you to use aliases/functions as commands.
  36
+alias eachdir=". eachdir"
  37
+
35 38
 # Create a new directory and enter it
36 39
 function md() {
37 40
   mkdir -p "$@" && cd "$@"

0 notes on commit 6ef96fc

Please sign in to comment.
Something went wrong with that request. Please try again.