-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_func
489 lines (426 loc) · 12.1 KB
/
.bash_func
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
############################################################
# functions
############################################################
function swap() # Swap 2 filenames around, if they exist
{
local TMPFILE=tmp.$$
[ $# -ne 2 ] && echo "swap: 2 arguments needed" && return 1
[ ! -e $1 ] && echo "swap: $1 does not exist" && return 1
[ ! -e $2 ] && echo "swap: $2 does not exist" && return 1
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
}
function lowercase() # move filenames to lowercase
{
for file ; do
filename=${file##*/}
case "$filename" in
*/*) dirname==${file%/*} ;;
*) dirname=.;;
esac
nf=$(echo $filename | tr A-Z a-z)
newname="${dirname}/${nf}"
if [ "$nf" != "$filename" ]; then
mv "$file" "$newname"
echo "lowercase: $file --> $newname"
else
echo "lowercase: $file not changed."
fi
done
}
function fspaces()
{
for file ; do
filename=${file##*/}
case "$filename" in
*/*) dirname==${file%/*} ;;
*) dirname=.;;
esac
nf=$(echo $filename | sed -E 's/[[:space:]]+/_/g')
newname="${dirname}/${nf}"
if [ "$nf" != "$filename" ]; then
mv "$file" "$newname"
echo "fspaces: $file --> $newname"
else
echo "fspaces: $file not changed."
fi
done
}
function repeat() # Repeat n times command
{
local i max
max=$1; shift;
for ((i=1; i <= max ; i++)); do
eval "$@";
done
}
function extract() # Handy Extract Program
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
function dirsize()
{
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
egrep '^ *[0-9.]*M' /tmp/list
egrep '^ *[0-9.]*G' /tmp/list
rm -f /tmp/list
}
function fe() # Find a file with pattern $1 in name and Execute $2 on it
{
# Check for proper number of command line args.
EXPECTED_ARGS=2
if [ $# -ne $EXPECTED_ARGS ]; then
echo "ERROR: invaild # of args: Usage: fe pattern cmd"
else
find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \; ;
fi
}
function apath() # Temporarily add directories to PATH
{
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo "Temporarily add to PATH"
echo "usage: apath [dir]"
else
PATH=$1:$PATH
fi
}
function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; }
#function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; }
function killps() # Kill by process name
{
local pid pname sig="-TERM" # Default signal
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: killps [-SIGNAL] pattern"
return;
fi
if [ $# = 2 ]; then sig=$1 ; fi
for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} ) ; do
pname=$(my_ps | awk '$1~var { print $5 }' var=$pid )
if ask "Kill process $pid <$pname> with signal $sig?"
# then kill -9 $sig $pid
then kill -9 $pid
fi
done
}
function ask()
{
echo -n "${RED}$@" '[y/n] ' "${RESET}" ; read ans
case "$ans" in
y*|Y*) return 0 ;;
*) return 1 ;;
esac
}
function my_ip() # Get IP address
{
MY_IP=$(/sbin/ifconfig | awk '/inet/ { print $2 } ' | sed -e s/addr://)
# MY_ISP=$(/sbin/ifconfig | awk '/P-t-P/ { print $3 } ' | sed -e s/P-t-P://)
}
function hi() # Get current host related info
{
echo -e "\n${RED}You are logged on ${RED}$HOST"
echo -e "\nAdditionnal information:$NC " ; uname -a
echo -e "\n${RED}Current date :$NC " ; date
echo -e "\n${RED}Machine stats :$NC " ; uptime
echo -e "\n${RED}Memory stats :$NC " ; free
my_ip 2>&- ;
echo -e "\n${RED}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"}
# echo -e "\n${RED}ISP Address :$NC" ; echo ${MY_ISP:-"Not connected"}
echo -e "\n${YELLOW}Users logged on:$NC " ; w -h
echo "${RESET}"
}
function ni() # network information
{
echo "${RED}"
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
/sbin/ifconfig | awk /'Bcast/ {print $3}'
/sbin/ifconfig | awk /'inet addr/ {print $4}'
/sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "---------------------------------------------------"
echo -e "\n${RED}Open connections :$NC "; netstat -pan --inet;
echo "${RESET}"
}
function bash_stat()
{
cut -f1 -d" " .bash_history | sort | uniq -c | sort -nr | head -n 10
}
alias hideme='history -d $((HISTCMD-1)) &&' $1
function clock()
{
while true; do
xit='null'
echo "${INVIS}"; read -s -t 1 -n 1 xit
if [ "$xit" == "q" ]; then
echo "Good-bye?!${RESET}"; sleep 1; clear; echo "${VIS}"
break
else
clear
timem=$(date +"%r" | awk /'/ {print $2}')
timet=$(date +"%r" | sed -E 's/(.{2})/\1 /g' | awk /'/ {print $1}')
if [ "$timem" == "AM" ]; then
if [ "$timet" == "12" ]; then echo "${BLACK}"
elif [ "$timet" == "01" ]; then echo "${BLACK}"
elif [ "$timet" == "02" ]; then echo "${BLACK}"
elif [ "$timet" == "03" ]; then echo "${BLUE}"
elif [ "$timet" == "04" ]; then echo "${BLUE}"
elif [ "$timet" == "05" ]; then echo "${BLUE}"
elif [ "$timet" == "06" ]; then echo "${RED}"
elif [ "$timet" == "07" ]; then echo "${RED}"
elif [ "$timet" == "08" ]; then echo "${YELLOW}"
elif [ "$timet" == "09" ]; then echo "${CYAN}"
elif [ "$timet" == "10" ]; then echo "${CYAN}"
elif [ "$timet" == "11" ]; then echo "${CYAN}"
fi
fi
if [ "$timem" == "PM" ]; then
if [ "$timet" == "12" ]; then echo "${CYAN}"
elif [ "$timet" == "01" ]; then echo "${CYAN}"
elif [ "$timet" == "02" ]; then echo "${CYAN}"
elif [ "$timet" == "03" ]; then echo "${CYAN}"
elif [ "$timet" == "04" ]; then echo "${CYAN}"
elif [ "$timet" == "05" ]; then echo "${YELLOW}"
elif [ "$timet" == "06" ]; then echo "${RED}"
elif [ "$timet" == "07" ]; then echo "${RED}"
elif [ "$timet" == "08" ]; then echo "${BLUE}"
elif [ "$timet" == "09" ]; then echo "${BLUE}"
elif [ "$timet" == "10" ]; then echo "${BLACK}"
elif [ "$timet" == "11" ]; then echo "${BLACK}"
fi
fi
echo "==================================="
echo "=========== $(date +"%r") ==========="
echo "==================================="
fi
done
}
function timer()
{
seconds=0
minutes=0
hours=0
while true; do
xit='null'
echo "${INVIS}"; read -s -t 1 -n 1 xit
clear
echo "${RED}"
echo "==================================="
echo "========== $(printf '%02d' ${hours}) : $(printf '%02d' ${minutes}) : $(printf '%02d' ${seconds}) ==========="
echo "==================================="
seconds=$((seconds + 1))
if [ "$minutes" -eq "59" ] && [ "$seconds" -eq "60" ]; then hours=$((hours + 1)) && minutes=0 && seconds=0; fi
if [ "$seconds" -eq "60" ]; then minutes=$((minutes + 1)) && seconds=0; fi
done
}
function shot() # Takes a screenshot of your size choice
{
import -frame -strip -quality 75 "$HOME/$(date +%s).png"
}
# Creates an archive from given directory
mktar() { tar cvf "${1%%/}.tar" "${1%%/}/"; }
mktgz() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
mktbz() { tar cvjf "${1%%/}.tar.bz2" "${1%%/}/"; }
function colortest()
{
# set colors
echo "${BLACK} TEST test ${TXTBLD} TEST test ${RESET}"
echo "${RED} TEST test ${TXTBLD} TEST test ${RESET}"
echo "${GREEN} TEST test ${TXTBLD} TEST test ${RESET}"
echo "${YELLOW} TEST test ${TXTBLD} TEST test ${RESET}"
echo "${BLUE} TEST test ${TXTBLD} TEST test ${RESET}"
echo "${PURPLE} TEST test ${TXTBLD} TEST test ${RESET}"
echo "${CYAN} TEST test ${TXTBLD} TEST test ${RESET}"
echo "${WHITE} TEST test ${TXTBLD} TEST test ${RESET}"
}
function color256()
{
for i in {0..255} ; do
printf "\x1b[38;5;${i}mcolour${i}\n"
done
}
function trans()
{
awk '
{
for (i=1; i<=NF; i++) {
a[NR,i] = $i
}
}
NF>p { p = NF }
END {
for(j=1; j<=p; j++) {
str=a[1,j]
for(i=2; i<=NR; i++){
str=str" "a[i,j];
}
print str
}
}' $1
}
function add()
{
awk '{total = total + $1}END{print total}' $1
}
function spl() {
mysql -e 'show processlist' | egrep $1
}
# A simple script which will name a tab in iTerm
function nametab () {
echo -ne "\033]0;"$@"\007"
}
google() {
search=""
echo "Googling: $@"
for term in $@; do
search="$search%20$term"
done
open "http://www.google.com/search?q=$search"
}
gc() {
# git add .
git commit -m "$*"
}
gcb() {
git checkout -b "$*"
}
function last_two_dirs {
pwd |rev| awk -F / '{print $1,$2}' | rev | sed s_\ _/_
}
function spy()
{
usage="\nspy <server>\n"
if [ $# -lt 1 ] || [ "$1" = "-h" ] ; then
echo -e "$usage"
return
fi
server="$1"
ssh -t $server "watch 'mysqladmin processlist | grep -v Sleep'"
}
function how_to_ln {
echo "ln -s /path/to/file /path/to/symlink"
echo "more info at http://stackoverflow.com/questions/1951742/how-to-symlink-a-file-in-linux"
}
function kitten() {
for i in $@; do echo; echo $'\e[33;1m'$i$'\e[0m'; cat $i; done
}
k8s_node_of_first_pod() {
kubectl get po -o wide | grep "$1" | head -n1 | awk '{print $NF}'
}
hostname() {
echo "$1" | egrep '(\d+[\.-]){3}(\d+)'
}
cadvisor() {
local hostname local_port
if hostname $1; then
hostname="$1"
else
hostname="$(k8s_node_of_first_pod "$1")"
fi
local_port="${2-4194}"
echo "tunnelling to $hostname"
echo "cadvisor is available locally on port $local_port"
ssh -NL "${local_port}":localhost:4194 "$hostname"
}
airport_code_lookup() {
local input
input="$1"
airport=$(echo "$input" | tr /a-z/ /A-Z/)
curl -s https://raw.githubusercontent.com/jbrooksuk/JSON-Airports/master/airports.json \
| jq ".[] | select(.iata == \"$airport\")"
}
volume() {
local adjustment current_volume new_volume
adjustment="$1"
current_volume=$(osascript -e 'output volume of (get volume settings)')
case "$adjustment" in
+)
new_volume=$(($current_volume + 10))
set_volume "$new_volume"
;;
-)
new_volume=$(($current_volume - 10))
set_volume "$new_volume"
;;
[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9]|9[0-9])
set_volume "$adjustment"
;;
current)
print_volume "$current_volume"
;;
max)
set_volume '100'
;;
min)
set_volume '0'
;;
mute)
# Check to see if volume is already muted
if [[ $(echo $(osascript -e 'output muted of (get volume settings)')) == 'true' ]]; then
osascript -e 'set volume output muted false'
echo "${GREEN}Volume unmuted${RESET}"
else
osascript -e 'set volume output muted true'
echo "${GREEN}Volume muted${RESET}"
fi
;;
*)
print_volume "$current_volume"
;;
esac
}
set_volume() {
local volume
volume="$1"
osascript -e "set volume output volume $volume"
print_volume "$volume"
}
print_volume() {
local level
level="$1"
echo "${GREEN}Volume set to $level%${RESET}"
}
snow() {
ruby -e 'C=`stty size`.scan(/\d+/)[1].to_i;S=["2743".to_i(16)].pack("U*");a={};puts "\033[2J";loop{a[rand(C)]=0;a.each{|x,o|;a[x]+=1;print "\033[#{o};#{x}H \033[#{a[x]};#{x}H#{S} \033[0;0H"};$stdout.flush;sleep 0.1}'
}
git_url() {
local url="$1"
curl -is https://git.io -F "url=$url"
}
tmux_layout() {
tmux list-windows | sed -n 's/.*layout \(.*\)] @.*/\1/p'
}
math() {
local input=$@
#$(($input)) 2>&1 | awk '{gsub(/:/,""); print $2}'
ruby -e "puts $input"
}
export MANPATH="/Users/$(whoami)/man:/usr/local/share/man:/usr/share/man"
make_manpath() {
DIRS='
/usr/local/share/man
/usr/share/man
/usr/local/MacGPG2/share/man
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man
/Applications/Xcode.app/Contents/Developer/usr/share/man
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
~/scratch/ansible/docs/man'
export MANPATH=$(echo $DIRS | tr ' ' ':')
}