-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunc_rm.sh
214 lines (182 loc) · 5.4 KB
/
func_rm.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
214
# rmd
#-------------------------------------------------------------------------------
rmd() {
local NAME MAP YORN REPEAT FORCE USAGE CREATE OPTIONS OPTION
NAME=rmd
FORCE=no
USAGE=yes
CREATE=no
usage()
{
cat <<EOF
NAME
$NAME - Remove a directory that might not be empty
(user is prompted before removal)
SYNOPSIS
$NAME [OPTION] map1 map2 ...
DESCRIPTION
Following options can be used:
-c, --create
Create back removed directory.
-f, --force
Do not ask for confirmation. Use with care!
-h, --help
Print this output.
EXAMPLE
Remove directory test
$NAME test
Remove directory test and test2
$NAME test test2
Remove directory test without asking first
$NAME -f test
AUTHOR
Gregor GORJANC <gregor.gorjanc at gmail dot com>
EOF
}
# --- Options ---
OPTIONS=("$@")
for OPTION in "$@"; do
case "$OPTION" in
-c | --create )
OPTIONS=$(echo $OPTIONS | sed -e "s|$OPTION||")
CREATE=yes
;;
-f | --force )
OPTIONS=$(echo $OPTIONS | sed -e "s|$OPTION||")
FORCE=yes
;;
-h | --help )
OPTIONS=$(echo $OPTIONS | sed -e "s|$OPTION||")
USAGE=no
usage
;;
* ) # no options, just run
;;
esac
done
# --- Funcs ---
rmMap() {
if [ -e "$1" ]; then
if [ -d "$1" ]; then
rm -Rf "$1"
if [ -e "$1" ]; then
echo "Directory $1 not removed. Something went wrong! "
elif [ -n "$2" -a "$2" = "yes" ]; then
mkdir "$1"
echo "Directory $1 cleaned. "
else
echo "Directory $1 removed. "
fi
else
echo "$1 is not a directory. Skipped! "
fi
else
echo "Directory $1 doesn't exist. "
fi
}
# --- Core ---
if [ -n "$OPTIONS" ]; then
echo "Working directory ${PWD}. "
for MAP in "${OPTIONS[@]}"; do
if [ "$FORCE" = "no" ]; then
REPEAT=true
while ${REPEAT}; do
echo
echo -e "Are you sure to remove directory $MAP (y/s/N)? s - show contents \c"
read YORN
REPEAT=false
if [ "${YORN}" = "y" ]; then
rmMap "$MAP" "$CREATE"
elif [ "${YORN}" = "s" ]; then
if [ -e "$MAP" ]; then
ls -lFa "$MAP"
REPEAT=true
else
echo "Directory $MAP doesn't exist. "
fi
else
echo "Directory $MAP not removed. "
fi
done
else
rmMap "$MAP" "$CREATE"
fi
done
else
if [ "$USAGE" = "yes" ]; then
usage
fi
fi
}
# cld
#-------------------------------------------------------------------------------
cld() {
local NAME
NAME=cld
usage()
{
cat <<EOF
NAME
$NAME - Empty/clear a directory (user is prompted before removal)
SYNOPSIS
$NAME [OPTION] map1 map2 ...
DESCRIPTION
Following options can be used:
-f, --force
Do not ask for confirmation. Use with care!
-h, --help
Print this output.
EXAMPLE
Empty directory test
$NAME test
Empty directory test and test2
$NAME test test2
Empty directory test without asking first
$NAME -f test
AUTHOR
Gregor GORJANC <gregor.gorjanc at gmail dot com>
EOF
}
rmd "$@" --create
}
# rmtex
#-------------------------------------------------------------------------------
rmtex() {
# removes defined TeX temporary files in current directory.
local NAME FILES FILE YORN REPEAT USAGE
NAME=rmtex
USAGE="$NAME"
FILES=("*.log" "*.aux" "*.blg" "*.toc" "*.lot" "*.lof" "*.loa" "*.dvi" "*.idx" "*.bbl" "*.ilg" "*.ind" "*.tex.dep" "*.bm" "*.synctex.gz")
echo "Working directory ${PWD}"
REPEAT=true
while $REPEAT; do
echo
echo "--> ${FILES[*]}"
echo -e "Are you sure to remove TeX temporary files in this directory (y/s/N) s - show files? \c"
read YORN
REPEAT=false
case "$YORN" in
y)
setopt nullglob # Prevent errors if no files match the glob patterns
for FILE in "${FILES[@]}"; do
MATCHED_FILES=(${~FILE}) # Expand the glob pattern and store in an array
if [ -n "$MATCHED_FILES" ]; then
rm -f -- ${MATCHED_FILES[@]} # Remove matched files, if any
echo "TeX temporary files matching $FILE removed."
fi
done
unsetopt nullglob # Reset to the default behavior once done
;;
s)
echo "Listing TeX temporary files:"
for FILE in "${FILES[@]}"; do
find . -maxdepth 1 -name "$FILE"
done
REPEAT=true
;;
*)
echo "TeX temporary files not removed."
;;
esac
done
}