-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash.functions
246 lines (207 loc) · 4.57 KB
/
bash.functions
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
# vi: ft=sh
OS=`uname -s`
export OS
# functions
strlen()
{
str=$1
echo ${#str}
}
rand_str()
{
local len=$1
if [ -z "$len" ] ; then
len=10
fi
cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c $len
}
prefix()
{
sed -e "s/^/$1: /"
}
rand_no()
{
local ceil=10
if [ ! -z $1 ] ; then
ceil=$1
fi
local myrand=$(($RANDOM % $ceil))
printf "%d\n" $myrand
return $myrand
}
branches()
{
git for-each-ref --format='%(committerdate) %(authorname) %(refname)' | sort -k5n -k2M -k3n -k4n | column -t -s' '
}
# ascii conversion array
ascii_array=(nul soh stx etx eot enq ack bel bs ht nl vt np cr so si dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us sp \! \" \# \$ \% \& \' \( \) \* + , - . / 0 1 2 3 4 5 6 7 8 9 : \; \< = \> \? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \[ \\ \] ^ _ \` a b c d e f g h i j k l m n o p q r s t u v w x y z \{ \| \} ~ del)
itoa()
{
echo ${ascii_array[$1]}
}
k()
{
echo "$1*1024" | bc
}
m()
{
echo "$1*1024^2" | bc
}
g()
{
echo "$1*1024^3" | bc
}
export -f itoa rand_str strlen rand_no k m g
epochtime()
{
if [ -t 0 ] ; then
seconds="$1"
else
seconds=$(cat)
fi
if [ -z "$seconds" ] ; then
echo "Usage: $0 <epoch_seconds>"
echo " echo <epoch_seconds> | $0"
return 1
fi
case "$OS" in
Darwin)
date -r "$seconds"
;;
Linux)
date --date "@$seconds" --utc
;;
*)
echo "Unknown OS: \"$OS\""
;;
esac
}
now()
{
if [ $# -gt 0 ] ; then
ARGS=$@
date -u +"%Y-%m-%dT%TZ" -d "$ARGS"
else
date -u +"%Y-%m-%dT%TZ"
fi
}
export -f epochtime now
grepN()
{
lines=$1
if [ -z "$2" ] ; then
echo "Need a pattern"
return 1
fi
pattern="$2"
(head -n$lines ; tail -n +$lines | grep $pattern)
}
grep1()
{
grepN 1 "$@"
}
grep2()
{
grepN 2 "$@"
}
grep3()
{
grepN 3 "$@"
}
grep4()
{
grepN 4 "$@"
}
export -f grep{1..4}
# cdexec (PROMPT_COMMAND)
__cdexec()
{
if [ -x .cdexec ] ; then
file=`realpath .cdexec`
if [ `stat -c %A $file | cut -c6` == "-" ] && \
[ `stat -c %A $file | cut -c9` == "-" ] && \
[ `stat -c %U $file` == $USER ] ; then
. .cdexec
fi
fi
}
ht()
{
sval="$1"
if [ -z "$sval" ] ; then
echo "need regex to search"
return 1
fi
grep --no-filename "$sval" "$HISTDIR/today.log" | cut -d' ' -f7- | sort | uniq
}
hs()
{
sval="$1"
if [ -z "$sval" ] ; then
echo "need regex to search"
return 1
fi
grep --no-filename "$sval" "$HISTDIR/"* | cut -d' ' -f7- | sort | uniq
}
# PROMPT_COMMAND helpers
__write_history()
{
LAST_EXIT_STATUS=$?
[ -d "$HISTDIR" ] || mkdir "$HISTDIR"
printf "%s %s %q\n" $(hostname -s) $(pwd) $(history 1) >> $HISTDIR/bash_history_$(date "+%Y-%m-%d").log
ln -sf $HISTDIR/bash_history_$(date "+%Y-%m-%d").log ~/.history/today.log
}
__set_vars()
{
GIT_BRANCH=""
GIT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)
LAST_OUTPUT_EPOCH_SECONDS=0
ABBREV_DIRS=$(p="${PWD#${HOME}}"; [ "${PWD}" != "${p}" ] && printf "~";IFS=/; for q in ${p:1}; do printf /${q:0:1}; done; printf "${q:1}")
}
__set_it2_tb()
{
IT2TB="$HOME/.iterm2/it2setkeylabel"
if [ -x $IT2TB ] ; then
if [ -z "$GIT_BRANCH" ] ; then
TB_GIT_BRANCH=""
else
TB_GIT_BRANCH="⌥ $GIT_BRANCH"
fi
if [ ${#PWD} -gt 40 ] ; then
TB_CWD="🧭 $ABBREV_DIRS"
else
TB_CWD="🧭 $PWD"
fi
if [ "$LAST_EXIT_STATUS" == "0" ] ; then
TB_LAST=""
else
TB_LAST="(╯°□°)╯︵ ┻━┻"
#TB_LAST="ಠ__ಠ"
fi
if [ $(date +"%H") -gt 12 ]; then
TB_BEV="🍺"
else
TB_BEV="☕️"
fi
$IT2TB set status "$TB_BEV $TB_GIT_BRANCH $TB_CWD $TB_LAST"
fi
}
export ABBREV_DIRS LAST_EXIT_STATUS LAST_OUTPUT_EPOCH_SECONDS
export -f __write_history __set_vars __set_it2_tb
# Kubernetes
rename_gui_title() {
echo -ne "\033]0;"$@"\007"
}
# AWS
get_aws_creds() {
echo "AWS_DEFAULT_PROFILE: $AWS_DEFAULT_PROFILE"
echo "AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID"
echo "AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY"
echo "AWS_SESSION_TOKEN: $AWS_SESSION_TOKEN"
}
clear_aws_creds() {
unset AWS_DEFAULT_PROFILE
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
}