-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_functions
More file actions
349 lines (310 loc) · 7.92 KB
/
bash_functions
File metadata and controls
349 lines (310 loc) · 7.92 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
# vim: set ft=sh:
# daysd and daysi adapted from https://github.com/sorhus/random/blob/master/bash/functions
#
# Get the list of dates between $1 and $2 (exclusive)
#
# e.g. $ daysd 2016-01-01 2016-01-03
# > 2016-01-01 2016-01-02
#
function daysd {
unset result
current=$1
end=$2
i=0
while [[ "$current" < "$end" ]] ; do
result[$(( i++ ))]=$current
current=$(date +%Y-%m-%d -d "$current + 1 day")
done
echo ${result[*]}
}
#
# Get a list of $2 dates starting at $1
#
# e.g. $ daysi 2016-01-01 2
# > 2016-01-01 2016-01-02
#
function daysi {
daysd $1 "$(date +%Y-%m-%d -d "$1 + $2 day")"
}
pd() {
local filename="$(date +%Y-%m-%d_%H:%M:%S)-ipython.log"
ipython3 --logfile=$filename -i -c 'from zgulde.ds_imports import *'
}
# custom ll
list-long() {
local cols='##PERMS### LINKS OWNER GROUP SIZE MONTH DAY HH:MM NAME'
(echo $cols
ls -lh | sed 1d | awk "{print \$0}; NR % 10 == 0 {print \"$cols\"}"
) | column -t
}
# look at all the emoji
emoji() {
if [[ -n $1 ]] ; then
grep -i $1 ~/misc/emoji-list.txt
else
cat ~/misc/emoji-list.txt
fi
}
# figure out what is running on a given port
whats-on-port() {
# stolen from https://github.com/alexch/dotfiles/blob/master/functions.sh
lsof -i TCP:$1
}
# look something up in the php manual
phpman() {
open "http://php.net/manual-lookup.php?pattern=$1&scope=quickref"
}
# google (duckduckgo) something
g() {
ruby -r cgi -e '`open http://duckduckgo.com/?q=#{CGI.escape(ARGV.join(" "))}`' "$@"
}
# interaction with codeup vagrant-lamp box
vl() {
if [[ $# -eq 0 ]]; then
cd ~/vagrant-lamp
else
( cd ~/vagrant-lamp && vagrant $* )
fi
}
# interaction with warpspeed vagrant box
warpspeed() {
if [[ $# -eq 0 ]]; then
cd ~/Sites
else
( cd ~/warpspeed-vagrant && vagrant $* )
fi
}
# interaction w/ vagrant homestead box
homestead() {
if [[ $# -eq 0 ]]; then
cd ~/Code
elif [[ $1 == "cfg" ]]; then
vim ~/.homestead/Homestead.yaml
else
( cd ~/Homestead && vagrant $* )
fi
}
# get the status code from a url
get-status() {
local url=$1
curl --silent --head $url |\
head -n 1 |\
egrep -o '\b\d{3}\b'
}
# create and switch to a directory
mcd() {
mkdir -p "$@"
cd "$@"
}
# cowsay with a random cow
randcowsay() {
NUMOFCOWS=`cowsay -l | tail -n +2 | wc -w`
WHICHCOW=$((RANDOM%$NUMOFCOWS+1))
THISCOW=`cowsay -l | tail -n +2 | sed -e 's/\ /\'$'\n/g' | sed $WHICHCOW'q;d'`
cowsay -f $THISCOW $1
}
# sends an email from my mailgun domain - sendEmail <to> <subject> <body> [from='Don't Reply']
sendEmail() {
if [ "$1" == "help" ]
then
echo ""
echo "description"
echo " sends an email from my mailgun domain"
echo "usage"
echo " $ sendEmail <to> <subject> <body> [from='Don't Reply']"
echo ""
return
fi
TO=$1
SUBJECT=$2
BODY=$3
FROM=$4
if [ -z FROM ]
then
FROM="Don't Reply"
fi
curl -s --user "api:$MAILGUN_API_KEY" \
https://api.mailgun.net/v3/$MAILGUN_DOMAIN/messages \
-F from="$FROM <postmaster@$MAILGUN_DOMAIN>" \
-F to="$TO" \
-F subject="$SUBJECT" \
-F text="$BODY"
}
# parse_git_branch and parse_git_dirty adapted from http://ezprompt.net/
# get current branch in git repo (for PS1)
function parse_git_branch() {
local branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
local commit_msg=`git log -n1 --pretty='%s' 2>/dev/null`
if [ ! "${branch}" == "" ] ; then
local status=`parse_git_dirty`
echo "<${branch}:${commit_msg}>${status}"
else
echo ""
fi
}
# get current status of git repo (for git PS1)
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
# show first letter of the branch name and the symbols for modification (for PS1)
function minimal_git_status() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' | head -c 1`
if [ ! "${BRANCH}" == "" ] ; then
STAT=`parse_git_dirty`
echo "${BRANCH}${STAT}"
else
echo ""
fi
}
# recursivley grep all files in this directory, case insensitive, show linenums
rgrep() {
grep -iRn $1 ./
}
# function g() {
# g++ $1 && ./a.out
# }
# docker rm any 'Exited' containers
docker-cleanup() {
for id in $(docker ps -a | grep 'Exited' | awk '{print $1}') ; do
docker rm $id
done
}
# docker kill everything that's running (docker ps -q)
docker-kill-all() {
for id in $(docker ps -q) ; do
docker kill $id
done
}
# docker rmi any images that match <none>
docker-image-cleanup() {
for imgid in $(docker images | grep \<none\> | awk '{print $3}') ; do
docker rmi $imgid
done
}
# recursively find all the .git directories in the pwd and show their statuses
git-all-status(){
current_dir="$(pwd)"
for dir in $(find . -name '.git')
do echo -e "\n$dir\n\n"
cd $dir
cd ..
git status
cd $current_dir
done
}
# render a .dot file and immediately open it
graphv() {
dot -Tsvg -O $1; open -a 'Google Chrome' $1.svg
}
# open the documentation web page for various things
docs() {
local url
case $1 in
sb|spring|springboot)
open https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
;;
ss|spring-security)
open https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/
;;
bs|bs4|bootstrap)
open http://getbootstrap.com/docs/4.0/components/alerts/
;;
bs3|bootstrap3)
open https://getbootstrap.com/docs/3.3/
;;
thy|thyme|thymeleaf)
open http://www.thymeleaf.org/documentation.html
;;
jq)
open https://stedolan.github.io/jq/manual/
;;
emoji)
open https://unicode.org/Public/emoji/11.0/emoji-data.txt
;;
*)
cat <<-.
sb | spring | springboot
ss | spring-security
bs | bs4 | bootstrap
bs3 | bootstrap3
thy | thyme | thymeleaf
jq
emoji
.
;;
esac
}
# log an entry with a timestamp to ~/Dropbox/log.txt
lg() {
if [[ $# -eq 0 ]] ; then
cat <<-.
lg -- log events to ~/Dropbox/log.txt
.
return 1
fi
echo -e "$(date +%Y-%m-%d\ %H:%M:%S)\t$@" >> ~/Dropbox/log.txt
}
# put my email signature (in markdown) on the clipboard
sig() {
pbcopy <<-.
\--
Zach Gulde
Curriculum Developer @ [Codeup](https://codeup.com)
.
}
# show the help for custom functions
chelp() {
perl -0777 -ne 'while(m/#\s*(.*?)\n([a-zA-Z-_]+)\(\)\s*{\n/g) { print "\033[35m$2\033[0m\t$1\n" }' ~/.bash_functions | column -c$(tput cols) -ts$'\t' | sort
}
# print only the most recently modified file
ls-latest() {
ls -1tr $1 | tail -n 1
}
# wait until a url successfully responds to a ping, then run a command
pingthen() {
local url=$1
shift
ping -o $url && eval "$@"
}
make_help() {
\cat <<'.'
help: ## Show this help message
@grep -E '^[a-zA-Z._-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[34m%s\033[0m\t%s\n", $$1, $$2}' | column -ts$$'\t'
.
}
fpass() {
pass $1 $(fd . ~/.password-store | rg '^.*\.password-store/(.+)\.gpg$' --replace '$1' | fzf)
}
docker-ip() {
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1
}