improvements and fixes
[distro-setup] / brc
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # this gets sourced. shebang is just for file mode detection
5
6 # Use source ~/.bashrc instead of doing bash -l when running a script
7 # so this can set extdebug and avoid the bash debugger.
8 if [[ -s /a/bin/errhandle/err ]]; then
9 source /a/bin/errhandle/err
10 elif [[ -s $bashrc_dir/err ]]; then
11 # shellcheck source=/a/bin/errhandle/err
12 source $bashrc_dir/err
13 fi
14
15 # In t8, it runs clear_console for login shells by default. I don't want
16 # my console cleared. And linux ttys get cleared without this.
17 if shopt login_shell >/dev/null && [[ -e ~/.bash_logout ]]; then
18 rm ~/.bash_logout
19 fi
20
21 # if [[ -s /usr/share/bash-completion/completions/git ]]; then
22 # source /usr/share/bash-completion/completions/git
23 # fi
24 # if [[ -s /usr/share/bash-completion/completions/gitk ]]; then
25 # source /usr/share/bash-completion/completions/gitk
26 # fi
27
28 # for testing error catching:
29 # t2() {
30 # echo t2
31 # grep sdf sdfd
32 # echo wtf
33 # }
34 # t1() {
35 # echo t1
36 # t2 a b c
37 # }
38
39 # * settings
40
41 CDPATH=.
42
43
44 # remove all aliases. aliases provided by the system tend to get in the way,
45 # for example, error happens if I try to define a function the same name as an alias
46 unalias -a
47
48 # remove gnome keyring warning messages
49 # there is probably a more proper way, but I didnt find any easily on google
50 # now using xfce+xmonad instead of vanilla xmonad, so disabling this
51 #unset GNOME_KEYRING_CONTROL
52
53 # use extra globing features.
54 shopt -s extglob
55 # include .files when globbing, but ignore files name . and ..
56 # setting this also sets dotglob.
57 export GLOBIGNORE="*/.:*/.."
58
59 # Useful info. see man bash.
60 PS4='$LINENO+ '
61
62
63 # broken with bash_completion package. Saw a bug for this once. dont anymore.
64 # still broken in wheezy
65 # still buggered in latest stable from the web, version 2.1
66 # perhaps its fixed in newer git version, which fails to make for me
67 # this note is from 6-2014.
68 # still broken in flidas.
69 #shopt -s nullglob
70
71 # make tab on an empty line do nothing
72 shopt -s no_empty_cmd_completion
73
74 # fix spelling errors for cd, only in interactive shell
75 shopt -s cdspell
76 # append history instead of overwritting it
77 shopt -s histappend
78 # for compatibility, per gentoo/debian bashrc
79 shopt -s checkwinsize
80 # attempt to save multiline single commands as single history entries.
81 shopt -s cmdhist
82 # enable **
83 shopt -s globstar
84
85
86 # inside emacs fixes
87 if [[ $LC_INSIDE_EMACS ]]; then
88 # EMACS is used by bash on startup, but we dont need it anymore.
89 # plus I hit a bug in a makefile which inherited it
90 unset EMACS
91 export LC_INSIDE_EMACS
92 export PAGER=cat
93 export MANPAGER=cat
94 # scp completion does not work, but this doesnt fix it. todo, figure this out
95 #complete -r scp &> /dev/null
96 # todo, remote file completion fails, figure out how to turn it off
97 export NODE_DISABLE_COLORS=1
98 # This gets rid of ugly terminal escape chars in node repl
99 # sometime, Id like to have completion working in emacs shell for node
100 # the offending chars can be found in lib/readline.js,
101 # things that do like:
102 # stream.write('\x1b[' + (x + 1) + 'G');
103 # We can remove them and keep readline, for example by doing this
104 # to start a repl:
105 #!/usr/bin/env nodejs
106 # var readline = require('readline');
107 # readline.cursorTo = function(a,b,c) {};
108 # readline.clearScreenDown = function(a) {};
109 # const repl = require('repl');
110 # var replServer = repl.start('');
111 #
112 # no prompt, or else readline complete seems to be confused, based
113 # on our column being different? node probably needs to send
114 # different kind of escape sequence that is not ugly. Anyways,
115 # completion doesnt work yet even with the ugly prompt, so whatever
116 #
117 export NODE_NO_READLINE=1
118
119 fi
120
121 export SSH_CONFIG_FILE_OVERRIDE=/root/.ssh/confighome
122
123 # emacs has a different default search path than the info command. This
124 # adds the info defaults to emacs, but not the reverse, because I dun
125 # care much about the cli. The search path is only on the cli if you run
126 # "info xxx", or in emacs if you run '(info xxx)', so not that
127 # important, but might as well fix it.
128
129 # info info says this path is what was compiled, and its not documented
130 # anywhere. Through source grepping, i found it in filesys.h of the info
131 # source in trisquel flidas.
132 #
133 # Traling : means for emacs to add its own stuff on to the end.
134
135 export INFOPATH=$PATH:/usr/local/info:/usr/info:/usr/local/lib/info:/usr/lib/info:/usr/local/gnu/info:/usr/local/gnu/lib/info:/usr/gnu/info:/usr/gnu/lib/info:/opt/gnu/info:/usr/share/info:/usr/share/lib/info:/usr/local/share/info:/usr/local/share/lib/info:/usr/gnu/lib/emacs/info:/usr/local/gnu/lib/emacs/info:/usr/local/lib/emacs/info:/usr/local/emacs/info:.:
136
137 # for openwrt system that has no stty, this is easier than
138 # guarding every time i use it.
139 if ! type -p stty >/dev/null; then
140 stty() { :; }
141 fi
142
143
144 use_color=false
145 if [[ $- == *i* ]]; then
146 # for readline-complete.el
147 if [[ $LC_INSIDE_EMACS ]]; then
148 # all for readline-complete.el
149 stty echo
150 bind 'set horizontal-scroll-mode on'
151 bind 'set print-completions-horizontally on'
152 bind '"\C-i": self-insert'
153 else
154
155
156 if [[ $TERM != dumb ]] && test -t 1; then
157 use_color=true
158 fi
159
160 if [[ $KONSOLE_PROFILE_NAME ]]; then
161 TERM=xterm-256color
162 fi
163
164 if [[ $TERM == alacritty && ! -e /usr/share/terminfo/a/alacritty ]]; then
165 # todo: we should try installing the alacritty terminfo if it is not found
166 # https://github.com/alacritty/alacritty/issues/2838
167 TERM=xterm-256color
168 fi
169
170 # copying from the alacritty example above,
171 if [[ $TERM == xterm-kitty ]]; then
172 if [[ ! -e /usr/share/terminfo/x/xterm-kitty ]]; then
173 TERM=xterm-256color
174 else
175 if [[ -e /a/opt/kitty/shell-integration/bash/kitty.bash ]]; then
176 KITTY_SHELL_INTEGRATION=t
177 source /a/opt/kitty/shell-integration/bash/kitty.bash
178 fi
179 fi
180 fi
181
182 # todo: not sure this works in sakura
183 #stty werase undef
184 #bind "\C-w": kill-region
185 # sakura == xterm-256color
186 # konsole == xterm
187 if [[ $TERM != xterm-kitty && $TERM == xterm* ]]; then
188 # control + arrow keys. for other terminals, see http://unix.stackexchange.com/questions/10806/how-to-change-previous-next-word-shortcut-in-bash
189 bind '"\e[1;5C": shell-forward-word' 2>/dev/null
190 bind '"\e[1;5D": shell-backward-word' 2>/dev/null
191 else
192 # make ctrl-backspace work. for konsole, i fixed it through
193 # /home/iank/.local/share/konsole/default.keytab
194 stty werase ^h
195 bind '"\eOc": shell-forward-word'
196 bind '"\eOd": shell-backward-word'
197 fi
198 # i cant remember why i did this, probably to free up some keys to bind
199 # to other things in bash.
200 # other than C-c and C-z, the rest defined by stty -a are, at least in
201 # gnome-terminal, overridden by bash, or disabled by the system
202 stty lnext undef stop undef start undef
203 fi
204
205 fi
206
207 case $TERM in
208 # fixup broken backspace in chroots
209 xterm-kitty|alacritty)
210 chroot() {
211 TERM=xterm-256color command chroot "$@"
212 }
213 ;;
214 esac
215
216 export BC_LINE_LENGTH=0
217
218 # ansible option
219 export PROFILE_TASKS_TASK_OUTPUT_LIMIT=100
220
221 # note, if I use a machine I dont want files readable by all users, set
222 # umask 077 # If fewer than 4 digits are entered, leading zeros are assumed
223
224 # i for insensitive. the rest from
225 # X means dont remove the current screenworth of output upon exit
226 # R means to show colors n things
227 export LESS=RXij12
228 export SYSTEMD_LESS=$LESS
229
230 export NNN_COLORS=2136
231
232 export SL_FILES_DIR=/b/ds/sl/.iank
233 export SL_INFO_DIR=/p/sshinfo
234
235
236 # * include files
237
238 if [[ -s $bashrc_dir/path-add-function ]]; then
239 source $bashrc_dir/path-add-function
240 if [[ $SSH_CLIENT ]]; then
241 if grep -qF /home/iank/.iank/e/e /etc/exports &>/dev/null; then
242 export EMACSDIR=/home/iank/.iank/e/e
243 fi
244 path-add $bashrc_dir
245 fi
246 fi
247
248 # if someone exported $SOE (stop on error), catch errors.
249 #
250 # Note, on debian this results in the following warning when in ssh,
251 # hich I haven't figured out how to fix. It doesn't happen if we source
252 # after the shell has started
253 #
254 # bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
255 # bash: warning: cannot start debugger; debugging mode disabled
256 if [[ $SOE ]]; then
257 if [[ -e /a/bin/errhandle/err ]]; then
258 source /a/bin/errhandle/err
259 fi
260 fi
261
262 # based on readme.debian. dunno if this will break on other distros.
263 if [[ -s /usr/share/wcd/wcd-include.sh ]]; then
264 source /usr/share/wcd/wcd-include.sh
265 fi
266
267
268 mysrc() {
269 local path dir file
270 path=$1
271 dir=${path%/*}
272 file=${path##*/}
273 if [[ -s $path ]]; then
274 source $path
275 elif [[ -s $bashrc_dir/$file ]]; then
276 source $bashrc_dir/$file
277 fi
278 }
279
280
281 mysrc /a/bin/small-misc-bash/ll-function
282 mysrc /a/bin/distro-functions/src/package-manager-abstractions
283
284
285 # * functions
286 ccomp() { # copy completion
287 local src=$1
288 local c
289 shift
290 if ! c=$(complete -p $src 2>/dev/null); then
291 _completion_loader $src &>/dev/null ||:
292 c=$(complete -p $src 2>/dev/null) || return 0
293 fi
294 # remove $src( .*|$)
295 c=${c% $src}
296 c=${c%% $src *}
297 eval $c $*
298 }
299
300
301 ..() { c ..; }
302 ...() { c ../..; }
303 ....() { c ../../..; }
304 .....() { c ../../../..; }
305 ......() { c ../../../../..; }
306
307 chere() {
308 local f path
309 for f; do
310 path=$(readlink -e "$f")
311 echo "cat >$path <<'EOF'"
312 cat "$f"
313 echo EOF
314 done
315 }
316
317
318 # file cut copy and paste, like the text buffers :)
319 # I havnt tested these.
320 _fbufferinit() { # internal use
321 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
322 rm -rf "${my_f_tempdir:?}"/*
323 }
324 fcp() { # file cp
325 _fbufferinit
326 cp "$@" "$my_f_tempdir"/
327 }
328 fct() { # file cut
329 _fbufferinit
330 mv "$@" "$my_f_tempdir"/
331 }
332 fpst() { # file paste
333 [[ $2 ]] && { echo too many arguments; return 1; }
334 target=${1:-.}
335 cp "$my_f_tempdir"/* "$target"
336 }
337
338 _khfix_common() {
339 local host ip port file key
340 read -r host ip port < <(timeout -s 9 2 ssh -oBatchMode=yes -oControlMaster=no -oControlPath=/ -v $1 |& sed -rn "s/debug1: Connecting to ([^ ]+) \[([^\]*)] port ([0-9]+).*/\1 \2 \3/p" ||: )
341 file=$(readlink -f ~/.ssh/known_hosts)
342 if [[ ! $ip ]]; then
343 echo "khfix: ssh failed"
344 return 1
345 fi
346 if [[ $port != 22 ]]; then
347 ip_entry="[$ip]:$port"
348 host_entry="[$host]:$port"
349 else
350 ip_entry=$ip
351 host_entry=$host
352 fi
353 tmpfile=$(mktemp)
354 if [[ $host != $ip ]]; then
355 key=$(ssh-keygen -F "$host_entry" -f $file | sed -r 's/^.*([^ ]+ +[^ ]+) *$/\1/')
356 if [[ $key ]]; then
357 grep -Fv "$key" "$file" | sponge "$file"
358 fi
359 fi
360 key=$(ssh-keygen -F "$ip_entry" -f $file | sed -r 's/^.*([^ ]+ +[^ ]+) *$/\1/')
361 if [[ $key ]]; then
362 grep -Fv "$key" "$file" | sponge "$file"
363 fi
364 ll ~/.ssh/known_hosts
365 rootsshsync
366 }
367 khfix() { # known hosts fix
368 _khfix_common "$@" || return 1
369 ssh $1 :
370 }
371 khcopy() {
372 _khfix_common "$@"
373 ssh-copy-id $1
374 }
375
376 a() {
377 local x
378 x=$(readlink -nf "${1:-$PWD}")
379 # yes, its kinda dumb that xclip/xsel cant do this in one invocation
380 echo -n "$x" | xclip -selection clipboard
381 echo -n "$x" | xclip
382 }
383
384 # a1 = awk {print $1}
385 for field in {1..20}; do
386 eval a$field"() { awk '{print \$$field}'; }"
387 done
388 # h1 = head -n1
389 for num in {1..9}; do
390 eval h$num"() { head -n$num; }"
391 done
392
393
394 b() {
395 # backwards
396 c -
397 }
398
399 hexipv4() {
400 printf '%d.%d.%d.%d\n' $(echo $1 | sed 's/../0x& /g')
401 }
402
403 vp9() {
404 local f out outdir in
405 outdir=vp9
406 case $1 in
407 --out)
408 outdir=$2
409 shift 2
410 ;;
411 esac
412 m mkdir -p $outdir
413 for f; do
414 out=$PWD/$outdir/$f
415 in=$PWD/$f
416 m cd $(mktemp -d)
417 pwd
418 m ffmpeg -threads 0 -i $in -g 192 -vcodec libvpx-vp9 -vf scale=-1:720 -max_muxing_queue_size 9999 -b:v 750K -pass 1 -an -f null /dev/null
419 m ffmpeg -y -threads 0 -i $in -g 192 -vcodec libvpx-vp9 -vf scale=-1:720 -max_muxing_queue_size 9999 -b:v 750K -pass 2 -c:a libvorbis -qscale:a 5 $out
420 cd -
421 done
422 }
423
424 utcl() { # utc 24 hour time to local hour 24 hour time
425 echo "print( ($1 $(date +%z | sed -r 's/..$//;s/^(-?)0*/\1/')) % 24)"|python3
426 }
427
428 # c. better cd
429 if ! type -t c &>/dev/null; then
430 if type -p wcd &>/dev/null; then
431 if [[ $LC_INSIDE_EMACS ]]; then
432 c() { wcd -c -z 50 -o "$@"; }
433 else
434 # lets see what the fancy terminal does from time to time
435 c() { wcd -c -z 50 "$@"; }
436 fi
437 else
438 c() { cd "$@"; }
439 fi
440 fi
441 ccomp cd c
442
443 c4() { c /var/log/exim4; }
444
445 caa() { git commit --amend --no-edit -a; }
446
447 cf() {
448 for f; do
449 hr
450 echo "$f"
451 hr
452 cat "$f"
453 done
454 }
455 caf() {
456 # shellcheck disable=SC2033
457 find -L "$@" -type f -not \( -name .svn -prune -o -name .git -prune \
458 -o -name .hg -prune -o -name .editor-backups -prune \
459 -o -name .undo-tree-history -prune \) \
460 -exec bash -c '. ~/.bashrc; hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
461
462 }
463 ccomp cat cf caf
464
465 calc() { echo "scale=3; $*" | bc -l; }
466 # no having to type quotes, but also no command history:
467 clc() {
468 local x
469 read -r x
470 echo "scale=3; $x" | bc -l
471 }
472
473 cam() {
474 git commit -am "$*"
475 }
476
477 ccat () { # config cat. see a config without extra lines.
478 sed -r '/^[[:space:]]*([;#]|--|\/\/|$)/d' "$@"
479 }
480 ccomp grep ccat
481
482 chrbind() {
483 local d
484 # dev/pts needed for pacman signature check
485 for d in dev proc sys dev/pts; do
486 [[ -d $d ]]
487 if ! mountpoint $d &>/dev/null; then
488 m s mount -o bind /$d $d
489 fi
490 done
491 }
492 chumount() {
493 local d
494 # dev/pts needed for pacman signature check
495 for d in dev proc sys dev/pts; do
496 [[ -d $d ]]
497 if mountpoint $d &>/dev/null; then
498 m s umount $d
499 fi
500 done
501 }
502
503
504 _cdiff-prep() {
505 # join options which are continued to multiples lines onto one line
506 local first=true
507 while IFS= read -r line; do
508 # remove leading spaces/tabs. assumes extglob
509 if [[ $line == "[ ]*" ]]; then
510 line="${line##+( )}"
511 fi
512 if $first; then
513 pastline="$line"
514 first=false
515 elif [[ $line == *=* ]]; then
516 echo "$pastline" >> "$2"
517 pastline="$line"
518 else
519 pastline="$pastline $line"
520 fi
521 done < <(grep -vE '^([ \t]*#|^[ \t]*$)' "$1")
522 echo "$pastline" >> "$2"
523 }
524
525 cdiff() {
526 # diff config files,
527 # setup for format of postfix, eg:
528 # option = stuff[,]
529 # [more stuff]
530 local pastline unified f1 f2
531 unified="$(mktemp)"
532 f1="$(mktemp)"
533 f2="$(mktemp)"
534 _cdiff-prep "$1" "$f1"
535 _cdiff-prep "$2" "$f2"
536 cat "$f1" "$f2" | grep -Po '^[^=]+=' | sort | uniq > "$unified"
537 while IFS= read -r line; do
538 # the default bright red / blue doesnt work in emacs shell
539 dwdiff -cblue,red -A best -d " ," <(grep "^$line" "$f1" || echo ) <(grep "^$line" "$f2" || echo ) | colordiff
540 done < "$unified"
541 }
542
543
544 cat-new-files() {
545 local start=$SECONDS
546 local dir="$1"
547 inotifywait -m "$dir" -e create -e moved_to |
548 # shellcheck disable=SC2030
549 while read -r filedir _ file; do
550 cat "$filedir$file"
551 hr
552 calc $((SECONDS - start)) / 60
553 sleep 5
554 done
555
556 }
557
558 # shellcheck disable=SC2032
559 chown() {
560 # makes it so chown -R symlink affects the symlink and its target.
561 if [[ $1 == -R ]]; then
562 shift
563 command chown -h "$@"
564 command chown -R "$@"
565 else
566 command chown "$@"
567 fi
568 }
569
570 cim() {
571 git commit -m "$*"
572 }
573
574 cl() {
575 # choose recent directory. cl = cd list
576 c =
577 }
578
579 d() { builtin bg "$@"; }
580 ccomp bg d
581
582 dc() {
583 diff --strip-trailing-cr -w "$@" # diff content
584 }
585 ccomp diff dc
586
587 despace() {
588 local x y
589 for x in "$@"; do
590 y="${x// /_}"
591 safe_rename "$x" "$y"
592 done
593 }
594
595 dig() {
596 command dig +nostats +nocmd "$@"
597 }
598 # Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed.
599 digsort() {
600 local sec
601 sec=
602 dig +nordflag "$@" | sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' | while read -r l; do
603 if [[ $l == [^\;]* ]]; then
604 sec+="$l"$'\n'
605 else
606 if [[ $sec ]]; then
607 printf "%s" "$sec" | sort
608 sec=
609 fi
610 printf "%s\n" "$l"
611 fi
612 done
613 }
614 ccomp dig digsort
615 # compare digs to the 2 servers
616 # usage: digdiff @server1 @server2 DIG_ARGS
617 # note: only the soa master nameserver will respond with
618 # ra "recursive answer" flag. That difference is meaningless afaik.
619 digdiff() {
620 local s1 s2
621 s1=$1
622 shift
623 s2=$1
624 shift
625 digsort $s1 "$@" | tee /tmp/digdiff
626 diff -u /tmp/digdiff <(digsort $s2 "$@")
627 }
628
629 dt() {
630 date "+%A, %B %d, %r" "$@"
631 }
632 ccomp date dt
633
634 dus() { # du, sorted, default arg of
635 du -sh ${@:-*} | sort -h
636 }
637 ccomp du dus
638
639
640 e() { echo "$@"; }
641
642 # echo args
643 ea() {
644 if (( ! $# )); then
645 echo no args
646 fi
647 for arg; do
648 printf "%qEOL\n" "${arg}"
649 printf "%s" "${arg}" |& hexdump -C
650 done
651 }
652 # echo vars. print var including escapes, etc
653 ev() {
654 if (( ! $# )); then
655 echo no args
656 fi
657 for arg; do
658 if [[ -v $arg ]]; then
659 printf "%qEOL\n" "${!arg}"
660 printf "%s" "${!arg}" |& hexdump -C
661 else
662 echo arg $arg is unset
663 fi
664 done
665 }
666
667 ediff() {
668 [[ ${#@} == 2 ]] || { echo "error: ediff requires 2 arguments"; return 1; }
669 emacs --eval "(ediff-files \"$1\" \"$2\")"
670 }
671
672 # mail related
673 etail() {
674 tail -F /var/log/exim4/mainlog -n 200 "$@"
675 }
676 ccomp tail etail
677
678 # print exim old pids
679 eoldpids() {
680 local configtime pid piduptime now daemonpid
681 printf -v now '%(%s)T' -1
682 configtime=$(stat -c%Y /var/lib/exim4/config.autogenerated)
683 if [[ -s /run/exim4/exim.pid ]]; then
684 daemonpid=$(cat /run/exim4/exim.pid)
685 fi
686 for pid in $(pgrep -f '^/usr/sbin/exim4( |$)'); do
687 # the daemonpid gets reexeced on HUP (service reloads), keeping its same old timestamp
688 if [[ $pid == $daemonpid ]]; then
689 continue
690 fi
691 piduptime=$(awk -v ticks="$(getconf CLK_TCK)" 'NR==1 { now=$1; next } END { printf "%9.0f\n", now - ($20/ticks) }' /proc/uptime RS=')' /proc/$pid/stat) ||: # sometimes pids disappear pretty fast
692 if (( configtime > now - piduptime )); then
693 echo $pid
694 fi
695 done
696 }
697
698 # exim tail but only watch lines from new pids
699 etailnew() {
700 local pid oldpids
701 for pid in $(eoldpids); do
702 oldpids+="$pid|"
703 done
704 if [[ $oldpids ]]; then
705 etail | awk '$3 !~ /^\[('"${oldpids%|}"')\]$/'
706 else
707 etail
708 fi
709 }
710 # exim watch as old pids go away
711 ewatchold() {
712 local configtime pid piduptime now
713 local -i count
714 local -a oldpids
715 count=0
716 while true; do
717 oldpids=($(eoldpids))
718 if (( ! ${#oldpids[@]} )); then
719 return
720 fi
721 # print the date every 20 iterations
722 if (( ! count % 20 )); then
723 date
724 fi
725 count+=1
726 ps -f -p "${oldpids[*]}"
727 sleep 1
728 done
729 }
730
731 eless() {
732 less /var/log/exim4/mainlog
733 }
734 ccomp less eless
735 eqcat() {
736 exiqgrep -i -o 60 | while read -r i; do
737 hlm exim -Mvc $i
738 echo
739 hlm exigrep $i /var/log/exim4/mainlog | cat ||:
740 done
741 }
742 eqrmf() {
743 exiqgrep -i | xargs exim -Mrm
744 }
745
746 econfdevnew() {
747 rm -rf /tmp/edev
748 mkdir -p /tmp/edev/etc
749 cp -ra /etc/exim4 /tmp/edev/etc
750 cp -ra /etc/alias* /tmp/edev/etc
751 find /tmp/edev/etc/exim4 -type f -execdir sed -i "s,/etc/,/tmp/edev/etc/,g" '{}' +
752 econfdev
753 }
754 econfdev() {
755 update-exim4.conf -d /tmp/edev/etc/exim4 -o /tmp/edev/e.conf
756 }
757
758
759
760 # shellcheck disable=SC2032
761 f() {
762 # cd forward
763 c +
764 }
765
766 fa() {
767 # find array. make an array of file names found by find into $x
768 # argument: find arguments
769 # return: find results in an array $x
770 while read -rd ''; do
771 x+=("$REPLY");
772 done < <(find "$@" -print0);
773 }
774
775 faf() { # find all files. use -L to follow symlinks
776 find "$@" -not \( -name .svn -prune -o -name .git -prune \
777 -o -name .hg -prune -o -name .editor-backups -prune \
778 -o -name .undo-tree-history -prune \) -type f 2>/dev/null
779 }
780
781 # todo: id like to do maybe a daily or hourly cronjob to
782 # check that my history file size is increasing. Ive had it
783 # inexplicably truncated in the past.
784 histrm() {
785 history -n
786 history | awk -v IGNORECASE=1 '{ a=$1; sub(/^( *[^ ]+){4} */, "") }; /'"$*"'/'
787 read -p "press anything but contrl-c to delete"
788 for entry in $(history | awk -v IGNORECASE=1 '{ a=$1; sub(/^( *[^ ]+){4} */, "") }; /'"$*"'/ { print a }' | tac); do
789 history -d $entry
790 done
791 history -w
792 }
793
794 # mail related
795 frozen() {
796 rm -rf /tmp/frozen
797 sudo mailq |gr frozen|awk '{print $3}' | while read -r id; do
798 sudo exim -Mvl $id
799 echo
800 sudo exim -Mvh $id
801 echo
802 sudo exim -Mvb $id
803 echo -e '\n\n##############################\n'
804 done | tee -a /tmp/frozen
805 }
806 frozenrm() {
807 local ids=()
808 while read -r line; do
809 printf '%s\n' "$line"
810 ids+=($(printf '%s\n' "$line" |gr frozen|awk '{print $3}'))
811 done < <(s mailq)
812 echo "sleeping for 2 in case you change your mind"
813 sleep 2
814 sudo exim -Mrm "${ids[@]}"
815 }
816
817 funce() {
818 # like -e for functions. returns on error.
819 # at the end of the function, disable with:
820 # trap ERR
821 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
822 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"
823 trap ERR
824 return' ERR
825 }
826
827 getdir () {
828 local help="Usage: getdir [--help] PATH
829 Output the directory of PATH, or just PATH if it is a directory."
830 if [[ $1 == --help ]]; then
831 echo "$help"
832 return 0
833 fi
834 if [[ $# -ne 1 ]]; then
835 echo "getdir error: expected 1 argument, got $#"
836 return 1
837 fi
838 if [[ -d $1 ]]; then
839 echo "$1"
840 else
841 local dir
842 dir="$(dirname "$1")"
843 if [[ -d $dir ]]; then
844 echo "$dir"
845 else
846 echo "getdir error: directory does not exist"
847 return 1
848 fi
849 fi
850 }
851
852 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
853 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
854 local root
855 root=$(gitroot) || return 1 # function to set gitroot
856 builtin cd "$root"
857 git symbolic-ref HEAD refs/heads/$1
858 rm .git/index
859 git clean -fdx
860 }
861
862 # shellcheck disable=SC2120
863 gitroot() {
864 local help="Usage: gitroot [--help]
865 Print the full path to the root of the current git repo
866
867 Handles being within a .git directory, unlike git rev-parse --show-toplevel,
868 and works in older versions of git which did not have that."
869 if [[ $1 == --help ]]; then
870 echo "$help"
871 return
872 fi
873 local p
874 p=$(git rev-parse --git-dir) || { echo "error: not in a git repo" ; return 1; }
875 [[ $p != /* ]] && p=$PWD
876 echo "${p%%/.git}"
877 }
878
879 g() {
880
881 # todo: patch emacs so it will look elsewhere. this is kinda sad:
882 # https://emacs.stackexchange.com/questions/4253/how-to-start-emacs-with-a-custom-user-emacs-directory
883
884 local args gdb=false
885
886 if [[ $EMACSDIR ]]; then
887 path-add "$EMACSDIR/lib-src" "$EMACSDIR/src"
888 fi
889
890 if [[ $DISPLAY ]]; then
891 args=-n
892 fi
893
894 if (( $# == 0 )); then
895 args+=" -c"
896 fi
897 # duplicate -c, but oh well
898 if ! pgrep -u $EUID emacsclient; then
899 if (( $# == 0 )) && type -p gdb &>/dev/null; then
900 gdb=true
901 else
902 args+=" -c"
903 fi
904 fi
905 if [[ $EMACSDIR ]]; then
906 # Alter the path here, otherwise the nfs mount gets triggered on the
907 # first path lookup when emacs is not being used.
908 PATH="$EMACSDIR/lib-src:$EMACSDIR/src:$PATH" EHOME=$HOME HOME=$EMACSDIR m emacsclient -a "" $args "$@"
909 else
910 if $gdb; then
911 # due to a bug, we cant debug from the start unless we get a new gdb
912 # https://sourceware.org/bugzilla/show_bug.cgi?id=24454
913 # m gdb -ex="set follow-fork-mode child" -ex=r -ex=quit --args emacs --daemon
914 m emacsclient -a "" $args "$@"
915 sleep 1
916 cd /a/opt/emacs-$(distro-name)$(distro-num)
917 s gdb -p $(pgrep -f 'emacs --daemon') -ex c
918 cd -
919 else
920 m emacsclient -a "" $args "$@"
921 fi
922 fi
923 }
924
925 # force terminal version
926 gn() {
927 g -n "$@"
928 }
929
930 gmacs() {
931 # quit will prompt if the program crashes.
932 gdb -ex=r -ex=quit --args emacs "$@"; r;
933 }
934
935 gdkill() {
936 # kill the emacs daemon
937 pk1 emacs --daemon
938 }
939
940 gr() {
941 grep -iIP --color=auto "$@" || return $?
942 }
943 grr() { # grep recursive
944 # Don't return 1 on nonmatch because this is meant to be
945 # interactive, not in a conditional.
946 if [[ ${#@} == 1 ]]; then
947 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -riIP --color=auto "$@" . || [[ $? == 1 ]]
948 else
949 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -riIP --color=auto "$@" || [[ $? == 1 ]]
950 fi
951 }
952 ccomp grep gr grr
953
954 rg() { grr "$@"; }
955 ccomp grep rg
956
957 hr() { # horizontal row. used to break up output
958 printf "$(tput setaf 5 2>/dev/null ||:)â–ˆ$(tput sgr0 2>/dev/null||:)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
959 echo
960 }
961 # highlight
962 hl() {
963 local col input_len=0
964 for arg; do
965 input_len=$((input_len + 1 + ${#arg}))
966 done
967 col=$((60 - input_len))
968 printf "\e[1;97;41m%s" "$*"
969 if (( col > 0 )); then
970 printf "\e[1;97;41m \e[0m%.0s" $(eval echo "{1..${col}}")
971 fi
972 echo
973 }
974 hlm() { hl "$*"; "$@"; }
975
976 hrcat() { local f; for f; do [[ -f $f ]] || continue; hr; echo "$f"; cat "$f"; done }
977
978
979 # get latest hub and run it
980 # main command to use:
981 # hub pull-request --no-edit
982 # --no-edit means to use the first commit\'s message as the pull request message.
983 # If that fails, try doing
984 # hub pull-request --no-edit -b UPSTREAM_OWNER:branch
985 # where branch is usually master. it does the pr against your current branch.
986 #
987 # On first use, you input username/pass and it gets an oath token so you dont have to repeat
988 # it\'s at ~/.config/hub
989 hub() {
990 local up uptar updir p
991 p=/github/hub/releases/
992 up=https://github.com/$(curl -s https://github.com$p| grep -o $p'download/[^/]*/hub-linux-amd64[^"]*' | head -n1)
993 uptar=${up##*/}
994 updir=${uptar%.tgz}
995 if [[ ! -e /a/opt/$updir ]]; then
996 rm -rf /a/opt/hub-linux-amd64*
997 wget -P /a/opt $up
998 tar -C /a/opt -zxf /a/opt/$uptar
999 rm -f /a/opt/$uptar
1000 fi
1001 if ! which hub &>/dev/null; then
1002 sudo /a/opt/$updir/install
1003 fi
1004
1005 # save token across computers
1006 if [[ ! -L ~/.config/hub ]]; then
1007 if [[ -e ~/.config/hub ]]; then
1008 mv ~/.config/hub /p/c/subdir_files/.config/
1009 fi
1010 if [[ -e /p/c/subdir_files/.config/hub ]]; then
1011 conflink
1012 fi
1013 fi
1014 command hub "$@"
1015 }
1016
1017 i() { git "$@"; }
1018 ccomp git i
1019
1020 ic() {
1021 # fast commit all
1022 git commit -am "$*"
1023 }
1024
1025 ipp() {
1026 git pull
1027 git push
1028 }
1029
1030
1031 ifn() {
1032 # insensitive find
1033 find -L . -not \( -name .svn -prune -o -name .git -prune \
1034 -o -name .hg -prune -o -name .editor-backups -prune \
1035 -o -name .undo-tree-history -prune \) -iname "*$**" 2>/dev/null
1036 }
1037
1038 ifd() {
1039 # insensitive find directory
1040 find -L . -type d -not \( -name .svn -prune -o -name .git -prune \
1041 -o -name .hg -prune -o -name .editor-backups -prune \
1042 -o -name .undo-tree-history -prune \) -iname "*$**" 2>/dev/null
1043 }
1044
1045
1046 ipdrop() {
1047 sudo iptables -A INPUT -s $1 -j DROP
1048 }
1049
1050
1051 istext() {
1052 grep -Il "" "$@" &>/dev/null
1053 }
1054
1055 jtail() {
1056 journalctl -n 10000 -f "$@"
1057 }
1058 jr() { journalctl "$@" ; }
1059 jrf() { journalctl -f "$@" ; }
1060 jru() {
1061 journalctl -u exim4 _SYSTEMD_INVOCATION_ID=$(systemctl show -p InvocationID --value $1)
1062 }
1063
1064 l() {
1065 if [[ $PWD == /[iap] ]]; then
1066 command ls -A --color=auto -I lost+found "$@"
1067 else
1068 command ls -A --color=auto "$@"
1069 fi
1070 }
1071
1072 lcn() { locate -i "*$**"; }
1073
1074 lg() { LC_COLLATE=C.UTF-8 ll --group-directories-first "$@"; }
1075
1076 lt() { ll -tr "$@"; }
1077
1078 lld() { ll -d "$@"; }
1079
1080 ccomp ls l lg lt lld ll
1081
1082 # low recursively
1083 lowr() {
1084 local f dirs i a
1085 local -a all
1086 for dirs in false true; do
1087 for f; do
1088 if [[ -d $f ]]; then
1089 all=("$f"/**)
1090 # reverse the order to rename the nested dirs first.
1091 # note: 0 element is the dir itself
1092 for ((i=${#all[@]}-1; i>=1; i--)); do
1093 a="${all[i]}"
1094 if $dirs && [[ -d $a ]]; then
1095 # e dirs low "$a" # debug
1096 low "$a"
1097 elif ! $dirs && [[ ! -d $a && -e $a ]]; then
1098 # debug
1099 # e not dirs low "$a" # debug
1100 low "$a"
1101 fi
1102 done
1103 fi
1104 # just rename all the top level args on the second pass
1105 if $dirs; then
1106 # e final dirs low "$f" # debug
1107 low "$f"
1108 fi
1109 done
1110 done
1111 }
1112
1113 low() { # make filenames lowercase, remove bad chars
1114 local arg new dir f
1115 for arg; do
1116 arg="${arg%%+(/)}" # remove trailing slashes. assumes we have extglob on.
1117 dir="${arg%/*}"
1118 if (( ${#dir} == ${#arg} )); then
1119 dir=.
1120 fi
1121 f="${arg##*/}"
1122 new="${f,,}" # downcase
1123 new="${new//[^a-zA-Z0-9._-]/_}" # sub bad chars
1124 new="${new#"${new%%[[:alnum:]]*}"}" # remove leading/trailing non-alnum
1125 new="${new%"${new##*[[:alnum:]]}"}"
1126 # remove bad underscores, like __ and _._
1127 new=$(echo $new | sed -r 's/__+/_/g;s/_+([.-])|([.-])_+/\1/g')
1128 safe_rename "$dir/$f" "$dir/$new" || return 1
1129 done
1130 return 0
1131 }
1132
1133 lower() { # make first letter of filenames lowercase.
1134 local x
1135 for x in "$@"; do
1136 if [[ ${x::1} == [A-Z] ]]; then
1137 y=$(tr '[:upper:]' '[:lower:]' <<<"${x::1}")"${x:1}"
1138 safe_rename "$x" "$y" || return 1
1139 fi
1140 done
1141 }
1142
1143
1144 k() { # history search
1145 grep -iP --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | tail -n 80 || [[ $? == 1 ]];
1146 }
1147 ks() { # history search
1148 grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | uniq || [[ $? == 1 ]];
1149 }
1150 ccomp grep k ks
1151
1152
1153 make-targets() {
1154 # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
1155 make -qp | awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
1156 }
1157
1158 mkc() {
1159 mkdir "$1"
1160 c "$1"
1161 }
1162 ccomp mkdir mkc
1163
1164 mkct() {
1165 mkc $(mktemp -d)
1166 }
1167
1168 mkt() { # mkdir and touch file
1169 local path="$1"
1170 mkdir -p "$(dirname "$path")"
1171 touch "$path"
1172 }
1173
1174 # shellcheck disable=SC2032
1175 mkdir() { command mkdir -p "$@"; }
1176
1177 nags() {
1178 # https://github.com/HenriWahl/Nagstamon/issues/357
1179 if ! pgrep -f /usr/lib/notification-daemon/notification-daemon >/dev/null; then
1180 /usr/lib/notification-daemon/notification-daemon &
1181 fi
1182 /usr/bin/nagstamon &
1183 }
1184
1185 nmt() {
1186 s nmtui-connect "$@"
1187 }
1188
1189 nopanic() {
1190 # shellcheck disable=SC2024
1191 sudo tee -a /var/log/exim4/paniclog-archive </var/log/exim4/paniclog; sudo truncate -s0 /var/log/exim4/paniclog
1192 }
1193
1194 p8() { ping "$@" 8.8.8.8; }
1195 p6() { ping6 "$@" 2001:4860:4860::8888; }
1196
1197 pkx() { # package extract
1198 local pkg cached tmp f
1199 c $(mktemp -d)
1200 pkg=$1
1201 # shellcheck disable=SC2012
1202 cached=$(ls -t /var/cache/apt/archives/$pkg* | tail -n1 2>/dev/null) ||:
1203 if [[ $cached ]]; then
1204 cp $cached .
1205 else
1206 aptitude download $pkg || return 1
1207 fi
1208 tmp=(*); f=${tmp[0]} # only 1 expected
1209 ex $f
1210 rm -f $f
1211 }
1212
1213 # pgrep and kill
1214 pk1() {
1215 local pid
1216 pid=($(pgrep -f "$*"))
1217 case ${#pid[@]} in
1218 1)
1219 # shellcheck disable=SC2128
1220 {
1221 ps -F $pid
1222 m kill $pid
1223 }
1224 ;;
1225 0) echo "no pid found" ;;
1226 *)
1227 ps -F ${pid[@]}
1228 ;;
1229 esac
1230 }
1231
1232 psg () {
1233 local x y help
1234 help="Usage: psg [--help] GREP_ARGS
1235 grep ps and output in a nice format"
1236 if [[ $1 == --help ]]; then
1237 echo "$help"
1238 return
1239 fi
1240 x=$(ps -eF)
1241 # final grep is because some commands tend to have a lot of trailing spaces
1242 y=$(echo "$x" | grep -iP "$@" | grep -o '.*[^ ]') ||:
1243 if [[ $y ]]; then
1244 echo "$x" | head -n 1 || [[ $? == 141 ]]
1245 echo "$y"
1246 fi
1247 }
1248
1249 pubip() { curl -4s https://icanhazip.com; }
1250 pubip6() { curl -6s https://icanhazip.com; }
1251 whatismyip() { pubip; }
1252
1253
1254 q() { # start / launch a program in the backround and redir output to null
1255 "$@" &> /dev/null &
1256 }
1257
1258 # shellcheck disable=SC2120
1259 r() {
1260 if [[ $HISTFILE ]]; then
1261 history -a # save history
1262 fi
1263 trap ERR # this avoids a segfault
1264 exit ${1:0}
1265 # i had this redir, not sure why
1266 # exit "$@" 2>/dev/null
1267 }
1268
1269 # scp is insecure and deprecated.
1270 scp() {
1271 rsync --inplace "$@"
1272 }
1273
1274 randport() {
1275 # available high ports are 1024-65535,
1276 # but lets skip things that are more likely to be in use
1277 python3 <<'EOF'
1278 import secrets
1279 print(secrets.SystemRandom().randrange(10002,65500))
1280 EOF
1281 }
1282
1283 # reapply bashrc
1284 reb() {
1285 source ~/.bashrc
1286 }
1287
1288 rl() {
1289 readlink -f "$@"
1290 }
1291 ccomp readlink rl
1292
1293 rsd() {
1294 # rsync, root is required to keep permissions right.
1295 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
1296 # --no-times --delete
1297 # basically, make an exact copy, use checksums instead of file times to be more accurate
1298 rsync -ahvic --delete "$@"
1299 }
1300 rsa() {
1301 # like rlu, but dont delete files on the target end which
1302 # do not exist on the original end.
1303 rsync -ahvic "$@"
1304 }
1305 rst() {
1306 # rl without preserving modification time.
1307 rsync -ahvic --delete --no-t "$@"
1308 }
1309 rsu() { # [OPTS] HOST PATH
1310 # eg. rlu -opts frodo /testpath
1311 # relative paths will expanded with readlink -f.
1312 opts=("${@:1:$#-2}") # 1 to last -2
1313 path="${*:$#}" # last
1314 host="${*:$#-1:1}" # last -1
1315 if [[ $path == .* ]]; then
1316 path=$(readlink -f $path)
1317 fi
1318 # rync here uses checksum instead of time so we dont mess with
1319 # unison relying on time as much. g is for group, same reason
1320 # to keep up with unison.
1321 m s rsync -rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
1322 }
1323 ccomp rsync rsd rsa rst rsu
1324
1325 # find programs listening on a port
1326 ssp() {
1327 local port=$1
1328 # to figure out these args, i had to look at the man page from git version, as of 2022-04.
1329 s ss -lpn state listening sport = $port
1330 }
1331
1332 resolvcat() {
1333 local f
1334 if [[ $(systemctl is-active nscd ||:) != inactive ]]; then
1335 m s nscd -i hosts
1336 fi
1337 f=/etc/resolv.conf
1338 echo $f:; ccat $f
1339 hr; s ss -lpn sport = 53
1340 if systemctl is-enabled dnsmasq &>/dev/null || [[ $(systemctl is-active dnsmasq ||:) != inactive ]]; then
1341 # this will fail is dnsmasq is failed
1342 hr; m ser status dnsmasq | cat || :
1343 f=/etc/dnsmasq.conf
1344 hr; echo $f:; ccat $f
1345 hr; m grr '^ *(servers-file|server) *=|^ *no-resolv *$' /etc/dnsmasq.conf /etc/dnsmasq.d
1346 f=/etc/dnsmasq-servers.conf
1347 hr; echo $f:; ccat $f
1348 fi
1349 hr
1350 echo /etc/nsswitch.conf:
1351 grep '^ *hosts:' /etc/nsswitch.conf
1352 if systemctl is-enabled systemd-resolved &>/dev/null || [[ $(systemctl is-active systemd-resolved ||:) != inactive ]]; then
1353 hr; m ser status systemd-resolved | cat || :
1354 hr; m systemd-resolve --status | cat
1355 fi
1356
1357 }
1358 rcat() {
1359 resolvcat | less
1360 }
1361 reresolv() {
1362 if [[ $(systemctl is-active nscd ||:) != inactive ]]; then
1363 m ser stop nscd
1364 sleep .5
1365 m ser start nscd
1366 m sudo nscd -i hosts
1367 fi
1368 if [[ $(systemctl is-active dnsmasq ||:) != inactive ]]; then
1369 m sudo systemctl restart dnsmasq
1370 fi
1371 if [[ $(systemctl is-active systemd-resolved ||:) != inactive ]]; then
1372 m sudo systemctl restart systemd-resolved
1373 fi
1374 if type -P resolvectl &>/dev/null; then
1375 resolvectl flush-caches
1376 fi
1377 }
1378
1379 rmstrips() {
1380 ssh fencepost head -n 300 /gd/gnuorg/EventAndTravelInfo/rms-current-trips.txt | less
1381 }
1382
1383 sudo () {
1384 command sudo "$@" || return $?
1385 DID_SUDO=true
1386 }
1387 s() {
1388 # background
1389 # I use a function because otherwise we cant use in a script,
1390 # cant assign to variable.
1391 #
1392 # note: gksudo is recommended for X apps because it does not set the
1393 # home directory to the same, and thus apps writing to ~ fuck things up
1394 # with root owned files.
1395 #
1396 if [[ $EUID != 0 || $1 == -* ]]; then
1397 # shellcheck disable=SC2034
1398 SUDOD="$PWD" command sudo -i "$@"
1399 DID_SUDO=true
1400 else
1401 "$@"
1402 fi
1403 }
1404 sb() { # sudo bash -c
1405 # use sb instead of s is for sudo redirections,
1406 # eg. sb 'echo "ok fine" > /etc/file'
1407 # shellcheck disable=SC2034
1408 local SUDOD="$PWD"
1409 sudo -i bash -c "$@"
1410 }
1411 ccomp sudo s sb
1412
1413 safe_rename() { # warn and dont rename if file exists.
1414 # mv -n exists, but it\'s silent
1415 if [[ $# != 2 ]]; then
1416 echo safe_rename error: $# args, need 2 >2
1417 return 1
1418 fi
1419 if [[ $1 != "$2" ]]; then # yes, we want to silently ignore this
1420 if [[ -e $2 || -L $2 ]]; then
1421 echo "Cannot rename $1 to $2 as it already exists."
1422 else
1423 mv -vi "$1" "$2"
1424 fi
1425 fi
1426 }
1427
1428
1429 sd() {
1430 sudo dd status=none of="$1"
1431 }
1432
1433 ser() {
1434 if type -p systemctl &>/dev/null; then
1435 s systemctl "$@"
1436 else
1437 if (( $# >= 3 )); then
1438 echo iank: ser expected 2 or less arguments
1439 return 1
1440 fi
1441 s service $2 $1
1442 fi
1443 }
1444 serstat() {
1445 systemctl -n 40 status "$@"
1446 }
1447
1448 seru() { systemctl --user "$@"; }
1449 # like restart, but do nothing if its not already started
1450 srestart() {
1451 local service=$1
1452 if [[ $(s systemctl --no-pager show -p ActiveState $service ) == ActiveState=active ]]; then
1453 systemctl restart $service
1454 fi
1455 }
1456
1457 setini() { # set a value in a .ini style file
1458 key="$1" value="$2" section="$3" file="$4"
1459 if [[ -s $file ]]; then
1460 sed -ri -f - "$file" <<EOF
1461 # remove existing keys
1462 / *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d}
1463 # add key
1464 /^\s*\[$section\]/a $key=$value
1465 # from section to eof, do nothing
1466 /^\s*\[$section\]/,\$b
1467 # on the last line, if we haven't found section yet, add section and key
1468 \$a [$section]\\
1469 $key=$value
1470 EOF
1471 else
1472 cat >"$file" <<EOF
1473 [$section]
1474 $key=$value
1475 EOF
1476 fi
1477 }
1478
1479 sgo() { # service go
1480 service=$1
1481 ser restart $service || return 1
1482 if type -p systemctl &>/dev/null; then
1483 ser enable $service
1484 fi
1485 }
1486 soff () {
1487 for service; do
1488 # ignore services that dont exist
1489 if systemctl cat $service &>/dev/null; then
1490 ser stop $service;
1491 ser disable $service
1492 fi
1493 done
1494 }
1495
1496 sgu() {
1497 systemctl list-unit-files | rg "$@"
1498 }
1499
1500
1501 sk() {
1502
1503
1504 # note, if you do something like this
1505 # x=( prefix* )
1506 # then disable the warning with:
1507 # shellcheck disable=SC2206 # globbing is intended
1508
1509 # 2029: "unescaped, this expands on the client side.": yes, I know how ssh works
1510 # 2164: "Use 'cd ... || exit' or 'cd ... || return' in case cd fails.": i have automatic error handling
1511 # 2086: unquoted $var: Quoting every var I set is way too much quotes.
1512 # 2068: Double quote array expansions to avoid re-splitting elements: same as above.
1513 # 2033: command arg is a function name: too many false positives.
1514
1515
1516 # these ones I had disabled, but without a good written explanation, so enabling them temporarily
1517 # 2046: unquoted $(cmd)
1518 # 2119: Functions with optional args get bad warnings when none are passed.
1519
1520 shellcheck -W 999 -x -e 2029,2164,2086,2068,2033 "$@" || return $?
1521 }
1522
1523
1524 # sl: ssh, but firsh rsync our bashrc and related files to a special
1525 # directory on the remote host if needed.
1526
1527 # Some environment variables and files need to be setup for this to work
1528 # (mine are set at the beginning of this file)
1529
1530 # SL_FILES_DIR: Environment variable. Path to folder which should at
1531 # least have a .bashrc file or symlink. This dir will be rsynced to ~ on
1532 # remote hosts (top level symlinks are resolved) unless the host already
1533 # has a $SL_FILES_DIR/.bashrc. In that case, we assume it is a host you
1534 # control and sync files to separately and already has the ~/.bashrc you
1535 # want. The remote bash will also take its .inputrc config from this
1536 # folder (default of not existing is fine). Mine looks like this:
1537 # https://iankelling.org/git/?p=distro-setup;a=tree;f=sl/.iank
1538
1539 # SL_INFO_DIR: Environment variable. This folder stores info about what
1540 # we detected on the remote system and when we last synced. It will be created
1541 # if it does not exist. Sometimes you may want to forget about a
1542 # remote system, you can use sl --rsync, or the function for that slr
1543 # below.
1544
1545 # SL_TEST_CMD: Env var. Meant to be used to vary the files synced
1546 # depending on the remote host. Run this string on the remote host the
1547 # first time sl is run (or if we run slr). The result is passed to
1548 # SL_TEST_HOOK. For example,
1549 # export SL_TEST_CMD=". /etc/os-release ; echo \${VERSION//[^a-zA-Z0-9]/}"
1550
1551 # SL_TEST_HOOK: Env var. It is run as $SL_TEST_HOOK. This can set
1552 # $SL_FILES_DIR to vary the files synced.
1553
1554 # SL_RSYNC_ARGS: Env var. String of arguments passed to rsync. For
1555 # example to exclude files within a directory. Note, excluded
1556 # files wont be deleted on rsync, you can add --delete-excluded
1557 # to the rsync command if that is desired.
1558
1559 # SL_SSH_ARGS: Env var. Default arguments passed to ssh.
1560
1561 # For when ~/.bashrc is already customized on the remote server, you
1562 # might find it problematic that ~/.bashrc is sourced for ALL ssh
1563 # commands, even in scripts. This paragraph is all about that. bash
1564 # scripts dont source ~/.bashrc, but call ssh in scripts and you get
1565 # ~/.bashrc. You dont want this. .bashrc is meant for interactive shells
1566 # and if you customize it, probably has bugs from time to time. This is
1567 # bad. Here's how I fix it. I have a special condition to "return" in my
1568 # .bashrc for noninteractive ssh shells (copy that code). Then use this
1569 # function or similar that passes LC_USEBASHRC=t when sshing and I want
1570 # my bashrc. Also, I don't keep most of my bashrc in .bashrc, i source a
1571 # separate file because even if I return early on, the whole file gets
1572 # parsed which can fail if there is a syntax error.
1573 sl() {
1574 # Background on LC_USEBASHRC var (no need to read if you just want to
1575 # use this function): env variables sent across ssh are strictly
1576 # limited, but we get LC_* at least in debian based machines, so we
1577 # just make that * be something no normal program would use. Note, on
1578 # hosts that dont allow LC_* I start an inner shell with LC_USEBASHRC
1579 # set, and the inner shell also allows running a nondefault
1580 # .bashrc. This means the outer shell still ran the default .bashrc,
1581 # but that is the best we can do.
1582
1583 local now args remote dorsync haveinfo tmpa sshinfo tmp tmp2 type info_sec force_rsync \
1584 sync_dirname testcmd extra_info testbool files_sec sl_test_cmd sl_test_hook
1585 declare -a args tmpa
1586
1587 args=($SL_SSH_ARGS)
1588
1589 # ssh [-1246Antivivisectionist] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
1590 # [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address]
1591 # [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option]
1592 # [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname
1593 # [command]
1594
1595 # ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
1596 # [-D [bind_address:]port] [-E log_file] [-e escape_char]
1597 # [-F configfile] [-I pkcs11] [-i identity_file]
1598 # [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
1599 # [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
1600 # [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
1601
1602 force_rsync=false
1603 if [[ $1 == --rsync ]]; then
1604 force_rsync=true
1605 shift
1606 fi
1607
1608 sl_test_cmd=$SL_TEST_CMD
1609 sl_test_hook=$SL_TEST_HOOK
1610 sl_rsync_args=$SL_RSYNC_ARGS
1611 while [[ $1 ]]; do
1612 case "$1" in
1613 --rsync)
1614 force_rsync=true
1615 ;;
1616 --sl-test-cmd)
1617 sl_test_cmd="$2"
1618 shift
1619 ;;
1620 --sl-test-hook)
1621 sl_test_hook="$2"
1622 shift
1623 ;;
1624 --sl-rsync-args)
1625 sl_rsync_args="$2"
1626 shift
1627 ;;
1628 *)
1629 break
1630 ;;
1631 esac
1632 shift
1633 done
1634
1635 while [[ $1 ]]; do
1636 case "$1" in
1637 # note we dont support things like -4oOption
1638 -[46AaCfGgKkMNnqsTtVvXxYy]*)
1639 args+=("$1"); shift
1640 ;;
1641 -[bcDEeFIiJLlmOopQRSWw]*)
1642 # -oOption etc is valid
1643 if (( ${#1} >= 3 )); then
1644 args+=("$1"); shift
1645 else
1646 args+=("$1" "$2"); shift 2
1647 fi
1648 ;;
1649 *)
1650 break
1651 ;;
1652 esac
1653 done
1654 remote="$1"
1655 if [[ ! $remote ]]; then
1656 echo $0: error hostname required >&2
1657 return 1
1658 fi
1659 shift
1660
1661 if [[ ! $SL_INFO_DIR ]]; then
1662 echo error: missing '$SL_INFO_DIR' env var >&2
1663 return 1
1664 fi
1665
1666 dorsync=false
1667 haveinfo=false
1668 tmpa=($SL_INFO_DIR/???????????"$remote")
1669 sshinfo=${tmpa[0]}
1670 if [[ -e $sshinfo ]]; then
1671 if $force_rsync; then
1672 rm -f $sshinfo
1673 else
1674 haveinfo=true
1675 fi
1676 fi
1677 if $haveinfo; then
1678 tmp=${sshinfo[0]##*/}
1679 tmp2=${tmp::11}
1680 type=${tmp2: -1}
1681 extra_info=$(cat $sshinfo)
1682 else
1683 # we test for string to know ssh succeeded
1684 testbool="test -e $SL_FILES_DIR/.bashrc -a -L .bashrc -a -v LC_USEBASHRC"
1685 testcmd="if $testbool; then printf y; else printf n; fi"
1686 if ! tmp=$(LC_USEBASHRC=y command ssh "${args[@]}" "$remote" "$testcmd; $sl_test_cmd"); then
1687 echo failed sl test. doing plain ssh -v
1688 command ssh -v "${args[@]}" "$remote"
1689 fi
1690 if [[ $tmp == y* ]]; then
1691 type=a
1692 else
1693 dorsync=true
1694 type=b
1695 fi
1696 extra_info="${tmp:1}"
1697 fi
1698 if [[ $sl_test_hook ]]; then
1699 RSYNC_RSH="ssh ${args[*]}" $sl_test_hook "$extra_info" "$remote"
1700 fi
1701
1702 if $haveinfo && [[ $type == b ]]; then
1703 info_sec=${tmp::10}
1704 read files_sec _ < <(find -L $SL_FILES_DIR -printf "%T@ %p\n" | sort -nr || [[ $? == 141 || ${PIPESTATUS[0]} == 32 ]] )
1705 files_sec=${files_sec%%.*}
1706 if (( files_sec > info_sec )); then
1707 dorsync=true
1708 rm -f $sshinfo
1709 fi
1710 fi
1711
1712 sync_dirname=${SL_FILES_DIR##*/}
1713
1714 if [[ ! $SL_FILES_DIR ]]; then
1715 echo error: missing '$SL_FILES_DIR' env var >&2
1716 return 1
1717 fi
1718
1719 if $dorsync; then
1720 RSYNC_RSH="ssh ${args[*]}" m rsync -rptL --delete $sl_rsync_args $SL_FILES_DIR "$remote":
1721 fi
1722 if $dorsync || ! $haveinfo; then
1723 sshinfo=$SL_INFO_DIR/$EPOCHSECONDS$type"$remote"
1724 [[ -e $SL_INFO_DIR ]] || mkdir -p $SL_INFO_DIR
1725 printf "%s\n" "$extra_info" >$sshinfo
1726 chmod 666 $sshinfo
1727 fi
1728 if [[ $type == b ]]; then
1729 if (( ${#@} )); then
1730 # Theres a couple ways to pass arguments, im not sure whats best,
1731 # but relying on bash 4.4+ escape quoting seems most reliable.
1732 command ssh "${args[@]}" "$remote" \
1733 LC_USEBASHRC=t bash -c '.\ '$sync_dirname'/.bashrc\;"\"\$@\""' bash ${@@Q}
1734 elif [[ ! -t 0 ]]; then
1735 # This case is when commands are being piped to ssh.
1736 # Normally, no bashrc gets sourced.
1737 # But, since we are doing all this, lets source it because we can.
1738 cat <(echo . $sync_dirname/.bashrc) - | command ssh "${args[@]}" "$remote" LC_USEBASHRC=t bash
1739 else
1740 command ssh -t "${args[@]}" "$remote" LC_USEBASHRC=t INPUTRC=$sync_dirname/.inputrc bash --rcfile $sync_dirname/.bashrc
1741 fi
1742 else
1743 if [[ -t 0 ]]; then
1744 LC_USEBASHRC=t command ssh "${args[@]}" "$remote" ${@@Q}
1745 else
1746 command ssh "${args[@]}" "$remote" LC_USEBASHRC=t bash
1747 fi
1748 fi
1749 # this function inspired from https://github.com/Russell91/sshrc
1750 }
1751
1752 slr() {
1753 sl --rsync "$@"
1754 }
1755 sss() { # ssh solo
1756 sl -oControlMaster=no -oControlPath=/ "$@"
1757 }
1758 # kill off old shared socket then ssh
1759 ssk() {
1760 m ssh -O exit "$@" || [[ $? == 255 ]]
1761 m sl "$@"
1762 }
1763 ccomp ssh sl slr sss ssk
1764 # plain ssh
1765 ssh() {
1766 if [[ $TERM == alacritty || $TERM == xterm-kitty ]]; then
1767 TERM=xterm-256color LC_USEBASHRC=t command ssh "$@"
1768 else
1769 LC_USEBASHRC=t command ssh "$@"
1770 fi
1771 }
1772
1773
1774 slog() {
1775 # log with script. timing is $1.t and script is $1.s
1776 # -l to save to ~/typescripts/
1777 # -t to add a timestamp to the filenames
1778 local logdir do_stamp arg_base
1779 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
1780 logdir="/a/dt/"
1781 do_stamp=false
1782 while getopts "lt" option
1783 do
1784 case $option in
1785 l ) arg_base=$logdir ;;
1786 t ) do_stamp=true ;;
1787 esac
1788 done
1789 shift $((OPTIND - 1))
1790 arg_base+=$1
1791 [[ -e $logdir ]] || mkdir -p $logdir
1792 $do_stamp && arg_base+=$(date +%F.%T%z)
1793 script -t $arg_base.s 2> $arg_base.t
1794 }
1795 splay() { # script replay
1796 #logRoot="$HOME/typescripts/"
1797 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
1798 scriptreplay "$1.t" "$1.s"
1799 }
1800
1801 sr() {
1802 # sudo redo. be aware, this command may not work right on strange distros or earlier software
1803 if [[ $# == 0 ]]; then
1804 sudo -E bash -c -l "$(history -p '!!')"
1805 else
1806 echo this command redos last history item. no argument is accepted
1807 fi
1808 }
1809
1810 srm () {
1811 # with -ll, less secure but faster.
1812 command srm -ll "$@"
1813 }
1814
1815 srun() {
1816 scp $2 $1:/tmp
1817 ssh $1 /tmp/${2##*/} $(printf "%q\n" "${@:2}")
1818 }
1819
1820
1821 swap() {
1822 local tmp
1823 tmp=$(mktemp)
1824 mv $1 $tmp
1825 mv $2 $1
1826 mv $tmp $2
1827 }
1828
1829 tclock() { # terminal clock
1830 local x
1831 clear
1832 date +%l:%_M
1833 len=60
1834 # this goes to full width
1835 #len=${1:-$((COLUMNS -7))}
1836 x=1
1837 while true; do
1838 if (( x == len )); then
1839 end=true
1840 d="$(date +%l:%_M) "
1841 else
1842 end=false
1843 d=$(date +%l:%M:%_S)
1844 fi
1845 echo -en "\r"
1846 echo -n "$d"
1847 for ((i=0; i<x; i++)); do
1848 if (( i % 6 )); then
1849 echo -n _
1850 else
1851 echo -n .
1852 fi
1853 done
1854 if $end; then
1855 echo
1856 x=1
1857 else
1858 x=$((x+1))
1859 fi
1860 sleep 5
1861 done
1862 }
1863
1864
1865 te() {
1866 # test existence / exists
1867 local ret=0
1868 for x in "$@"; do
1869 [[ -e "$x" || -L "$x" ]] || ret=1
1870 done
1871 return $ret
1872 }
1873
1874 psoff() {
1875 # normally, i would just execute these commands in the function.
1876 # however, DEBUG is not inherited, so we need to run it outside a function.
1877 # And we want to run set -x afterwards to avoid spam, so we cram everything
1878 # in here, and then it will run after this function is done.
1879 PROMPT_COMMAND='trap DEBUG; unset PROMPT_COMMAND; PS1="\w \$ "'
1880 }
1881 pson() {
1882 PROMPT_COMMAND=prompt-command
1883 if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
1884 trap 'settitle "$BASH_COMMAND"' DEBUG
1885 fi
1886 }
1887
1888 tx() { # toggle set -x, and the prompt so it doesnt spam
1889 if [[ $- == *x* ]]; then
1890 set +x
1891 pson
1892 else
1893 psoff
1894 fi
1895 }
1896
1897 psnetns() {
1898 # show all processes in the network namespace $1.
1899 # blank entries appear to be subprocesses/threads
1900 local x netns
1901 netns=$1
1902 ps -w | head -n 1
1903 sudo find -L /proc/[1-9]*/task/*/ns/net -samefile /run/netns/$netns | cut -d/ -f5 | \
1904 while read -r l; do
1905 x=$(ps -w --no-headers -p $l);
1906 if [[ $x ]]; then echo "$x"; else echo $l; fi;
1907 done
1908 }
1909 nonet() {
1910 if ! s ip netns list | grep -Fx nonet &>/dev/null; then
1911 s ip netns add nonet
1912 fi
1913 sudo -E env /sbin/ip netns exec nonet sudo -E -u iank /bin/bash
1914 }
1915
1916 m() { printf "%s\n" "$*"; "$@"; }
1917
1918 uptime() {
1919 if type -p uprecords &>/dev/null; then
1920 uprecords -B
1921 else
1922 command uptime
1923 fi
1924 }
1925
1926 virshrm() {
1927 for x in "$@"; do virsh destroy "$x"; virsh undefine "$x"; done
1928 }
1929
1930 vm-set-listen(){
1931 local t
1932 t=$(mktemp)
1933 local vm=$1
1934 local ip=$2
1935 sudo virsh dumpxml $vm | sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
1936 sed -r "s/listen='[^']+/listen='$ip/"> $t
1937 sudo virsh undefine $vm
1938 sudo virsh define $t
1939 }
1940
1941
1942 vmshare() {
1943 vm-set-listen $1 0.0.0.0
1944 }
1945
1946
1947 vmunshare() {
1948 vm-set-listen $1 127.0.0.1
1949 }
1950
1951 myiwscan() {
1952 # find input, copy to pattern space, when we find the first field, print the copy in different order without newlines.
1953 # instead of using labels, we could just match a line and group, eg: /signal:/,{s/signal:(.*)/\1/h}
1954 sudo iw dev wls1 scan | sed -rn "
1955 s/^\Wcapability: (.*)/\1/;Ta;h;b
1956 :a;s/^\Wsignal: -([^.]+).*/\1/;Tb;H;b
1957 # padded to min width of 20
1958 :b;s/\WSSID: (.*)/\1 /;T;s/^(.{20}(.*[^ ])?) */\1/;H;g;s/(.*)\n(.*)\n(.*)/\2 \3 \1/gp;b
1959 "|sort -r
1960 }
1961
1962 # * misc stuff
1963
1964
1965 if $use_color && type -p tput &>/dev/null; then
1966 term_bold="$(tput bold)"
1967 term_red="$(tput setaf 1)"
1968 term_green="$(tput setaf 2)"
1969 term_yellow="$(tput setaf 3)"
1970 term_purple="$(tput setaf 5)"
1971 term_nocolor="$(tput sgr0)" # no font attributes
1972
1973 # unused so far. commented for shellcheck
1974 # term_underl="$(tput smul)"
1975 # term_blue="$(tput setaf 4)"
1976 # term_cyan="$(tput setaf 6)"
1977 fi
1978 # Try to keep environment pollution down, EPA loves us.
1979 unset safe_term match_lhs use_color
1980
1981 # * prompt
1982
1983
1984 if [[ $- == *i* ]]; then
1985
1986
1987 case $HOSTNAME in
1988 bk|je|li)
1989 if [[ $EUID == 1000 ]]; then
1990 system-status _ ||:
1991 fi
1992 ;;
1993 esac
1994
1995
1996 # this needs to come before next ps1 stuff
1997 # this stuff needs bash 4, feb 2009,
1998 # old enough to no longer condition on $BASH_VERSION anymore
1999 shopt -s autocd
2000 shopt -s dirspell
2001 PS1='\w'
2002 if [[ $- == *i* ]] && [[ ! $LC_INSIDE_EMACS ]]; then
2003 PROMPT_DIRTRIM=2
2004 bind -m vi-command B:shell-backward-word
2005 bind -m vi-command W:shell-forward-word
2006 fi
2007
2008 if [[ $SSH_CLIENT || $SUDO_USER ]]; then
2009 unset PROMPT_DIRTRIM
2010 PS1="\h:$PS1"
2011 fi
2012
2013 # emacs terminal has problems if this runs slowly,
2014 # so I've thrown a bunch of things at the wall to speed it up.
2015 prompt-command() {
2016 local return=$? # this MUST COME FIRST
2017 local ps_char ps_color
2018 unset IFS
2019
2020 if [[ $HISTFILE ]]; then
2021 history -a # save history
2022 fi
2023
2024 # assigned in brc2
2025 # shellcheck disable=SC1303
2026 if [[ $jr_pid ]]; then
2027 if [[ -e /proc/$jr_pid ]]; then
2028 kill $jr_pid
2029 fi
2030 unset jr_pid
2031 fi
2032
2033 case $return in
2034 0) ps_color="$term_purple"
2035 ps_char='\$'
2036 ;;
2037 *) ps_color="$term_green"
2038 ps_char="$return \\$"
2039 ;;
2040 esac
2041 if [[ ! -O . ]]; then # not owner
2042 if [[ -w . ]]; then # writable
2043 ps_color="$term_bold$term_red"
2044 else
2045 ps_color="$term_bold$term_green"
2046 fi
2047 fi
2048
2049 # faster than sourceing the file im guessing
2050 if [[ -e /dev/shm/iank-status && ! -e /tmp/quiet-status ]]; then
2051 eval $(< /dev/shm/iank-status)
2052 fi
2053 if [[ $MAIL_HOST && $MAIL_HOST != "$HOSTNAME" ]]; then
2054 ps_char="@ $ps_char"
2055 fi
2056 # We could test if sudo is active with sudo -nv
2057 # but then we get an email and log of lots of failed sudo commands.
2058 # We could turn those off, but seems better not to.
2059 if [[ $EUID != 0 ]] && [[ $DID_SUDO ]]; then
2060 psudo="\[$term_bold$term_red\]s\[$term_nocolor\] "
2061 fi
2062 if [[ ! $HISTFILE ]]; then
2063 ps_char="NOHIST $ps_char"
2064 fi
2065 PS1="${PS1%"${PS1#*[wW]}"} $psudo\[$ps_color\]$ps_char\[$term_nocolor\] "
2066
2067 # set titlebar. instead, using more advanced
2068 # titelbar below
2069 #echo -ne "$_title_escape $HOSTNAME ${PWD/#$HOME/~} \007"
2070 }
2071 PROMPT_COMMAND=prompt-command
2072
2073 if [[ $TERM == screen* ]]; then
2074 _title_escape="\033]..2;"
2075 else
2076 # somme sites recommend this, i dunno what the diff is.
2077 #_title_escape="\033]30;"
2078 _title_escape="\033]0;"
2079 fi
2080
2081 settitle () {
2082 # this makes it so we show the current command if
2083 # one is running, otherwise, show nothing
2084
2085 if [[ $1 == prompt-command ]]; then
2086 return 0
2087 fi
2088 if (( ${#BASH_ARGC[@]} == 1 && BASH_SUBSHELL == 0 )); then
2089 echo -ne "$_title_escape ${PWD/#$HOME/~} "
2090 printf "%s" "$*"
2091 echo -ne "\007"
2092 fi
2093 }
2094
2095 # note, this wont work:
2096 # x=$(mktemp); cp a $x
2097 # I havnt figured out why, bigger fish to fry.
2098 #
2099 # for titlebar.
2100 # condition from the screen man page i think.
2101 # note: duplicated in tx()
2102 if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
2103 trap 'settitle "$BASH_COMMAND"' DEBUG
2104 else
2105 trap DEBUG
2106 fi
2107
2108 fi
2109
2110 # * stuff that makes sense to be at the end
2111
2112
2113 # best practice
2114 unset IFS
2115
2116 # shellcheck disable=SC1090
2117 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
2118
2119 # I had this idea to start a bash shell which would run an initial
2120 # command passed through this env variable, then continue on
2121 # interactively. But the use case I had in mind went away.
2122 #
2123 # if [[ $MY_INIT_CMD ]]; then
2124 # "${MY_INIT_CMD[@]}"
2125 # unset MY_INIT_CMD
2126 # fi
2127
2128 # ensure no bad programs appending to this file will have an affect
2129 return 0