new scripts for acting on hierarchies
[feisty_meow.git] / scripts / files / act_on_tree.sh
1 #!/bin/bash
2
3 # act_on_tree: performs a command on a hierarchy of directories.
4 #
5 # a handy way to run a command across a set of folders.
6
7 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
8
9 #echo command line in act_on_tree is: $*
10
11 if [ -z "$APP_NAME" ]; then
12   APP_NAME="$(basename $0 .sh)"
13 fi
14
15 function print_instructions_and_exit()
16 {
17   echo "
18 $APP_NAME [-d directory] [-f subfolder] action1 [action2...]
19
20 This script runs an action command on each of the folders that live under the
21 current directory (going one level down from this directory, not recursively).
22 The single action command to run is built from the pieces action1, action2,
23 and so on that are provided on the command line.
24
25 For example, this command:
26   $APP_NAME git branch
27 will show the branch information on each project under the current directory.
28
29 You can specify an alternate directory to use with the '-d' flag, e.g.:
30   $APP_NAME -d ~/turnip_codes/ ant clean build
31
32 You can provide a sub-folder name with -f that must exist and which the script
33 changes the directory to before the command is run.  This supports hierarchies
34 where the action must take place below the children of the -d directory.
35   $APP_NAME -f avenger5 rgetem
36
37 The flags and their parameters must precede the action1... arguments.
38
39 "
40   exit 1
41 }
42
43 changes=true
44
45 while [ $changes == true ]; do
46   changes=nope
47
48   # check if they gave us a special directory flag.
49   if [ "$1" == "-d" ]; then
50     shift  # toss the -d.
51     # grab the directory they want to actually use.
52     seekdir="$1"; shift
53     # check for more flags.
54     changes=true
55   fi
56
57   # check if they gave us a subfolder name flag.
58   if [ "$1" == "-f" ]; then
59     shift  # toss the -f.
60     # get their preferred subfolder.
61     subfoldername="$1"; shift
62     # check for more flags.
63     changes=true
64   fi
65 done
66
67 # check that there are some remaining parms for the action pieces.
68 if [ -z "$*" ]; then
69   print_instructions_and_exit
70 fi
71
72 # plug in our defaults.
73 if [ -z "$seekdir" ]; then
74   seekdir="."
75 fi
76 if [ -z "$subfoldername" ]; then
77   subfoldername="."
78 fi
79
80 sep 28
81
82 pushd $seekdir &>/dev/null
83
84 for i in *; do
85   if [ -d "$i" -a -d "$i/$subfoldername" ]; then
86     pushd "$i/$subfoldername" &>/dev/null
87     echo "[in '$i' running action: $*]"
88     $* 
89     sep 28
90     popd &>/dev/null
91   fi
92 done
93
94 popd &>/dev/null
95