-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.functions
More file actions
381 lines (343 loc) · 9.27 KB
/
.functions
File metadata and controls
381 lines (343 loc) · 9.27 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
# .functions
# follow a file using bat
batf() {
tail -f "$1" | bat -p --paging=never
}
# echo in colors
# shellhacks.com/bash-colors/
blue() {
echo -e "\e[34m${1}\e[0m"
}
cyan() {
echo -e "\e[36m${1}\e[0m"
}
green() {
echo -e "\e[32m${1}\e[0m"
}
red() {
echo -e "\e[31m${1}\e[0m"
}
yellow() {
echo -e "\e[33m${1}\e[0m"
}
# backup a file: bkp [file]
# will create [file].[CurrentDateTime].bak in the directory that the file resides in
bkp () {
dt=$(date +%Y%m%dT%H%M%S%z)
echo "copying $1 to $1.$dt.bak"
cp "$1"{,.$dt.bak}
}
# compute an md5sum and compare it with grep: md5check [file] [key]
md5check() {
md5sum "$1" | grep "$2";
}
# compute an sha256sum and compare it with grep: sha256sum [file] [key]
shacheck() {
sha256sum "$1" | grep "$2"
}
# compute a sha256 checksum
sha() {
if [ SYSTEM_TYPE=="macOS" ]; then
shasum -a 256 "$1"
else
sha256sum "$1"
fi
}
dttz() {
# timezone helper
echo -n 'America/Los_Angeles : ' && TZ=America/Los_Angeles date +%FT%T%z
echo -n 'America/Phoenix : ' && TZ=America/Phoenix date +%FT%T%z
echo -n 'America/Denver : ' && TZ=America/Denver date +%FT%T%z
echo -n 'America/Chicago : ' && TZ=America/Chicago date +%FT%T%z
echo -n 'America/New_York : ' && TZ=America/New_York date +%FT%T%z
echo -n 'UTC : ' && TZ=UTC date +%FT%T%z
echo -n 'Europe/London : ' && TZ=Europe/London date +%FT%T%z
echo -n 'Asia/Kolkata (IST) : ' && TZ=Asia/Kolkata date +%FT%T%z
echo
echo -n 'Local : ' && date +%FT%T%z
echo -n 'Epoch(sec) : ' && echo $EPOCHSECONDS
echo -n 'Epoch(milliseconds) : ' && echo $(( ${EPOCHSECONDS} * 1000))
echo -n 'Epoch(microseconds) : ' && echo $(( ${EPOCHSECONDS} * 1000 * 1000))
}
dttz_new() {
timezones=(
"US/Pacific"
"US/Mountain"
"US/Central"
"UTC"
"Europe/London"
"Asia/Kolkota"
)
for tz in "${timezones[@]}"; do
export TZ=$tz && printf "%-24s" "$tz" && echo " : $(date +%FT%Tz)"
done
unset TZ && echo
printf "%-24s" "Local" && echo " : $(date +%FT%Tz)"
printf "%-24s" "Epoch(sec)" && echo " : $(echo $EPOCHSECONDS)"
printf "%-24s" "Epoch(ms)" && echo " : $(echo $(( ${EPOCHSECONDS} * 1000)))"
}
# Create a new directory and enter it
mkd() {
mkdir -p "$@"
cd "$@" || exit
}
# Make a temporary directory and enter it
mkdt() {
local dir
if [ $# -eq 0 ]; then
dir=$(mktemp -d)
else
dir=$(mktemp -d -t "${1}.XXXXXXXXXX")
fi
cd "$dir" || exit
}
# extract any kind of archive: extract [archive file]
extract() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $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
}
# find things case-insensitive: findit stringToSearchFor
findit (){
if [ -z ${1} ];then
echo "Please pass an argument that you want to search for"
else
find . -iname "*$1*" -print
fi
}
# Determine size of a file or total size of a directory
fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh
else
local arg=-sh
fi
if [[ -n "$@" ]]; then
du $arg -- "$@"
else
du $arg -- .[^.]* *
fi
}
docker_python() {
#
# purpose:
# quickly start a python repl in a docker container given a version, tag, and one or more python packages
# usage:
# docker_python <python_ersion> <docker_tag> <python packages>
# example:
# docker_python 3.8.1 my_tag requests pytest
#
echo "python version : $1";
echo "docker image tag name : $2";
echo "python packages : ${@:3}";
echo -e "FROM python:$1-slim
RUN python -m pip install \
--upgrade pip \
${@:3}" > ./Dockerfile
echo -e "\nBuilding image from Dockerfile:\n$(cat ./Dockerfile)\n"
docker build . --tag $2
rm -f Dockerfile
docker container run -it --rm $2
}
start_pyspark() {
#
# purpose:
# simple pyspark starter that ensures necessary environment variables are setup
# and ipython is the default shell
# usage:
# start_pyspark mode /path/to/sparkhome
# mode:
# can be [ps|ss|jl], that is pyspark shell, scala shell, jupyter lab
#
#unalias ipython jupyter-lab
export PYSPARK_DRIVER_PYTHON=$(which $(command jupyter-lab))
export PYSPARK_DRIVER_PYTHON_OPTS="--TerminalInteractiveShell.editing_mode=vi"
export PYSPARK_PYTHON=$(which $(command python))
export SPARK_LOCAL_IP="localhost"
export SPARK_HOME="${SPARK_HOME:-$1}"
$SPARK_HOME/bin/pyspark
}
byte_convert () {
helper=$(cat <<EOT
#
# purpose:
# convert bytes from one unit to another
# usage:
# usage: byte_convert <num> <unit>. Valid units [b|kb|mb|gb|tb|pb|eb
# example:
# byte_convert 123456 gb
#
EOT
)
if [ $# -eq 0 ]; then
echo $helper
fi
text=$(cat <<EOT
def converter(kind: str, num: int):
units = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb']
kind_pstn = units.index(kind)
for unit in units:
pstn = units.index(unit)
offset = abs(kind_pstn - pstn)
if pstn < kind_pstn:
print(f'{int(num * (1024 ** offset)) : <20} : {unit}')
elif pstn > kind_pstn:
print(f'{int(num / (1024 ** offset)) : <20} : {unit}')
else:
print(f'{num : <20} : {unit}')
converter(kind='$2', num=int($1))
EOT
)
echo $text | python3
}
abspath () {
# retrieve absolute path of file - platform independent
echo "$(cd $(dirname "$1");pwd)/$(basename "$1")"
}
function log_msg() {
#
# purpose:
# print a message with a date (utc) and log level to stdout
# usage:
# log_msg "custom message here" # <-- default log level is INFO
# log_msg "custom message here" ERROR # <-- override default log level to ERROR
#
custom_msg=$1
level="INFO"
if [ -n "$2" ]; then
level="$2"
fi
full_msg="$(date -u +%Y-%m-%dT%H:%M:%S.%3N%z) | $level | ${custom_msg}"
echo $full_msg
}
function bluetooth-connect() {
#
# purpose:
# connect to a bluetooth device
# usage:
# bluetooth-connect deviceName
#
bluetoothctl connect $(bluetoothctl devices | grep -i $1 | awk '{print $2}')
}
function bluetooth-disconnect() {
#
# purpose:
# disconnect from a bluetooth device
# usage:
# bluetooth-disconnect deviceName
#
bluetoothctl disconnect $(bluetoothctl devices | grep -i $1 | awk '{print $2}')
}
function bluetooth-off() {
bluetoothctl power off
if [ $? -ne 0 ]; then
rfkill block bluetooth
rfkill unblock bluetooth
fi
bluetoothctl power off
}
function bluetooth-on() {
bluetoothctl power on
if [ $? -ne 0 ]; then
rfkill block bluetooth
rfkill unblock bluetooth
bluetoothctl power on
fi
}
function wifi-connect() {
#
# purpose:
# quickly connect to a wifi network
# usage:
# wifi-connect networkName networkPass
#
if [[ "${OS_FAMILY}" == "MacOS" ]]; then
networksetup -setairportnetwork en0 $1 $2
elif [[ "${OS_FAMILY}" == Linux* ]]; then
nmcli dev wifi rescan
nmcli dev wifi connect $1 password $2
fi
}
function dns-show() {
#
# purpose:
# show dns ipv4 and ipv6 hostnames
# usage:
# dns-show <connectionNameOrUUID>
#
nmcli device show $1 | grep -i dns
}
function aws-assume-role() {
#
# purpose:
# assume an aws iam role using the role arn
# usage:
# aws-assume-role roleArn
# example:
# aws-assume-role arn:aws:iam::123456789012:role/role-to-be-assumed
#
export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \
$(aws sts assume-role \
--role-arn $1 \
--role-session-name my-session \
--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \
--output text))
}
function pyinfo() {
#
# purpose:
# view environmental python info
# usage:
# pyinfo
#
echo "python version : $(python --version)"
echo "which python : $(which python)"
echo "which pip : $(which pip)"
command -v pyenv > /dev/null
if [ $? -eq 0 ];
then echo "pyenv version : $(pyenv version)"
fi
}
function git-diff-file() {
helper=$(cat <<EOT
#
# purpose:
# git diff a file across 2 branches by executing `git diff branch1:filename branch2:filename`
# usage:
# gitdiff-file filename branch1 branch2
#
EOT
)
if [ $# -eq 0 ]; then
echo $helper
return 1
fi
echo "executing: git diff $2:$1 $3:$1"
git diff $2:$1 $3:$1
}
function notification() {
#
# purpose:
# send a simple notification via osascript
# usage:
# notification "hello world"
#
if [ SYSTEM_TYPE=="macOS" ]; then
X="$1" /usr/bin/osascript -e "display notification \"$1\" with title \"my notification\" sound name \"Submarine\""
fi
}