-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolders.sh
executable file
·214 lines (185 loc) · 4.54 KB
/
folders.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
#
# MIT License
#
# Copyright (c) 2017 Maciej Chałapuk
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
PRG=$0
usage() {
echo "Usage: $PRG [options...] <command> [arguments...]" >&2
echo "">&2
echo " Commands:" >&2
echo " list - lists added folders" >&2
echo " add [folders...] - adds specified folders to the list" >&2
echo " del [folders...] - deletes specified folders from the list" >&2
echo " run <command-line> - runs specified command on all added folders" >&2
echo " usage - shows this message" >&2
echo "" >&2
echo " Options:" >&2
echo " --clean (-c) - displays only stdout and stderr from child processes">&2
echo "" >&2
}
CURSOR=1
declare -A OPTS
OPTS=(
[-c]="CLEAN_OUTPUT"
[--clean]="CLEAN_OUTPUT"
)
CLEAN_OUTPUT=false
for OPT in "${@:$CURSOR}"
do
if ! grep -q '^-.*$' <<< "$OPT"
then
break
fi
VAR=${OPTS[$OPT]}
if [ -z "$VAR" ]
then
echo "`red "unrecognized option: $OPT"`" >&2
usage
exit 1
fi
eval $VAR=true
CURSOR=$[$CURSOR + 1]
done
eval CMD=\$$CURSOR
CURSOR=$[$CURSOR + 1]
# Arguments need to be properly escaped and quoted
# as they will be passed into another shell.
ARGS=""
for ARG in "${@:$CURSOR}"
do
ARG="${ARG//\\/\\\\}"
if grep -q ' ' <<< "$ARG"
then
ARGS="$ARGS \"${ARG//\"/\\\"}\""
else
ARGS="$ARGS $ARG"
fi
done
# Group name is derived from program name
# (symlinking the script creates another folder group)
GROUP_NAME=${PRG##*/}
FOLDERS_DB=~/.$GROUP_NAME
test -f $FOLDERS_DB || touch $FOLDERS_DB
# When running in terminal we would like to have colored messages.
if tty -s
then
cyan() {
echo -e "\e[36m$@\e[0m"
}
green() {
echo -e "\e[32m$@\e[0m"
}
red() {
echo -e "\e[31m$@\e[0m"
}
yellow() {
echo -e "\e[33m$@\e[0m"
}
magenta() {
echo -e "\e[35m$@\e[0m"
}
else
cyan() {
echo $@
}
green() {
echo $@
}
red() {
echo $@
}
yellow() {
echo $@
}
magenta() {
echo $@
}
fi
# <COMMANDS>
list() {
cat $FOLDERS_DB | while read LINE; do cyan $LINE; done
}
add() {
ls -1d $@ | while read FOLDER
do
ADDED=`readlink -f $FOLDER`
if egrep -q "^$ADDED$" $FOLDERS_DB
then
red "already added: $ADDED" >&2
continue
fi
$CLEAN_OUTPUT || echo `yellow "$GROUP_NAME"`: `green "+"``cyan "$ADDED"`
echo "$ADDED" >> $FOLDERS_DB
done
}
del() {
ls -1d $@ | while read ENTRY
do
DELETED=`readlink -f "$ENTRY"`
if ! egrep -q "^$DELETED$" $FOLDERS_DB
then
red "not found: $DELETED" >&2
continue
fi
mv $FOLDERS_DB $FOLDERS_DB.old
cat $FOLDERS_DB.old | while read FOLDER
do
ABSOLUTE_PATH=`readlink -f "$FOLDER"`
if [[ "$ABSOLUTE_PATH" == "$DELETED" ]]
then
$CLEAN_OUTPUT || echo `yellow "$GROUP_NAME"`: `red "-"``cyan "$DELETED"`
else
echo "$ABSOLUTE_PATH" >> $FOLDERS_DB
fi
done
done
}
# tty must not be run inside a function
# (functions have their own input and output descriptors).
TTY=`tty`
run() {
cat $FOLDERS_DB | while read FOLDER
do
$CLEAN_OUTPUT || echo `yellow $USER@$(cat /etc/hostname)`:`cyan $FOLDER`$ $*
bash -c "cd $FOLDER; $*" 0<$TTY || true
$CLEAN_OUTPUT || echo `magenta EOF`
done
}
# </COMMANDS>
case "$CMD" in
"list") ;& "all") list;;
"add") add $ARGS;;
"del") ;& "delete") ;& "remove") ;& "rm") del $ARGS;;
"run") run $ARGS;;
"help") ;& "?") ;& "usage") usage;;
"")
echo `red "command is required"` >&2
echo "" >&2
usage
;;
*)
echo `red "unknown command: $CMD"` >&2
echo "" >&2
usage
;;
esac