-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_mac
473 lines (410 loc) · 12.3 KB
/
bash_mac
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
shopt -s histappend
[[ "$BASH_VERSION" == 4.* ]] && shopt -s globstar
[[ "$PS1" ]] && {
alias mv='mv -i'
alias cp='cp -i'
}
# LOAD BASH Completions
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
export TERM=xterm-256color
OS=$(uname)
export OS
# "HISTCONTROL" possible values:
# ignorespace -> ignore command lines with leading space
# erasedups -> delete duplicates of current command line from history
# ignoredups -> don't add duplicates to history
# {NO VALUE} -> add everything
export HISTCONTROL=ignorespace:erasedups
export HISTSIZE=
export HISTFILESIZE=
export HISTTIMEFORMAT='%s '
export EDITOR=vim
export PAGER=less
function _ps1() {
local UC="\[\033[38;5;66m\]"
local HC="\[\033[38;5;130m\]"
local DC="\[\033[38;5;135m\]"
local NC="\[\e[0m\]"
local SC="\[\033[38;5;240m\]"
local PC="\[\033[38;5;240m\]"
#echo
#ps1-host-icon
echo "${UC}\\u${SC}@${HC}\\h${SC}:${DC}\\w ${PC}\$${NC} "
#echo "${UC}\\u${SC}@${HC}\\h${SC}:${DC}\\w ${PC}\$${NC} "
# echo "\$(clock) $TC\D{%Y-%m-%d %H:%M:%S} ${HC}\\! ${UC}\\u${SC}@${MC}\\h${SC}:${DC}\w${NC}"
# echo -en "${PC}\xE2\x9E\xAA${NC} "
}
PS1="$(_ps1)"
#export PS1
function _set_path() {
local path
for p in {/usr/local,/usr,}/{bin,sbin} "$@"; do
[[ -d "$p" ]] && path="$path:$p"
done
export PATH=${path:1}
}
_set_path '/usr/local/go/bin' \
"$HOME/bin" \
"$HOME/code/log/util" \
"$HOME/src/google-cloud-sdk/bin" \
'/Applications/Docker.app/Contents/Resources/bin' \
'/usr/local/MacGPG2/bin' \
'/Applications/VMware Fusion.app/Contents/Library' \
'/usr/local/Cellar/coreutils/8.26/bin' \
'/opt/improbable/bin' \
'/Library/TeX/texbin' \
'/usr/local/Cellar/openssl/1.0.2m/bin' \
'/Library/Frameworks/Mono.framework/Versions/Current/Commands' \
'/usr/local/munki' \
'/opt/puppetlabs/bin:/Library/TeX/texbin' \
'/improbable/tools/latest/mac' \
'/Users/gavin/bin/gam' \
'/Applications/Visual Studio Code.app/Contents/Resources/app/bin' \
'/opt/puppetlabs/puppet/bin/' \
'/usr/local/munki'
export OLDGAMPATH=/Users/gavin/bin/gam
export GOPATH=/Users/gavin/go
export PATH=$PATH:$GOPATH/bin
export SSH_AUTH_SOCK=${HOME}/.gnupg/S.gpg-agent.ssh
if [[ $OS == Darwin ]]; then
export BROWSER='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
fi
function len() {
echo "${#1}"
}
function join-lines() {
for l in "$@"; do
echo "$l"
done
}
function inlinepy() {
python <(join-lines "$@")
}
alias gia='git add'
alias gib='git branch'
alias gic='git checkout'
alias gil='git log --author=$USER'
alias gip='git pull'
alias gis='git status'
alias gbrm="git branch | grep -v "master" | xargs git branch -D"
alias gbrp="git branch | grep -v "production" | xargs git branch -D"
alias :q="exit"
# shellcheck disable=SC2139
alias rb='. ~/.bashrc'
function scp.() {
local target=$1
shift
(($#)) || set -- code/dot/{bashrc,profile,vimrc,tmux.conf} code/dot/bashrc.d/*
tar -C "$HOME" -czf - "$@" | ssh "$target" '
cd
IFS=$'\''\n'\''
for f in $(tar -xzvf - 2>&1 | sed -nE "s/^x (.*)/\\1/p")
do [[ $f != */bashrc.d/* ]] && ln -vfs "$f" ".${f##*/}"
done'
}
alias gpg-decode='gpg --list-packets --verbose'
OCTET_RE='([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
IP4_RE="($OCTET_RE[.]){3}$OCTET_RE"
HEXD_RE='[0-9A-Fa-f]{1,4}'
IP6_RE="(($HEXD_RE:){7}($HEXD_RE|:))"
IP6_RE="$IP6_RE|(($HEXD_RE:){6}(:$HEXD_RE|:))"
IP6_RE="$IP6_RE|(($HEXD_RE:){5}(((:$HEXD_RE){1,2})|:))"
IP6_RE="$IP6_RE|(($HEXD_RE:){4}(((:$HEXD_RE){1,3})|:))"
IP6_RE="$IP6_RE|(($HEXD_RE:){3}(((:$HEXD_RE){1,4})|:))"
IP6_RE="$IP6_RE|(($HEXD_RE:){2}(((:$HEXD_RE){1,5})|:))"
IP6_RE="$IP6_RE|(($HEXD_RE:){1}(((:$HEXD_RE){1,6})|:))"
IP6_RE="$IP6_RE|(:(((:$HEXD_RE){1,7})|:))"
IP_RE="($IP4_RE)|($IP6_RE)"
LO_RE="127([.]$OCTET_RE){3}"
RFC1918A_RE="10([.]$OCTET_RE){3}"
RFC1918BC_RE="(192[.]168|172[.](1[6-9]|2[0-9]|3[012]))([.]$OCTET_RE){2}"
IP6_INT='2620:[a-fA-F0-9:]+'
LOCAL_IP_RE="($RFC1918A_RE)|($RFC1918BC_RE)|($IP6_INT)"
export IP_RE LO_RE LOCAL_IP_RE
case $OS in
Darwin)
function clipi() { pbcopy --Prefer txt; }
function clipo() { pbpaste; }
;;
Linux)
function clipi() { pbcopy --Prefer txt; }
function clipo() { pbpaste; }
;;
esac
if [[ $OS == Darwin ]]; then
function passdialog() {
local message=${1:-password}
local title=${2:-password}
osascript -e '
display dialog "'"$message"'" ¬
default answer "" ¬
buttons {"Cancel", "OK"} ¬
default button 2 ¬
with hidden answer ¬
with title "'"$title"'"
' | sed -En 's/button returned:OK, text returned:(.*)/\1/p'
}
elif [[ $OS == Linux ]]; then
function passdialog() {
local message=${1:-password}
local title=${2:-password}
zenity --entry --title "$title" --text="$message" --hide-text
}
fi
function ssh-pubkey() {
ssh-keygen -y -f ~/.ssh/id_rsa
}
function bin2str() {
for ((i = 0; i < ${#1}; i++)); do
printf '\\x%0.2x' "'${s:$i:1}"
done
#python -c 'import sys; print "".join(r"\x%0.2x" % ord(c) for c in sys.stdin.read())'
#inlinepy 'import sys' \
# 'print "".join(r"\x%0.2x" % ord(c) for c in sys.stdin.read())'
}
function png-dimensions() {
while (($#)); do
dd if="$1" bs=8 skip=2 count=1 2>/dev/null |
/usr/local/bin/god --endian=big -t u4 |
sed -nE 's/^[0-9]+ +([0-9]+) +([0-9]+)$/\1 x \2/p'
shift
done
}
function dng-time() {
exiftool "$1" | sed -En '/^Date\/Time Original/ s/.*(20..):(..):(..) (..:..:..)$/\1-\2-\3T\4/p'
}
# usage: leica-import [base [db]]
function leica-import() {
local card current db dng first last name total
card="${1:-/Volumes/LEICA M/DCIM/100LEICA}"
db="${2:-/var/photo/leica}"
cd "$card" || return $?
total=$(find "$card" -type f -name '*.DNG' | wc -l)
current=0
echo "TOTAL: $total"
for dng in *.DNG; do
#echo $dng
name=$(dng-time "$dng")
first="${first:-$name}"
if [[ "$name" == "${last:0:19}" ]]; then
if [[ "${last:19}" ]]; then
name="${name}$(printf %X $(("0x${last:19}" + 1)))"
else
name="${name}A"
fi
fi
last="$name"
cp -n "$dng" "${db}/dng/${name}.dng"
[[ -f "${dng%.DNG}.JPG" ]] && cp -n "${dng%.DNG}.JPG" "${db}/jpg/${name}.jpg"
((current++))
printf '\r%5d/%d %3d%%' "$current" "$total" $(((100 * current) / total))
done
#echo "$(date "+%Y-%m-%dT%H:%M:%S") $first --> $name"
}
function unichar2echo() {
echo "echo -e '$(head -1 | tr -d '\n' | hexdump -C | sed -E 's/[0-9]*//; s/[ \t]*[|].*//; s/([0-9a-f]{2})/\\x\1/g' | tr -d ' ')'"
}
function hex-icon() {
local icons="$HOME/code/unicode/icons"
sed -n "$(("0x$1" % "$(wc -l "$icons" | grep -Eo '[0-9]+')" + 1))p" "$icons"
}
function hash-icon() {
hex-icon "$(sha256 "$@" | cut -c60-)"
}
function git-diff-meld() {
git difftool --tool=meld
}
function bandwidth-remote() {
ssh "$1" '{ [[ $(uname) == Darwin ]] && nc -ul 9999 || nc -ulp 9999; } | dd of=/dev/null bs=1048576 count='"${2:-100}"' 2> /dev/null'
}
function bandwidth-client() {
time dd if=/dev/zero bs=1048576 count="${2:-100}" 2> >(grep -o '[0-9]* bytes/sec' >&2) | nc -u "$1" 9999
}
function tftp-server() {
inlinepy \
'import tftpy' \
"tftpy.TftpServer(tftproot='${1:-$PWD}').listen('127.0.0.1', 6969)"
}
function tftp-client() {
{
echo 'mode binary'
echo "get $1"
} | tftp 127.0.0.1 6969
}
function standup() {
local standup today
standup="$HOME/cave/documents/standup.txt"
today=$(date +%Y-%m-%d)
if [[ -e "$standup" ]] && grep -q "^# $today$" "$standup"; then
vim "$standup"
else
{
echo
echo "# $today"
for s in Yesterday Today; do
echo
echo "## $s"
echo -e '\xe2\x80\xa2 '
done
} >>"$standup"
vim -c '$|-5|i' "$standup"
fi
}
# Substitute Mc'Donalds -> Mc'"'"'Donalds
function escape-single() {
sed "s/'/'\"'\"'/g" <<<"$1"
}
# usage:
# ssh-sudo <remote-host> <command>
# example:
# PASS=mickeymouse ssh-sudo myserver useradd goofy
function ssh-sudo() {
local passpipe make_pass_pipe ssh_cmd pass
if [[ "$SUDO_PASS" ]]; then
pass="$SUDO_PASS"
else
pass="$(pass knife)"
fi
passpipe=$'#!/bin/sh\n/bin/cat'
make_pass_pipe="echo '$passpipe' > /tmp/c && chmod 700 /tmp/c && SUDO_ASKPASS=/tmp/c sudo -A ${*:2} && rm /tmp/c"
ssh_cmd="/usr/bin/env bash -c '$(escape-single "$make_pass_pipe")'"
# echo "ssh $1 '$(escape-single "$ssh_cmd")' <<< '$pass'"
# shellcheck disable=SC2029
ssh "$1" "$ssh_cmd" <<<"$pass"
}
# usage: rpc-ports remote.host.io
function rpc-ports() {
rpcinfo "$HOST" | sed -nE '/ (ud|tc)p / s/.* 0[.]0[.]0[.]0[.]([0-9]+)[.]([0-9]+) .*/256*\1+\2/p' | bc | sort -u
}
# usage: make-forwards remote.host.io 111 1111 2049
function make-forwards() {
local -a cmd=(ssh -N "$1")
for p in "${@:2}"; do
cmd=("${cmd[@]}" -R "127.0.0.1:$p:127.0.0.1:$p")
done
echo "${cmd[@]}"
}
function xgrep() {
grep -E "$(sed 's~/|~\|/~' | tr '\n' '|' | sed 's/|$//')" "$@"
}
function date-rename() {
mv "$1" "$(date "+%Y-%m-%d")${2:- }$1"
}
export AGENT_CONNECT="$HOME/.gnupg/agent-connect.sh"
function gpg-agent-restart() {
killall {ssh,gpg}-agent
unset GPG_AGENT_CONNECT SSH_AGENT_PID SSH_AUTH_SOCK
gpg-agent --daemon --enable-ssh-support >"$AGENT_CONNECT"
# shellcheck disable=SC1090
source "$AGENT_CONNECT"
}
function pf1r() { ssh -t [email protected] "$@" 'bash --rcfile /home/sherif/.bashrc -i'; }
function pf2() {
ssh 'pf2' -t -o 'ProxyCommand=ssh impf01.corp.improbable.io nc 172.16.7.253 22' "$@" \
'bash --rcfile /home/sherif/.bashrc -i'
}
function pf2r() {
ssh -l root 'pf2' -t -o 'ProxyCommand=ssh impf01.corp.improbable.io nc 172.16.7.253 22' "$@" \
'bash --rcfile /home/sherif/.bashrc -i'
}
function docker-ip() {
docker inspect "$1" | jq -r '.[]|.NetworkSettings.Networks.separate_default.IPAddress'
}
function docker-ubuntu() {
docker run "$@" -it -v /:/host --privileged --entrypoint /bin/bash ubuntu:17.10
}
function cave-sync() {
local args=(/var/cave/code ssh://station//cave/code)
if [[ "$1" == '-i' ]]; then
args=(-ui graphic -diff meld "${args[@]}")
else
args=(-ui text -batch -auto "${args[@]}")
fi
unison "${args[@]}"
}
function diff-git() {
git diff --no-index "$1" "$2"
}
function vim-leaders() {
grep -Eio '[<]leader[>][^[:space:]]+' ~/.vimrc | sort -u
}
function ping-stats() {
#script=${script/</$'\xe2\x86\x92'}
#script=${script/+/$'\xc2\xb1'}
#sudo ping -q -c 100 -i 0.01 "$1" | sed -nE '$s~^[^=]+= ([0-9.]+)/([0-9.]+)/([0-9.]+)/([0-9.]+) ms$~\1 '$'\xe2\x86\x90'' \2 '$'\xe2\x86\x92'' \3 ''\4 (ms)~p'
sudo ping -q -c 100 -i 0.01 "$1" |
sed -nE '$s~^[^=]+= ([0-9.]+)/([0-9.]+)/([0-9.]+)/([0-9.]+) ms$~\1 < \2 > \3 +\4 (ms)~p' |
unicode-sub # shellcheck disable=SC2120
}
function md5() {
if [[ $OS == Darwin ]]; then
/sbin/md5 <<<"$*"
else
/usr/bin/md5sum <<<"$*" | cut -d' ' -f1
fi
}
function sha256() {
if [[ $OS == Darwin ]]; then
shasum -a256 <<<"$*"
else
sha256sum <<<"$*"
fi | cut -d' ' -f1
}
# usage: _cached <validity> <command>
function cache() {
local file
file="$HOME/.cache/.data.$(sha256 "${*:2}")"
if [[ ! -e "$file" ]]; then
touch -t 198010270420 "$file"
chmod 600 "$file"
fi
if (($(stat -f %m "$file") < $(($(date +%s) - $1)))); then
"${@:2}" >"$file"
fi
cat "$file"
}
function lines2pattern() {
sed 's/|/\|/g ; s/$/|/' | tr -d '\n' | sed 's/|$//'
}
function functions() {
declare -f | grep -E '^[[:alnum:]_-]+ [(][)] $' | sed 's/ () $//'
}
function deps() {
local immediate
immediate=$(declare -f "$1" |
grep -oE "$(functions | lines2pattern)" |
sort -u |
grep -v "^$1$")
# shellcheck disable=SC2086
for f in $immediate; do
echo $f
deps "$f" | sed 's/^/ /'
done
}
function ln-resolve() {
cd "$(dirname "$1")" || return $?
if [[ -L "$1" ]]; then
ln-resolve "$(readlink "$1")"
else
echo "${PWD}/$(basename "$1")"
fi
}
function source-bashrc-d() {
# TODO: permission fs ownership & permissions check. "eval" alert!
pushd "$(ln-resolve "${BASH_SOURCE[0]}").d" >/dev/null 2>&1 || return
# shellcheck disable=SC1090
for s in *.sh; do
source "$s"
done
popd >/dev/null
}
source-bashrc-d
alias gam-adv="/Users/gavin/bin/gamadv-xtd/gam"
# zsh like completion:
bind 'set show-all-if-ambiguous on'
bind 'TAB:menu-complete'
alias goland='/usr/local/bin/goland'
export MICROMDM_ENV_PATH=/Users/gavin/.micromdm/env
eval "$(direnv hook bash)"