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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
# Copyright 2025 Mitchell Riedstra
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
setaliases() {
alias ls="ls -F";
alias ll="ls -lhF";
alias g="grep";
alias m="more";
alias j="jobs";
alias p="pwd";
alias rot13="tr 'A-Za-z' 'N-ZA-Mn-za-m'"
alias setxkbmap-list="man xkeyboard-config"
# docker-getip $container_id
[ -x "$(command -v docker)" ] && \
alias docker-getip="docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'"
[ -z "$(command -v gdb)" ] && alias gdb='gdb -q'
#shellcheck disable=SC2139
[ -x "$(command -v python3)" ] && alias python="$(command -v python3) -q"
if [ "$(uname)" = "Linux" ] ; then
alias bc='bc -q'
fi
}
setcoloraliases() {
if [ "$(uname)" = "Linux" ] ; then
alias ls="ls --color=auto -F";
alias lm='ls -lh --color=force | more';
elif [ "$(uname)" = "FreeBSD" ] ; then
alias ls="ls -FG";
alias lm='CLICOLOR_FORCE="1" ls -l | less -r';
fi
if echo "color test" | grep -q --color=auto "color test" >/dev/null 2>&1 ; then
alias grep="grep --color=auto";
alias egrep='grep -E --color=auto'
alias search='grep -E --color=auto -rnI';
fi
#shellcheck disable=SC2139
alias diff="$(command -v diff colordiff 2>/dev/null | tail -n 1)";
alias t='tree -CdL'
}
setsudoaliases() {
# Prefer doas if available, as it's likely to be configured by \
# me over sudo
_sudo="$(command -v sudo 2>/dev/null | sed 1q)"
_doas="$(command -v doas 2>/dev/null | sed 1q)"
if [ -n "$_doas" ] ; then
_sudo="$_doas"
fi
_cmds="apt
apt-get
apt-file
apk
yum
dnf
pacman
xbps-install
xbps-remove
pkg_add
pkg_delete
pkg
pkgin
tlp
tlp-stat
zfs
zpool
btrfs
mount
umount"
# Only add aliases for commands that actually exist on our system
if [ -n "$_sudo" ] ; then
for _c in $_cmds ; do
[ -n "$(command -v "$_c")" ] && \
eval "alias $_c=\"$_sudo $_c\""
done
fi
case $_sudo in
*doas*) alias sudo='doas'; ;;
esac
}
# E.g. after `.kshrc` is called, perhaps in `~/.kshrc.local`
# export GOPATH=$HOME/code/gopath
# export GOROOT=$HOME/go/1.13.5
# set_go
set_go() {
export GOPATH="${GOPATH:-$HOME/go}"
path_preappend "$GOPATH/bin"
GOROOT="${GOROOT:-/usr/local/go}"
_gobin="${GOROOT}/bin"
path_preappend "$_gobin"
}
_set_editor() {
export EDITOR="$1"
export VISUAL="$1"
#shellcheck disable=SC2139
alias vi="$EDITOR"
#shellcheck disable=SC2139
alias vim="$EDITOR"
#shellcheck disable=SC2139
alias edit="$EDITOR"
#shellcheck disable=SC2139
alias e="$EDITOR"
}
set_editor() {
for editor in nvim vim vi ed ; do
unalias "$editor" >/dev/null 2>&1
e="$(command -v "$editor")"
if [ -n "$e" ] && [ -x "$e" ] ; then
_set_editor "$e"
fi
break
done
}
set_pager() {
#shellcheck disable=SC2155
export PAGER="$(command -v cat more less 2>/dev/null | tail -n1)"
}
set_lang() {
[ -z "$1" ] && return
export CHARSET="$1";
export LANG="$1";
}
set_ksh_competion() {
if [ -z "$KSH_VERSION" ] ; then return ; fi
if [ -r ~/.passsword-store ] ; then
#shellcheck disable=SC2046
set -A complete_pass -- show find otp grep \
insert edit generate rm mv cp \
$(showPasswordStore ~/.password-store)
#shellcheck disable=SC2046
set -A complete_dpw_1 -- \
show copy type list ls mv cp rm edit otp fnd find insert
set -A complete_dpw_2 -- $(dpw list)
#shellcheck disable=SC2046
set -A complete_passenv -- \
$(showPasswordStore ~/.password-store)
#shellcheck disable=SC2046
set -A complete_dpwenv -- \
$(showPasswordStore ~/.password-store)
fi
#shellcheck disable=SC2046
set -A complete_ssh -- -L -D -A -X \
$(showsshhosts | awk '{print $1}')
#shellcheck disable=SC2046
set -A complete_rsync -- $(showsshhosts | awk '{print $1}')
set -A complete_setPrompt -- date time basic
}
# Usage debugstarttls $ipaddr:$port
debugstarttls() {
openssl s_client -starttls smtp -crlf -connect "$1" "$2"
}
randmacgen() {
if [ "$(uname)" = "FreeBSD" ] ; then
LC_COLLATE=c
fi
tr -c -d '0123456789abcdef' < /dev/urandom \
| head -c 12 \
| sed 's!^M$!!;s!\-!!g;s!\.!!g;s!\(..\)!\1:!g;s!:$!!'
echo ""
}
# Add a given path to the start of your $PATH if it doesn't already
# exist
path_preappend() {
if [ -z "$1" ] ; then echo "need something to preappend" ; return ; fi
pth="$1"; shift
if ! echo "$PATH" | grep -qF "$pth" ; then
export PATH="$pth:$PATH"
fi
}
path_append() {
if [ -z "$1" ] ; then echo "need something to append" ; return ; fi
pth="$1"; shift
if ! echo "$PATH" | grep -qF "$pth" ; then
export PATH="${PATH}:$pth"
fi
}
path_list() {
echo "$PATH" | tr ':' '\n'
}
set_nocaps() {
setxkbmap -option ctrl:nocaps
setxkbmap -option shift:breaks_caps
setxkbmap -option numpad:microsoft
}
# Usage: dir_oct:file_oct path
setperms() {
_perm="$1"; shift
dir_perms=$(echo "$_perm" | awk -F: '{print $1}')
file_perms=$(echo "$_perm" | awk -F: '{print $2}')
find "$@" -type f -print0 | xargs -0 chmod "$file_perms"
find "$@" -type d -print0 | xargs -0 chmod "$dir_perms"
}
pwgenForBanks() {
set -x
pwgen -r '[-#{};[:"'"'"'^()?><.|\/,`=]' -ncsy "$@"
set +x
}
# dump out the exit code of the last process in the from of <code>:
# if not zero
nzret() {
_ret=$?
[ -n "${_ret##0}" ] && echo "${_ret}:"
}
setPrompt() {
case $1 in
date) export PS1='$(date "+%m.%d %H:%M") `nzret`\$ ' ;;
time) export PS1='$(date "+%H:%M") `nzret`\$ ' ;;
basic) export PS1='`nzret`\$ ' ;;
esac
}
# Pull a list of all our SSH hostnames out of the config and
# dump them to stdout
showsshhosts() {
cat ~/.ssh/config ~/.ssh/inc/* 2>/dev/null | awk '/^[hH]ost /{$1=""; print $0}'
}
showPasswordStore() {
_passDir="$1" ; shift
find "$_passDir" -type f -iname '*.gpg' \
| while read -r _line; do
_line="${_line##"$_passDir"}"
_line="${_line#/}"
_line="${_line%.gpg}"
echo "$_line"
done
unset _passDir
}
set_title() {
printf $'\033]0;'"%s"$'\007' "$1"
}
set_my_title() {
_load="Load Avg: $(_sys_load)"
_free_mem="M Free: $(_sys_memory)"
_uptime="Uptime: $(_sys_uptime)"
set_title "$(id -un)[$(hostname)] -- $_load $_free_mem $_uptime"
}
_sys_memory() {
! [ -e "$(command -v vmstat)" ] && return
if [ "$(uname)" = "Linux" ] ; then
vmstat | tail -n1 | awk '{print $4/1024}' | sed -e's/\..*$//g'
elif [ "$(uname)" = "FreeBSD" ] ; then
vmstat | tail -n1 | awk '{print $5/1024}' | sed -e's/\..*$//g'
elif [ "$(uname)" = "OpenBSD" ] ; then
vmstat | sed -n '$p' | awk '{print $4}' | sed 's/M$//'
fi
}
_sys_load() {
# Works on BSD too, neat.
uptime | sed -E 's/^.*load averages?: ([0-9]+\.[0-9]+).*$/\1/g'
}
_sys_uptime() {
# Took awhile to figure out, but this works on BSD as well
uptime | sed -E -e's/^.*up[ ]+//g' -e's/^([^,]*),.*/\1/g' \
-e's/^([^ ]+)[ ]*(.).*/\1\2/g'
}
timestamp() {
date +%m.%d.%y_%H.%M.%S
}
sshSetAskpass() {
export SSH_ASKPASS="$1"
export SSH_ASKPASS_REQUIRE=force
}
# checkSSHAgentForward is useful for if we're an active SSH connection and setup
# the `agent_conf` for it. this can be dropped into your ~/.kshrc.local on a
# remote server to allow your remote sessions to pick up on forwarded keys,
# useful for jump boxes, remote development and similar.
checkSSHAgentForward() {
if ! [ -d "$HOME"/.ssh ] ; then
mkdir -p "$HOME"/.ssh
chmod 600 "$HOME"/.ssh
fi
if [ -z "$SSH_AGENT_PID" ] \
&& [ -n "$SSH_CONNECTION" ] \
&& [ -z "$TMUX" ] ; then
echo "SSH_AUTH_SOCK=\"$SSH_AUTH_SOCK\"; export SSH_AUTH_SOCK" \
> "$HOME"/.ssh/agent_conf
elif [ -n "$TMUX" ]; then
. "$HOME"/.ssh/agent_conf
fi
}
# checkSSHAgent is my go-to agent setup, it's aggressive, non-standard and a
# little hacky, but way more reliable at setting up a persistent ssh-agent than
# doing something at login and hoping everything else inherits it
# usage is fairly straightforward, drop it into your `~/.kshrc.local` and
# let it do it's thing.
#shellcheck disable=SC2120
checkSSHAgent() {
if [ "$1" = "-k" ] ; then
pkill -9 ssh-agent
fi
# So OpenSSH 10.1 steps on my old configuration, rather than passing in -T
# which doesn't exist on older SSH, simply move my configuration out of the
# way.
if [ -f "$HOME/.ssh/agent" ] ; then
rm "$HOME/.ssh/agent"
fi
ssh_agent_conf="$HOME/.ssh/agent_conf"
if [ -e "$ssh_agent_conf" ] ; then
#shellcheck disable=SC1090
. "$ssh_agent_conf"
fi
#shellcheck disable=SC2009
if ! ps aux | awk '{print $2}' | grep -q "$SSH_AGENT_PID" \
|| ! [ -e "$ssh_agent_conf" ] \
|| [ -z "$SSH_AGENT_PID" ] ; \
then
ssh-agent -s | grep -v echo > "$ssh_agent_conf"
#shellcheck disable=SC1090
. "$ssh_agent_conf"
fi
}
# checkSSHAgentGPG sets up SSH to talk to GPG for its keys instead
checkSSHAgentGPG() {
#shellcheck disable=SC2155
export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
}
# calls pass and dumps the contents of the password into your environment
passenv() {
_f="$(mktemp)"
pass "$@" > "$_f"
#shellcheck disable=SC1090
. "$_f"
rm -f "$_f"
}
# calls dpw ( https://git.riedstra.dev/mitch/dpw ) and dumps the contents of the
# password into your environment
dpwenv() {
_f="$(mktemp)"
dpw "$@" > "$_f"
#shellcheck disable=SC1090
. "$_f"
rm -f "$_f"
}
# gensubids is useful for generating /etc/subuid and /etc/subgid
# if you have say a 3rd user you want to add
gensubids() {
_step=65536
_err=0
_un=
_n=
while [ $# -gt 0 ] ; do case $1 in
-u) _un="$2"; shift ; shift ;;
-n) _n="$2"; shift ; shift ;;
*)
echo "unknown option: $1";
echo "gensubids -u <username> -n <entry_number>";
return 1 ;;
esac ; done
if [ -z "$_n" ] ; then
echo "need a number"
_err=1
fi
if [ -z "$_un" ] ; then
echo "need a username"
_err=1
fi
if [ "$_err" -eq 1 ] ; then
return 1;
fi
echo "$_un:$(( (_n*_step) + 1 )):$(( (_n*_step) + _step ))"
}
# scangit is useful for checking which repositories you've modified
scangit() {
verbose=false
ref="origin/master"
while [ $# -gt 0 ] ; do case $1 in
-v) verbose=true ; shift ;;
-r) ref="$2"; shift ; shift ;;
--) break ;;
*) echo "bad option '$1' usage: scangit [-v] [-r <ref>] -- <paths>" ; return ;;
esac ; done
find "$@" -iname '.git' -type d 2>/dev/null \
| while read -r _d ; do
$verbose && echo "Checking... $(dirname "$_d") against ref \"$ref\""
cd "$(dirname "$_d")"
if $verbose ; then
git diff-index --quiet "$ref" 2>/dev/null \
|| echo "Has changes"
else
git diff-index --quiet "$ref" 2>/dev/null \
|| echo "$(dirname "$_d")"
fi
cd - >/dev/null
done
}
gitdate() {
date=""
while [ $# -gt 0 ] ; do case $1 in
-d) date="$2" ; shift ; shift ;;
*) printf "Bad option: $1... usage:\n$0 -d \"\$date_string\"\n" ; return ;;
esac ; done
date="$(date --date "$date")"
export GIT_AUTHOR_DATE="$date"
export GIT_COMMITTER_DATE="$date"
echo export GIT_AUTHOR_DATE="$date"
echo export GIT_COMMITTER_DATE="$date"
}
gitdateUnset() {
unset GIT_AUTHOR_DATE
unset GIT_COMMITTER_DATE
}
gpgTmpDir() {
__privdir='/dev/shm'
if [ "$(uname)" != "Linux" ] ; then
echo "Warning /tmp may not be a ramdisk"
__privdir="/tmp"
fi
set -x
if echo "$GNUPGHOME" | grep -q "$__privdir"; then
rm -rvf "$GNUPGHOME"
fi
#shellcheck disable=SC2155
export GNUPGHOME="$(mktemp -d "$__privdir"/GPG.XXXXXX)"
set +x
}
setcolorterminal() {
case $TERM in
xterm*)
export TERM=xterm-256color
;;
screen*)
export TERM=screen-256color
;;
esac
}
setlscolors() {
if [ "$(uname)" = "Linux" ] ; then
export LS_COLORS='di=36;40:ln=1;31;40:so=32;40:pi=33;40:ex=1;32;40:bd=34;46:cd=34;43:su=0;41:sg=0;46:tw=0;42:ow=0;43:'
elif [ "$(uname)" = "FreeBSD" ] ; then
export LSCOLORS='gxBxcxdxCxegedabagacad'
fi
}
colormanpages() {
export LESS_TERMCAP_mb="[01;33m"
export LESS_TERMCAP_md="[01;33m"
export LESS_TERMCAP_me="[0m"
export LESS_TERMCAP_se="[0m"
export LESS_TERMCAP_so="[01;44;33m"
export LESS_TERMCAP_ue="[0m"
export LESS_TERMCAP_us="[01;36m"
## Gentoo is nuts
export GROFF_NO_SGR=1
}
pkg_switch_branch() {
_f="/etc/pkg/FreeBSD.conf"
cp $_f ${_f}.orig
sed -e's/quarterly/latest/' < ${_f}.orig > ${_f}
}
SHELL_CONFIG=".kshrc"
COLOR=0
case "$TERM" in
xterm*) COLOR=1;;
*256color) COLOR=1;;
esac
path_preappend "$HOME/bin"
PRELOAD="$HOME/${SHELL_CONFIG}.pre"
LOCAL="$HOME/${SHELL_CONFIG}.local"
# Don't put any variables below this, as they can't be overridden before the
# functions if you do that
# Load pre config
[ -r "$PRELOAD" ] && . "$PRELOAD"
set_pager
# note that the use of set_editor and _set_editor on oksh may
# change the input mode. ( set -o emacs ) after to go back
set_editor # Also, opinionated, hope you like vim/vi
setaliases
setsudoaliases
# Support various different shells
if [ -n "$YASH_VERSION" ] ; then
echo "Intentionally left blank" >/dev/null 2>&1
elif [ -n "$KSH_VERSION" ] ; then
HISTFILE="$HOME/.ksh_history"
HISTSIZE=10000
bind ^L=clear-screen
set -o emacs
set_ksh_competion
# if you want PWD
# export PS1='`nzret`\u@\h \$PWD \$ '
export PS1='`nzret`\u@\h\$ '
if [ "$(id -u)" -eq 0 ] ; then
export PS1='`nzret`\h\$ '
fi
elif [ "$SHELL" = "/bin/sh" ] && [ "$(uname)" = "FreeBSD" ] ; then
set -o emacs
#shellcheck disable=SC2155
export PS1="$(whoami)"'@\h:\w \$ '
elif [ -n "$BASH" ] ; then
HISTFILE="$HOME/.bash_history"
HISTSIZE=10000
HISTFILESIZE=10000
shopt -s histappend
export PROMPT_COMMAND="history -a; history -c; history -r"
# if you want PWD
# export PS1='\u@\h \$PWD \$ '
export PS1='\u@\h\$ '
if [ "$(id -u)" -eq 0 ] ; then
export PS1='\h\$ '
fi
fi
setcolor() {
colormanpages
setcolorterminal
setcoloraliases
setlscolors
}
[ -d "$HOME/.local/bin" ] && path_preappend "$HOME/.local/bin"
# Include local configuration options
[ -r "$LOCAL" ] && . "$LOCAL"
|