-
Notifications
You must be signed in to change notification settings - Fork 0
/
.personal_bash_aliases
354 lines (303 loc) · 10.3 KB
/
.personal_bash_aliases
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
#!/bin/bash
# Personal bash aliases and utility functions
# These are for use on development machines,
# e.g. not production machines or machines with important credentials
# Anything that could be potentially dangerous (as many Linux commands are)
# has a comment stating: "Use at your own risk!"
# Usually these will require a sudo prompt, but not always.
# I do my best to explain what could go wrong, but it may not be comprehensive.
# I denote main sections with [Section Name]
# Current sections are:
# [String Manipulation]
# [General Utilities]
# [Navigation Utilities]
# [Disk Utilities]
# [Process Monitoring]
# [Networking]
# [Kubernetes]
# To utilize this, add the following lines to ~/.bashrc:
# if [ -f ~/.personal_bash_aliases ]; then
# source ~/.personal_bash_aliases
# fi
# Dependencies (not needed unless you use the corresponding aliases)
# Not necessarily a comprehensive list - depends on your system
# [orpie](https://github.com/pelzlpj/orpie)
# [xsel](https://github.com/kfish/xsel)
# [lsscsi](https://github.com/doug-gilbert/lsscsi)
# [neovim](https://github.com/neovim/neovim)
# [tmux](https://github.com/tmux/tmux)
# [lazygit](https://github.com/jesseduffield/lazygit)
# [String Manipulation]
# Debug function for checking input and output sources
# I mainly use this as a reference
function debug_check_io {
[[ -t 1 ]] && echo 'shell is tty'
[[ -p /dev/stdout ]] && echo 'stdout is pipe'
[[ ! -t 1 && ! -p /dev/stdout ]] && echo 'stdout is redirect'
[[ -f /dev/stdin ]] && echo 'stdin is file'
[[ -p /dev/stdin ]] && echo 'stdin is pipe'
}
# Remove whitespace from a string
function strip_whitespace {
# Quoting input to sed breaks everything
# shellcheck disable=SC2086
sed 's/^[[:space:]]*//;s/[[:space:]]*$//' $1
}
# Remove newlines from a string
function strip_newlines {
# Quoting input to tr breaks everything
# shellcheck disable=SC2086
tr -d '\n' $1
}
# Wrap the input in double quotes
# If input comes from a pipe, wrap the whole contents
# Newlines get removed if reading from a pipe
function doublequote {
if [ "$#" -gt 0 ]; then
# Normal Args
printf %s \""$*"\"
elif [[ -p /dev/stdin ]]; then
# Input from pipe
local line
printf '"'
while IFS= read -r line; do
printf %s "$line"
done
printf '"'
fi
}
# Given a list of arguments,
# produce a regex that matches a string containing all of them, in any order.
# It accomplishes matching in "any order" by matching each argument with positive lookahead
# Use at your own risk! Many cases of positive lookahead can make a really slow regex.
# Don't use this to blindly generate a regex for a production environment.
function include_list_regex {
local ret_regex=""
for arg in "$@"; do
ret_regex+="(?=.${arg})"
done
echo "$ret_regex"
}
# [General Utilities]
# Interfacing with ~/.bashrc
alias sbrc='source $HOME/.bashrc'
alias vbrc='nvim $HOME/.bashrc'
alias cbrc='cat $HOME/.bashrc'
# Source tmux configuration
alias tmux-source='tmux source $HOME/.tmux.conf'
# Clear screen
alias cls='clear'
# Cron Utilities (assumes cron logs go to /var/mail)
alias cronlog='grep CRON /var/log/syslog'
alias catmail='cat /var/mail/$USER'
alias catmail-recent='tail -n 30 /var/mail/$USER'
alias tailmail='tail -f /var/mail/$USER'
# Reverse Polish Notation Calculator
alias rpn-calc='orpie'
alias rpn='orpie'
# Lazygit
alias lg='lazygit'
# Get the last command executed
alias last_cmd='fc -ln -1 | strip_whitespace'
# Copy the last command
alias cpl='last_cmd | strip_newlines | xsel --clipboard'
# Shortcut for clipboard, usually I pipe to this
alias clip='xclip -selection clipboard'
# Clip the working directory
alias clipwd='pwd | clip'
alias cwd='pwd | clip'
# Run a command with `sudo`, attempting to preserve aliases
# Use at your own risk! Existing commands can be overwritten by aliases,
# Thus anything ran this way may work differently than expected.
# This could also be a security risk if a non-sudo user has access
# to the sourced `.bashrc`
function sudo_with_aliases {
sudo bash --rcfile "$HOME"/.bashrc -ci "$1"
}
# [Navigation Utilities]
# Go to the local bin
alias cdbin='cd $HOME/.local/bin'
# Change into the first directory from a parent directory
# If a path isn't passed, it uses the current directory as the working directory
function cd_to_first_directory {
local target_dir
if [ "$#" == 0 ]; then
target_dir=$(find . -maxdepth 1 -mindepth 1 -type d | sort | head -n 1)
else
target_dir=$(find "$1" -maxdepth 1 -mindepth 1 -type d | sort | head -n 1)
fi
cd "$target_dir" || return
}
alias cdf='cd_to_first_directory'
# Change into a directory with an index
# If there's not a directory at the index, it just returns
function cd_to_directory_with_index {
local directory_at_index
directory_at_index=$(find . -maxdepth 1 -mindepth 1 -type d | sort | sed -n "${1}p")
cd "$directory_at_index" || return
}
alias cdi="cd_to_directory_with_index"
# "smart" change directory
# Usage: smart_cd [additional-search-paths] [target]
# or: smart_cd [target]
# It will search an array of directories for the target directory
# As soon as a match is found, this will execute the "cd"
# If a PATH-like variable is passed (':'-delimited paths),
# All paths will be added to the search dirs
# TODO (joshua-dean): This should be refactored into it's own script with proper arg parsing
# TODO (joshua-dean): Tab completion
function smart_cd {
local smart_search_dirs=(
"$HOME"
"$HOME/Documents"
)
local target
if [ "$#" == 0 ]; then
echo "No arguments passed."
return
elif [ "$#" == 1 ]; then
target="$1"
elif [ "$#" -ge 2 ]; then
# If a path-like variable is passed, parse and add it to the search dirs
# '#' gets the length, '-t' ensures we remove the delimiter
readarray -d ':' -O "${#smart_search_dirs[@]}" -t smart_search_dirs <<<"$1"
target="$2"
fi
for dir in "${smart_search_dirs[@]}"; do
if [ -d "$dir/$target" ]; then
cd "$dir/$target" || return
return
fi
done
# If we've gotten here, the directory doesn't exist elsewhere
# so we can try to "cd" anyways (and let it fail)
# shellcheck disable=SC2164
cd "$target"
}
alias scd='smart_cd'
# Change directory to ".." n times
# Usage: cd_dot_dot [n]
# Defaults to one if nothing is passed
function cd_dot_dot {
if [ "$#" == 0 ]; then
cd ..
else
# Construct a single target so that "cd -" still works
local cd_str=''
for _ in $(seq 1 "$1"); do
cd_str+='../'
done
cd "$cd_str" || return
fi
}
alias cdd='cd_dot_dot'
# "change directory up"
alias cdu='cd_dot_dot'
# "drill" down through single directories
# If there is only one subdirectory in the current directory,
# Repeat the search in the subdirectory.
# This constructs a single "cd" command so that "cd -" still works
function drill {
local current_dir='.'
local find_result
while true; do
find_result=$(find "$current_dir" -maxdepth 1 -mindepth 1 -type d)
if [ -z "$find_result" ] || [ "$(echo "$find_result" | wc -l)" -ne 1 ]; then
break
fi
current_dir="$find_result"
done
if [ "$current_dir" != '.' ]; then
cd "$current_dir" || return
fi
}
# [Disk Utilities]
# Disk usage summary, human readable
alias dush='du -sh *'
alias dush-sorted='du -sh * | sort -h'
alias dushs='dush-sorted'
# Disconnect and spin down a HDD
# Use at your own risk!
# Usage: `disconnect_hdd sdX`
function disconnect_hdd {
local drive
drive="$1"
sudo umount /dev/"$drive"
sudo hdparm -Y /dev/"$drive"
sudo bash -c "echo 1 > /sys/block/$drive/device/delete"
}
# Scans SCSI hosts, but only if they are inactive
# Checks /sys/class/scsi_host for available hosts,
# and sees if the hosts show up in `lsscsi`
# Use at your own risk! The parsing here could definitely go belly-up
# If you scan an active device, this could interrupt reads/writes
function scan_inactive_scsi_hosts {
local host_ids
local raw_scsi_hosts
raw_scsi_hosts=$(find /sys/class/scsi_host -maxdepth 1 -mindepth 1)
host_ids=$(echo "$raw_scsi_hosts" | sed -E 's/(.+)host([0-9]+)/\2/g')
for host_idx in $host_ids; do
if [ -z "$(lsscsi -i "$host_idx")" ]; then
sudo bash -c "echo '- - -' > /sys/class/scsi_host/host$host_idx/scan"
fi
done
}
# [Process Monitoring]
# awk to pull PID from `ps` (pipe to this, generally)
# e.g. `ps <some-heinous-condition> | awk_pid`
# For simple calls, just use `pidof`
function awk_pid {
awk '{ print $2 }'
}
# "Ninja" meaning the grep won't return itself, primarily for process listing
function ninjagrep {
grep "$*" | grep -v grep
}
# Find processes by name (with pgrep), then output full-format info
# 'ps' won't work if I surround the $(...) with quotes,
# so I disabled that rule for now.
function ps_full_format_by_name {
# shellcheck disable=SC2046
ps -fp $(pgrep "$1")
}
alias psf='ps_full_format_by_name'
# [Networking]
# Get the public IP via cloudflare
alias whoami-cloudflare="dig +short txt ch whoami.cloudflare @1.0.0.1"
# [Kubernetes]
# The general structure I followed during development
# for configuration files was:
# First line was a comment with a brief description
# Second line was a comment containing a command to apply/execute the file
# This isn't good for production, but was great for development
# Use at your own risk! Running this on untrusted input could be dangerous
# Get the "kubectl" command from a configuration file
function get_kube_command {
# shellcheck disable=SC2002
cat "$1" | sed -n '2p' "$1" | cut -c3-
}
alias gkc='get_kube_command'
# Run a "kubectl" command from a configuration file
function run_kube_command {
eval "$(get_kube_command "$1")"
}
alias rkc='run_kube_command'
# Delete a "kubectl" command
# pulls the command from the file and replaces "apply" with "delete"
function delete_kube_command {
local rkc_cmd
rkc_cmd=$(get_kube_command "$1")
if [[ "$rkc_cmd" == *"apply"* ]]; then
eval "${rkc_cmd//apply/delete}"
else
echo "No 'apply' command found in file."
fi
}
alias dkc='delete_kube_command'
# Reload a "kubectl" command
# Deletion and re-application
function reload_kube_command {
delete_kube_command "$1"
run_kube_command "$1"
}
alias rlkc='reload_kube_command'