-
Notifications
You must be signed in to change notification settings - Fork 12
/
@
executable file
·432 lines (344 loc) · 9.29 KB
/
@
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/bin/bash
# @, aka monkey-tail
# Licenced under the GNU GPLv2
###########################################################################
# Globals
SELF=$( basename $0 )
SELF_LONG=$0
SELF_VERSION="0.01"
SELF_AUTHOR="Lucas Martin-King"
###########################################################################
# Embedded help
# |start|<program>|start a program detached from a terminal|
# |run-once|<program>|only start a program if it isn't running|
# |config|[<variable> [<new value>]]|manage embedded settings|
# |list-variables||list all embedded settings|
# |set-variable|<variable> <new value>|set variable to new value|
# |get-variable|<variable>|get variable value|
# |list-externals||list the names of all embedded externals|
# |external-add|<origin>|add an external from origin|
# |external-update|<external> [<origin>]|update an external's code|
# |external-remove|<external>|remove external's code, keeping header|
# |external-append|<external> <file>|append code to external|
# |external-code|<external>|output external's code, without headers|
# |external-extract|<external>|output external's code, including headers|
# |list-requirements||list required system commands|
# |check-requirements||check if system has required commands|
# |disk-usage|<path>|show disk usage|
# |disk-hogs|<path>|show top 10 disk hogs|
# |disk-free|<path>|show free space|
# |mem-used||show used memory|
# |mem-free||show free memory|
# |mem-total||show total memory|
# |proc|<name or pid>|show process info|
# |proc-tree|<name or pid>|show process tree|
# |proc-files|<name or pid>|show process files|
###########################################################################
# Embedded settings
# :proc.fields=uname,tty=TTY,pid,pcpu,pmem,rss,comm:
# :disk.hogs.number=10:
###########################################################################
# Subcommands
_start () {
require nohup
[[ -z "$1" ]] && return
( nohup $* > /dev/null 2>&1 & ) > /dev/null 2>&1
}
_run-once () {
require pidof
pidof $1 || _start $*
}
_disk-usage () {
require du
local dir="."
[[ ! -z "$1" ]] && dir=$1
du -chs $1 | tail -n 1
}
_disk-hogs () {
require du sort head cut printf xargs
IFS="$(printf '\n\t')"
if [[ ! -z "$1" ]] ; then
[[ ! -d $1 ]] && exit 1
[[ -d $1 ]] && cd $1
fi
dirs="$( ls -A . )"
du -sk $dirs | sort -nr | head -n $( _get-variable disk.hogs.number ) | cut -f 2 |
xargs -i% du --max-depth=0 -h "%"
}
_disk-free () {
require df
local dir="."
[[ ! -z "$1" ]] && dir=$1
df -h $dir
}
_mem-free () {
require free
free -m | sed -n 3p | awk '{ print $4"M" }'
}
_mem-used () {
require free
free -m | sed -n 3p | awk '{ print $3"M" }'
}
_mem-total () {
require free
free -m | awk '/Mem:/ { print $2"M" }'
}
_proc () {
require ps
local opt="-C $1"
local fields=$( _get-variable proc.fields )
is_numeric $1 && opt="-p $1"
[[ -z "$1" ]] && opt="-U $USER"
ps -H -o $fields $opt
}
_proc-tree () {
require pstree pidof
for pid in $(pidof $1) ; do
pstree -l $pid
done
}
_proc-files () {
require lsof
local opt="-c $1"
is_numeric $1 && opt="-p $1"
lsof -f $opt | grep -v "/lib\|/usr/lib\|/usr/local/lib\| socket\| pipe\| anon_inode"
}
###########################################################################
# Helper commands
_help () {
echo "Usage: $SELF <subcommand>"
echo " or: $SELF list-commands"
echo " or: $SELF help-on <subcommand>"
exit 1
}
_version () {
echo "$SELF, version $SELF_VERSION, by $SELF_AUTHOR"
exit 0
}
_list-commands () {
declare -F | cut -d ' ' -f 3 | grep "^_.*" | sed 's/^_//' | column
}
_help-on () {
local cmd=$1
grep "^# |$cmd|" $SELF_LONG | sed -e 's/# |//' -e 's/|$//' -e 's/|/\t/g'
}
_get-variable () {
local var=$1
grep "^# :$var=.*:$" $SELF_LONG | sed -e 's/# ://' -e 's/:$//' | cut -d '=' -f 2- | head -n 1
}
_set-variable () {
local var val oldval file
[[ ! -w $SELF_LONG ]] && die "$SELF is read only"
var=$1
val=$2
var_exists $var || die "$var doesn't exist"
oldval=$( _get-variable $var )
[[ "$oldval" == "$val" ]] && exit 0
[[ -z "$val" ]] && warn "new value is empty"
echo "var: $var old: $oldval new: $val"
sed -i.bak -e "s/# :$var=$oldval:/# :$var=$val:/" $SELF_REAL
}
_list-variables () {
grep "^# :.*=.*:$" $SELF_LONG | sed -e 's/# ://' -e 's/:$//' | uniq
}
_config () {
if [[ -z "$1" ]] ; then
_list-variables
fi
if [[ ! -z "$1" ]] && [[ ! -z "$2" ]] ; then
_set-variable ${1} ${2}
fi
_get-variable ${1}
}
_list-externals () {
grep -n "^# {begin:.*:.*}" $SELF_LONG | cut -d ":" -f 3
}
_external () {
local begins ends origin loc
origin=$( ext_origin $1 )
begins=$( ext_begins $1 )
ends=$( ext_ends $1 )
[[ "$begins" == "" || "$ends" == "" ]] && return 1
loc=":"
[[ $begins -gt $ends ]] && begins=$ends && loc="|"
echo "${begins}${loc}${ends} ${origin}"
}
_external-code () {
ext_code $1
}
_external-extract () {
ext_header $1 $( ext_origin $1 )
ext_code $1
ext_footer $1
}
_external-remove () {
local starts ends file
starts=$( ext_begins $1 )
ends=$( ext_ends $1 )
[[ -z "$starts" || -z "$ends" ]] && die "Cannot find start or end of $1"
is_numeric $starts || die "something is not right"
is_numeric $ends || die "something is not right"
[[ $ends -lt $starts ]] && return 1
echo "Removing code of '$1' from $SELF_REAL"
sed -i.bak -e "${starts},${ends}d" $SELF_REAL
}
_external-append () {
local line=$( ext_ends $1 )
local file=$2
echo "Appending code from $file to $SELF_REAL"
[[ -f "$file" && "$line" != "" ]] && ext_insert "$file" $line
}
_external-add () {
local origin name tmpfile offset
origin=$1
[[ -f $origin ]] && origin=$( readlink -f $origin )
name=$( basename $origin )
echo $name
ext_begins $name > /dev/null && die "An external already exists by that name"
ext_ends $name > /dev/null && die "An external already exists by that name"
echo "..."
tmpfile=/tmp/$SELF.$$.ext.add.tmp
echo $tmpfile
echo > $tmpfile
ext_header $name $origin >> $tmpfile
ext_footer $name >> $tmpfile
offset=$( grep -n "^# {end:.*}$" $SELF_LONG | tail -n 1 | cut -d ":" -f 1 )
if [[ -z "$offset" ]] ; then
offset=$( wc -l $SELF_LONG | cut -d ' ' -f 1 )
let "offset = $offset - 5"
fi
ext_insert $tmpfile $offset && _external-update $name
}
_external-update () {
local ext=$1
local origin
if ext_begins $ext > /dev/null && ext_ends $ext > /dev/null ; then
origin=$( ext_origin $ext )
[[ ! -z "$2" ]] && origin=$2
[[ -z "$origin" ]] && die "Origin not set"
echo "Using $origin as origin"
local ofile
if echo $origin | grep -q '\(http\(s\?\)\|ftp\)://' ; then
ofile=/tmp/$SELF.$$.ext.tmp
echo "Fetching from $origin, saving to $ofile"
wget --no-verbose -nc --progress=dot -O $ofile $origin || die "Could not fetch properly"
else
ofile=$origin
fi
if [[ -f "$ofile" ]] ; then
sed '1p' $ofile | grep -q "^# @$" || warn "$ofile doesn't have '# @' on the first line, is it an external?"
else
die "Cannot load $origin"
fi
local backup=/tmp/$SELF.$$.tmp
cp $SELF_REAL $backup && echo "Backup of $SELF_REAL is at $backup"
_external-remove $ext
_external-append $ext $ofile
else
die "Cannot update '$ext' because I can't find it"
fi
}
_list-requirements () {
require uniq
local reqs=$(grep '\srequire \(\w*\)\(\(\s*\)\(\w*\)\)*' $SELF | sed -e 's/require//g' -e 's/ /\n/g' | sort | uniq)
echo $reqs
}
_check-requirements () {
local reqs=$(_list-requirements)
for req in ${reqs} ; do
eval "require $req"
done
}
###########################################################################
# Helper functions
catch_all () {
case "$1" in
'-v' | '-version' | '--version') _version ;;
'-h' | '-help' | '--help') _help ;;
esac
[[ ! -z "$1" ]] && echo "Command '$1' does not exist"
_help
}
fn_exists () {
type $1 2> /dev/null | grep -q 'function'
}
is_numeric () {
echo "$@" | grep -q -v "[^0-9]"
}
var_exists () {
local var=$1
grep -q "^# :$var=.*:$" $SELF_LONG
}
die () {
echo "error: $1" 1>&2 && exit 1
}
warn () {
echo "warning: $1" 1>&2
}
file_type () {
[[ ! -f "$1" ]] && return 1
file -b "$1" | cut -d "," -f 1
}
file_mime () {
[[ ! -f "$1" ]] && return 1
file -b -i "$1" | cut -d ";" -f 1
}
require () {
for cmd in $@ ; do
if [[ ! "$( command -v "$cmd" )" ]] ; then
die "require $cmd"
fi
done
}
main () {
require sed awk grep cut head tail readlink file
SELF_REAL=$( readlink -f $SELF_LONG )
[[ -z "$SELF_REAL" ]] && SELF_REAL=$SELF_LONG
if fn_exists _$(basename $0) ; then
CMD=$(basename $0)
elif fn_exists _$1 ; then
CMD=$1
shift
else
catch_all $1
fi
_$CMD "$@"
}
ext_begins () {
local ln=$( grep -n "# {begin:$1:.*}" $SELF_LONG | cut -d ':' -f 1 )
[[ -z "$ln" ]] && return 1
expr $ln + 1
}
ext_ends () {
local ln=$( grep -n "# {end:$1}" $SELF_LONG | cut -d ':' -f 1 )
[[ -z "$ln" ]] && return 1
expr $ln - 1
}
ext_origin () {
sed -n 's/# {begin:'${1}':\(.*\)}/\1/p' $SELF_LONG
}
ext_code () {
local start end
start=$( ext_begins $1 )
end=$( ext_ends $1 )
[[ -z "$start" || -z "$end" ]] && die "Cannot find start or end of $1"
[[ $end -lt $start ]] && return 1
sed -n "${start},${end}p" $SELF_LONG
}
ext_insert () {
local file start
file=$1
start=$2
sed -i.bak -e "${start} r ${file}" $SELF_REAL
}
ext_header () {
echo "# {begin:$1:$2}"
}
ext_footer () {
echo "# {end:$1}"
}
###########################################################################
# External commands
###########################################################################
# Program
main "$@"