mostly features, some minor bug 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 # a useful flag is -F aka --quit-if-one-screen
228 export LESS=RXij12
229 export SYSTEMD_LESS=$LESS
230
231 export NNN_COLORS=2136
232
233 export SL_FILES_DIR=/b/ds/sl/.iank
234 export SL_INFO_DIR=/p/sshinfo
235
236
237 # * include files
238
239 if [[ -s $bashrc_dir/path-add-function ]]; then
240 source $bashrc_dir/path-add-function
241 if [[ $SSH_CLIENT ]]; then
242 if grep -qF /home/iank/.iank/e/e /etc/exports &>/dev/null; then
243 export EMACSDIR=/home/iank/.iank/e/e
244 fi
245 path-add $bashrc_dir
246 fi
247 fi
248
249 # if someone exported $SOE (stop on error), catch errors.
250 #
251 # Note, on debian this results in the following warning when in ssh,
252 # hich I haven't figured out how to fix. It doesn't happen if we source
253 # after the shell has started
254 #
255 # bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
256 # bash: warning: cannot start debugger; debugging mode disabled
257 if [[ $SOE ]]; then
258 if [[ -e /a/bin/errhandle/err ]]; then
259 source /a/bin/errhandle/err
260 fi
261 fi
262
263
264
265
266 mysrc() {
267 local path dir file
268 path=$1
269 dir=${path%/*}
270 file=${path##*/}
271 if [[ -s $path ]]; then
272 source $path
273 elif [[ -s $bashrc_dir/$file ]]; then
274 source $bashrc_dir/$file
275 fi
276 }
277
278
279 mysrc /a/bin/small-misc-bash/ll-function
280 mysrc /a/bin/distro-functions/src/package-manager-abstractions
281
282 # things to remember:
283 # ALT-C - cd into the selected directory
284 # CTRL-T - Paste the selected file path into the command line
285 #
286 # good guide to some of its basic features is the readme file
287 # https://github.com/junegunn/fzf
288
289 # if [[ -s /usr/share/doc/fzf/examples/key-bindings.bash ]]; then
290 # source /usr/share/doc/fzf/examples/key-bindings.bash
291 # fi
292
293 # * functions
294
295 ### begin FSF section ###
296
297 # Comments before functions are meant to be good useful
298 # documentation. If they fail at that, please improve them or send Ian a
299 # note.
300
301 ## copy bash completion
302 # Usage: ORIGINAL_COMMAND TARGET_COMMAND...
303 #
304 # It copies how the bash completion works from one command to other
305 # commands.
306 ccomp() {
307 local c src
308 src=$1
309 shift
310 if ! c=$(complete -p $src 2>/dev/null); then
311 _completion_loader $src &>/dev/null ||:
312 c=$(complete -p $src 2>/dev/null) || return 0
313 fi
314 # remove $src( .*|$)
315 c=${c% $src}
316 c=${c%% $src *}
317 eval $c $*
318 }
319
320 ## directory history tracking and navigation.
321 #
322 # cd becomes a function, also aliased to c. b to go back, f to go
323 # forward, cl to list recent directories and choose one.
324 #
325 # The finer details you may want to skip:
326 #
327 # We also define bl to print the list of back and forward directories.
328 #
329 # We keep 2 stacks, forward and back. Unlike with a web browser, the
330 # forward stack is not erased when going somewhere new.
331 #
332 # Recent directories are stored in ~/.cdirs.
333 #
334 declare -a _dir_forward _dir_back
335 c() {
336 # normally, the top of _dir_back is our current dir. if it isn't,
337 # put it on there, except we don't want to do that when we
338 # just launched a shell
339 if [[ $OLDPWD ]]; then
340 if (( ${#_dir_back[@]} == 0 )) || [[ ${_dir_back[-1]} != "$PWD" ]]; then
341 _dir_back+=("$PWD")
342 fi
343 fi
344 command cd "$@"
345 if (( ${#_dir_back[@]} == 0 )) || [[ ${_dir_back[-1]} != "$PWD" ]]; then
346 _dir_back+=("$PWD")
347 fi
348 echo "$PWD" >> ~/.cdirs
349 }
350 ccomp cd c
351
352 # back
353 b() {
354 local top_back
355 if (( ${#_dir_back[@]} == 0 )); then
356 echo "nothing left to go back to" >&2
357 return 0
358 fi
359 top_back="${_dir_back[-1]}"
360
361 if [[ $top_back == "$PWD" ]] && (( ${#_dir_back[@]} == 1 )); then
362 echo "already on last back entry" >&2
363 return 0
364 fi
365
366
367 if [[ $top_back == "$PWD" ]]; then
368 # add to dirf if not already there
369 if (( ${#_dir_forward[@]} == 0 )) || [[ ${_dir_forward[-1]} != "$top_back" ]]; then
370 _dir_forward+=("$top_back")
371 fi
372 unset "_dir_back[-1]"
373 command cd "${_dir_back[-1]}"
374 else
375 if (( ${#_dir_forward[@]} == 0 )) || [[ ${_dir_forward[-1]} != "$PWD" ]]; then
376 _dir_forward+=("$PWD")
377 fi
378 command cd "$top_back"
379 fi
380
381 # Interesting feature, not sure I want it.
382 # give us a peek at what is next in the list
383 # if (( ${#_dir_back[@]} >= 2 )); then
384 # printf "%s\n" "${_dir_back[-2]}"
385 # fi
386 #
387
388 # c/b/f Implementation notes:
389 #
390 # The top of the back is $PWD
391 # as long as the last directory change was due to c,b,or cl.
392 #
393 # Example of stack changes:
394 #
395 # a b c (d)
396 ## back
397 # a b (c)
398 # d
399 #back
400 #a (b)
401 #d c
402 #back
403 #(a)
404 #d c b
405 #forward
406 #a (b)
407 #d c
408 #
409 # a b c
410 ## back
411 # a b
412 # (c)
413 ## forward
414
415 }
416 # forward
417 f() {
418 local top_forward
419 if (( ${#_dir_forward[@]} == 0 )); then
420 echo "no forward dir left" >&2
421 return 0
422 fi
423 top_forward="${_dir_forward[-1]}"
424 unset "_dir_forward[-1]"
425 c "$top_forward"
426
427 # give us a peek at what is next in the list
428 # if (( ${#_dir_forward[@]} )); then
429 # printf "%s\n" "${_dir_forward[-1]}"
430 # fi
431 }
432 # cd list
433 cl() {
434 local i line input start
435 local -A buttondirs alines
436 local -a buttons dirs lines
437 buttons=( {a..z} {2..9} )
438 if [[ ! -s ~/.cdirs ]]; then
439 echo nothing in ~/.cdirs
440 return 0
441 fi
442
443 i=0
444
445 mapfile -t lines <~/.cdirs
446 start=$(( ${#lines[@]} - 1 ))
447
448 # we have ~33 buttons as of this writing, so lets
449 # prune down the history every once in a while.
450 if (( start > 500 )); then
451 tac ~/.cdirs | awk '!seen[$0]++' | head -n 200 | tac | sponge ~/.cdirs || [[ $? == 141 ]]
452 fi
453
454 for (( j=$start; j >= 0; j-- )); do
455 line="${lines[$j]}"
456 if [[ ! $line || ${alines[$line]} || ! -d "$line" || $line == "$PWD" || line == "$HOME" ]]; then
457 continue
458 fi
459 alines[$line]=t
460 buttondirs[${buttons[i]}]="$line"
461 printf "%s %s\n" ${buttons[i]} "$line"
462 if (( i == ${#buttons[@]} - 1 )); then
463 break
464 fi
465 i=$(( i + 1 ))
466 done
467
468 if (( i == 0 )); then
469 echo "no dirs in ~/.cdirs"
470 return 0
471 fi
472 read -r -N 1 input
473 if [[ $input != $'\n' ]]; then
474 c "${buttondirs[$input]}"
475 fi
476 }
477 # back list
478 bl() {
479 local start i j max
480 max=10
481 start=$(( ${#_dir_back[@]} - 1 ))
482
483 # cleanup possible repeating of pwd
484 if (( start >= 0 )) && [[ ${_dir_back[$start]} == "$PWD" ]]; then
485 start=$(( start - 1 ))
486 fi
487 j=1
488 if (( start >= 0 )); then
489 for (( i=$start; i >= 0 ; i-- )); do
490 printf "%s %s\n" $j ${_dir_back[i]}
491 j=$(( j + 1 ))
492 if (( j >= max )); then
493 break
494 fi
495 done
496 fi
497
498 max=10
499 start=$(( ${#_dir_forward[@]} - 1 ))
500
501 # cleanup possible repeating of pwd
502 if (( start >= 0 )) && [[ ${_dir_forward[$start]} == "$PWD" ]]; then
503 start=$(( start - 1 ))
504 fi
505 if (( start < 0 )); then
506 return 0
507 fi
508 echo --
509 j=1
510 for (( i=$start; i >= 0 ; i-- )); do
511 printf "%s %s\n" $j ${_dir_forward[i]}
512 j=$(( j + 1 ))
513 if (( j >= max )); then
514 break
515 fi
516 done
517 }
518
519 # pee do. run args as a command with output copied to syslog.
520 #
521 # Usage: pd [-t TAG] COMMAND...
522 #
523 # -t TAG Override the tag in the syslog. The default is COMMAND with
524 # any path part is removed, eg. for /bin/cat the tag is cat.
525 #
526 # You can view the log via "journalctl -t TAG"
527 pd() {
528 local tag ret
529 ret=0
530 tag=${1##*/}
531 case $1 in
532 -t) tag="$2"; shift 2 ;;
533 esac
534 echo "PWD=$PWD command: $*" | logger -t $tag
535 "$@" |& pee cat "logger -t $tag" || ret=$?
536 echo "exited with status=$ret" | pee cat "logger -t $tag"
537 # this avoids any err-catch
538 (( $ret == 0 )) || return $ret
539 }
540 ccomp time pd
541
542 # jdo = journal do. Run command as transient systemd service, tailing
543 # its output in the journal until it completes.
544 #
545 # Usage: jdo COMMAND...
546 #
547 # Compared to pd: commands recognize this is a non-interactive shell.
548 # The service is unaffected if our ssh connection dies, no need to run
549 # in screen or tmux.
550 #
551 # Note: The last few lines of any existing entries for a unit by that
552 # name will be output first, and there will be a few second delay at the
553 # start of the command, and a second or so at the end.
554 #
555 # Note: Functions and aliases obviously won't work, we resolve the
556 # command to a file.
557 #
558 # Note: requires running as root.
559 jdo() {
560 local cmd cmd_name jr_pid ret
561 ret=0
562 cmd="$1"
563 shift
564 if [[ $EUID != 0 ]]; then
565 echo "jdo: error: rerun as root"
566 return 1
567 fi
568 cmd_name=${cmd##*/}
569 if [[ $cmd != /* ]]; then
570 cmd=$(type -P "$cmd")
571 fi
572 # -q = quiet
573 journalctl -qn2 -f -u "$cmd_name" &
574 # Trial and error of time needed to avoid missing initial lines.
575 # .5 was not reliable. 1 was not reliable. 2 was not reliable
576 sleep 4
577 jr_pid=$!
578 systemd-run --unit "$cmd_name" --wait --collect "$cmd" "$@" || ret=$?
579 # The sleep lets the journal output its last line
580 # before the prompt comes up.
581 sleep .5
582 kill $jr_pid &>/dev/null ||:
583 unset jr_pid
584 fg &>/dev/null ||:
585 # this avoids any err-catch
586 (( $ret == 0 )) || return $ret
587 }
588 ccomp time jdo
589 #### end fsf section
590
591
592 ..() { c ..; }
593 ...() { c ../..; }
594 ....() { c ../../..; }
595 .....() { c ../../../..; }
596 ......() { c ../../../../..; }
597
598 chere() {
599 local f path
600 for f; do
601 path=$(readlink -e "$f")
602 echo "cat >$path <<'EOF'"
603 cat "$f"
604 echo EOF
605 done
606 }
607
608
609 # file cut copy and paste, like the text buffers :)
610 # I havnt tested these.
611 _fbufferinit() { # internal use
612 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
613 rm -rf "${my_f_tempdir:?}"/*
614 }
615 fcp() { # file cp
616 _fbufferinit
617 cp "$@" "$my_f_tempdir"/
618 }
619 fct() { # file cut
620 _fbufferinit
621 mv "$@" "$my_f_tempdir"/
622 }
623 fpst() { # file paste
624 [[ $2 ]] && { echo too many arguments; return 1; }
625 target=${1:-.}
626 cp "$my_f_tempdir"/* "$target"
627 }
628
629 _khfix_common() {
630 local host ip port file key
631 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" ||: )
632 file=$(readlink -f ~/.ssh/known_hosts)
633 if [[ ! $ip ]]; then
634 echo "khfix: ssh failed"
635 return 1
636 fi
637 if [[ $port != 22 ]]; then
638 ip_entry="[$ip]:$port"
639 host_entry="[$host]:$port"
640 else
641 ip_entry=$ip
642 host_entry=$host
643 fi
644 tmpfile=$(mktemp)
645 if [[ $host != $ip ]]; then
646 key=$(ssh-keygen -F "$host_entry" -f $file | sed -r 's/^.*([^ ]+ +[^ ]+) *$/\1/')
647 if [[ $key ]]; then
648 grep -Fv "$key" "$file" | sponge "$file"
649 fi
650 fi
651 key=$(ssh-keygen -F "$ip_entry" -f $file | sed -r 's/^.*([^ ]+ +[^ ]+) *$/\1/')
652 if [[ $key ]]; then
653 grep -Fv "$key" "$file" | sponge "$file"
654 fi
655 ll ~/.ssh/known_hosts
656 rootsshsync
657 }
658 khfix() { # known hosts fix
659 _khfix_common "$@" || return 1
660 ssh $1 :
661 }
662 khcopy() {
663 _khfix_common "$@"
664 ssh-copy-id $1
665 }
666
667 a() {
668 local x
669 x=$(readlink -nf "${1:-$PWD}")
670 # yes, its kinda dumb that xclip/xsel cant do this in one invocation
671 echo -n "$x" | xclip -selection clipboard
672 echo -n "$x" | xclip
673 }
674
675 # a1 = awk {print $1}
676 for field in {1..20}; do
677 eval a$field"() { awk '{print \$$field}'; }"
678 done
679 # h1 = head -n1
680 for num in {1..9}; do
681 eval h$num"() { head -n$num; }"
682 done
683
684
685 hexipv4() {
686 printf '%d.%d.%d.%d\n' $(echo $1 | sed 's/../0x& /g')
687 }
688
689 vp9() {
690 local f out outdir in
691 outdir=vp9
692 case $1 in
693 --out)
694 outdir=$2
695 shift 2
696 ;;
697 esac
698 m mkdir -p $outdir
699 for f; do
700 out=$PWD/$outdir/$f
701 in=$PWD/$f
702 m cd $(mktemp -d)
703 pwd
704 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
705 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
706 cd -
707 done
708 }
709
710 utcl() { # utc 24 hour time to local hour 24 hour time
711 echo "print( ($1 $(date +%z | sed -r 's/..$//;s/^(-?)0*/\1/')) % 24)"|python3
712 }
713
714 bwm() {
715 s bwm-ng -T avg -d
716 }
717
718
719 # for running in a fai rescue. iank specific.
720 kdrescue() {
721 d=vgata-Samsung_SSD_850_EVO_2TB_S2RLNX0J502123D
722 for f in $d vgata-Samsung_SSD_870_QVO_8TB_S5VUNG0N900656V; do
723 cryptsetup luksOpen --key-file /p /dev/$f/root crypt-$f-root
724 cryptsetup luksOpen --key-file /p /dev/$f/o crypt-$f-o
725 done
726 mount -o subvol=root_trisquelaramo /dev/mapper/crypt-$d-root /mnt
727 mount -o subvol=a /dev/mapper/crypt-$d-root /mnt/a
728 mount -o subvol=o /dev/mapper/crypt-$d-o /mnt/o
729 mount -o subvol=boot_trisquelaramo /dev/sda2 /mnt/boot
730 cd /mnt
731 chrbind
732 }
733
734
735
736
737 c4() { c /var/log/exim4; }
738
739 caa() { git commit --amend --no-edit -a; }
740
741 cf() {
742 for f; do
743 hr
744 echo "$f"
745 hr
746 cat "$f"
747 done
748 }
749 caf() {
750 # shellcheck disable=SC2033
751 find -L "$@" -type f -not \( -name .svn -prune -o -name .git -prune \
752 -o -name .hg -prune -o -name .editor-backups -prune \
753 -o -name .undo-tree-history -prune \) \
754 -exec bash -c '. ~/.bashrc; hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
755
756 }
757 ccomp cat cf caf
758
759 calc() { echo "scale=3; $*" | bc -l; }
760 # no having to type quotes, but also no command history:
761 clc() {
762 local x
763 read -r x
764 echo "scale=3; $x" | bc -l
765 }
766
767 cam() {
768 git commit -am "$*"
769 }
770
771 ccat () { # config cat. see a config without extra lines.
772 sed -r '/^[[:space:]]*([;#]|--|\/\/|$)/d' "$@"
773 }
774 ccomp grep ccat
775
776 chrbind() {
777 local d
778 # dev/pts needed for pacman signature check
779 for d in dev proc sys dev/pts; do
780 [[ -d $d ]]
781 if ! mountpoint $d &>/dev/null; then
782 m s mount -o bind /$d $d
783 fi
784 done
785 }
786 chumount() {
787 local d
788 # dev/pts needed for pacman signature check
789 for d in dev/pts dev proc sys; do
790 [[ -d $d ]]
791 if mountpoint $d &>/dev/null; then
792 m s umount $d
793 fi
794 done
795 }
796
797
798 _cdiff-prep() {
799 # join options which are continued to multiples lines onto one line
800 local first=true
801 while IFS= read -r line; do
802 # remove leading spaces/tabs. assumes extglob
803 if [[ $line == "[ ]*" ]]; then
804 line="${line##+( )}"
805 fi
806 if $first; then
807 pastline="$line"
808 first=false
809 elif [[ $line == *=* ]]; then
810 echo "$pastline" >> "$2"
811 pastline="$line"
812 else
813 pastline="$pastline $line"
814 fi
815 done < <(grep -vE '^([ \t]*#|^[ \t]*$)' "$1")
816 echo "$pastline" >> "$2"
817 }
818
819 cdiff() {
820 # diff config files,
821 # setup for format of postfix, eg:
822 # option = stuff[,]
823 # [more stuff]
824 local pastline unified f1 f2
825 unified="$(mktemp)"
826 f1="$(mktemp)"
827 f2="$(mktemp)"
828 _cdiff-prep "$1" "$f1"
829 _cdiff-prep "$2" "$f2"
830 cat "$f1" "$f2" | grep -Po '^[^=]+=' | sort | uniq > "$unified"
831 while IFS= read -r line; do
832 # the default bright red / blue doesnt work in emacs shell
833 dwdiff -cblue,red -A best -d " ," <(grep "^$line" "$f1" || echo ) <(grep "^$line" "$f2" || echo ) | colordiff
834 done < "$unified"
835 }
836
837
838 cat-new-files() {
839 local start=$SECONDS
840 local dir="$1"
841 # shellcheck disable=SC2030
842 inotifywait -m "$dir" -e create -e moved_to | \
843 while read -r filedir _ file; do
844 cat "$filedir$file"
845 hr
846 calc $((SECONDS - start)) / 60
847 sleep 5
848 done
849
850 }
851
852 chownme() {
853 s chown -R $USER:$USER "$@"
854 }
855
856 # shellcheck disable=SC2032
857 chown() {
858 # makes it so chown -R symlink affects the symlink and its target.
859 if [[ $1 == -R ]]; then
860 shift
861 command chown -h "$@"
862 command chown -R "$@"
863 else
864 command chown "$@"
865 fi
866 }
867
868 cim() {
869 git commit -m "$*"
870 }
871
872
873 d() { builtin bg "$@"; }
874 ccomp bg d
875
876 # f would be more natural, but i already am using it for something
877 z() { builtin fg "$@"; }
878 ccomp fg z
879
880 x() { builtin kill %%; }
881
882 dc() {
883 diff --strip-trailing-cr -w "$@" # diff content
884 }
885 ccomp diff dc
886
887 despace() {
888 local x y
889 for x in "$@"; do
890 y="${x// /_}"
891 safe_rename "$x" "$y"
892 done
893 }
894
895 # df progress
896 # usage: dfp MOUNTPOINT [SECOND_INTERVAL]
897 # SECOND_INTERVAL defaults to 90
898 dfp() {
899 # mp = mountpoint
900 local a b mp interval
901 mp=$1
902 interval=${2:-90}
903 if [[ ! $mp ]]; then
904 echo "dfp: error, missing 1st arg" >&2
905 return 1
906 fi
907 while true; do
908 a=$(df --output=used $mp | tail -n1)
909 sleep $interval
910 b=$(df --output=used $mp | tail -n1)
911 printf "used mib: %'d mib/min: %s\n" $(( b /1000 )) $(( (b-a) / (interval * 1000 / 60 ) ))
912 done
913 }
914
915 # get ipv4 ip from HOST. or if it is already a number, return that
916 hostip() {
917 local host="$1"
918 case $host in
919 [0-9:])
920 echo "$host"
921 ;;
922 *)
923 getent ahostsv4 "$host" | awk '{ print $1 }' | head -n1
924 ;;
925 esac
926 }
927
928 dig() {
929 command dig +nostats +nocmd "$@"
930 }
931 # Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed.
932 digsort() {
933 local sec
934 sec=
935 dig +nordflag "$@" | sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' | while read -r l; do
936 if [[ $l == [^\;]* ]]; then
937 sec+="$l"$'\n'
938 else
939 if [[ $sec ]]; then
940 printf "%s" "$sec" | sort
941 sec=
942 fi
943 printf "%s\n" "$l"
944 fi
945 done
946 }
947 ccomp dig digsort
948 # compare digs to the 2 servers
949 # usage: digdiff @server1 @server2 DIG_ARGS
950 # note: only the soa master nameserver will respond with
951 # ra "recursive answer" flag. That difference is meaningless afaik.
952 digdiff() {
953 local s1 s2
954 s1=$1
955 shift
956 s2=$1
957 shift
958 digsort $s1 "$@" | tee /tmp/digdiff
959 diff -u /tmp/digdiff <(digsort $s2 "$@")
960 }
961
962 # date in a format i like reading
963 dt() {
964 date "+%A, %B %d, %r" "$@"
965 }
966 dtr() {
967 date -R "$@"
968 }
969 # date with all digits in a format i like
970 dtd() {
971 date +%F_%T% "$@"
972 }
973 ccomp date dt dtr dtd
974
975 dus() { # du, sorted, default arg of
976 du -sh ${@:-*} | sort -h
977 }
978 ccomp du dus
979
980
981 e() { printf "%s\n" "$*"; }
982
983 # echo args
984 ea() {
985 if (( ! $# )); then
986 echo no args
987 fi
988 for arg; do
989 printf "%qEOL\n" "${arg}"
990 printf "%s" "${arg}" |& hexdump -C
991 done
992 }
993
994 # echo variables. print var including escapes, etc, like xxd for variable
995 ev() {
996 if (( ! $# )); then
997 echo no args
998 fi
999 for arg; do
1000 if [[ -v $arg ]]; then
1001 printf "%qEOL\n" "${!arg}"
1002 printf "%s" "${!arg}" |& hexdump -C
1003 else
1004 echo arg $arg is unset
1005 fi
1006 done
1007 }
1008
1009 ediff() {
1010 [[ ${#@} == 2 ]] || { echo "error: ediff requires 2 arguments"; return 1; }
1011 emacs --eval "(ediff-files \"$1\" \"$2\")"
1012 }
1013
1014 # mail related
1015 etail() {
1016 ngset
1017 tail -F /var/log/exim4/mainlog /var/log/exim4/*main /var/log/exim4/paniclog /var/log/exim4/*panic -n 200 "$@"
1018 ngreset
1019 }
1020 etailm() {
1021 tail -F /var/log/exim4/mainlog -n 200 "$@"
1022 }
1023 etail2() {
1024 tail -F /var/log/exim4/mymain -n 200 "$@"
1025 }
1026 ccomp tail etail etail2
1027
1028
1029 showkeys() {
1030 ssh "$@" cat .ssh/authorized_keys{,2}
1031 }
1032
1033
1034 # print exim old pids
1035 eoldpids() {
1036 local configtime pid piduptime now daemonpid
1037 printf -v now '%(%s)T' -1
1038 configtime=$(stat -c%Y /var/lib/exim4/config.autogenerated)
1039 if [[ -s /run/exim4/exim.pid ]]; then
1040 daemonpid=$(cat /run/exim4/exim.pid)
1041 fi
1042 for pid in $(pgrep -f '^/usr/sbin/exim4( |$)'); do
1043 # the daemonpid gets reexeced on HUP (service reloads), keeping its same old timestamp
1044 if [[ $pid == $daemonpid ]]; then
1045 continue
1046 fi
1047 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
1048 if (( configtime > now - piduptime )); then
1049 echo $pid
1050 fi
1051 done
1052 }
1053
1054 # exim tail but only watch lines from new pids
1055 etailnew() {
1056 local pid oldpids
1057 for pid in $(eoldpids); do
1058 oldpids+="$pid|"
1059 done
1060 if [[ $oldpids ]]; then
1061 etail | awk '$3 !~ /^\[('"${oldpids%|}"')\]$/'
1062 else
1063 etail
1064 fi
1065 }
1066 # exim watch as old pids go away
1067 ewatchold() {
1068 local configtime pid piduptime now
1069 local -i count
1070 local -a oldpids
1071 count=0
1072 while true; do
1073 oldpids=($(eoldpids))
1074 if (( ! ${#oldpids[@]} )); then
1075 return
1076 fi
1077 # print the date every 20 iterations
1078 if (( ! count % 20 )); then
1079 date
1080 fi
1081 count+=1
1082 ps -f -p "${oldpids[*]}"
1083 sleep 1
1084 done
1085 }
1086
1087 eless() {
1088 less /var/log/exim4/mainlog
1089 }
1090 ccomp less eless
1091 eqcat() {
1092 exiqgrep -ir.\* -o 60 | while read -r i; do
1093 hlm exim -Mvc $i
1094 echo
1095 hlm exigrep $i /var/log/exim4/mainlog | cat ||:
1096 done
1097 }
1098 eqrmf() {
1099 # other ways to get the list of message ids:
1100 # exim -bp | awk 'NF == 4 {print $3}'
1101 # # this is slower 160ms, vs 60.
1102 # exipick -i
1103 exiqgrep -ir.\* | xargs exim -Mrm
1104 }
1105
1106 econfdevnew() {
1107 rm -rf /tmp/edev
1108 mkdir -p /tmp/edev/etc
1109 cp -ra /etc/exim4 /tmp/edev/etc
1110 cp -ra /etc/alias* /tmp/edev/etc
1111 find /tmp/edev/etc/exim4 -type f -execdir sed -i "s,/etc/,/tmp/edev/etc/,g" '{}' +
1112 econfdev
1113 }
1114 econfdev() {
1115 update-exim4.conf -d /tmp/edev/etc/exim4 -o /tmp/edev/e.conf
1116 }
1117
1118 # exim grep in
1119 # show important information about incoming mail in the exim log
1120 egrin() {
1121 sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).*T="(.*)" from (<[^ ]+> .*$)/\1 \4\n \3/p' <${1:-/var/log/exim4/mainlog}
1122 }
1123
1124 # 2nd line is message-id:
1125 egrinid() {
1126 sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).* id=([^ ]+) T="(.*)" from (<[^ ]+> .*$)/\1 \5\n \3\n \4/p' <${1:-/var/log/exim4/mainlog}
1127 }
1128 etailin() {
1129 tail -F /var/log/exim4/mainlog | sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).*T="(.*)" from (<[^ ]+> .*$)/\1 \4\n \3/p'
1130 }
1131
1132
1133
1134
1135 fa() {
1136 # find array. make an array of file names found by find into $x
1137 # argument: find arguments
1138 # return: find results in an array $x
1139 while read -rd ''; do
1140 x+=("$REPLY");
1141 done < <(find "$@" -print0);
1142 }
1143
1144 faf() { # find all files. use -L to follow symlinks
1145 find "$@" -not \( -name .svn -prune -o -name .git -prune \
1146 -o -name .hg -prune -o -name .editor-backups -prune \
1147 -o -name .undo-tree-history -prune \) -type f 2>/dev/null
1148 }
1149
1150 # usage ffconcat FILES_TO_CONCAT OUTPUT_FILE
1151 ffconcat() {
1152 local tmpf
1153 tmpf=$(mktemp)
1154 printf "file '%s'\n" "$1" >$tmpf
1155 while (( $# > 1 )); do
1156 shift
1157 printf "file '%s'\n" "$1" >>$tmpf
1158 done
1159 # https://trac.ffmpeg.org/wiki/Concatenate
1160 ffmpeg -f concat -safe 0 -i $tmpf -c copy "$1"
1161 rm $tmpf
1162 }
1163 ffremux() {
1164 local tmpf tmpd
1165 if (( $# == 0 )); then
1166 echo ffremux error expected args >&2
1167 return 1
1168 fi
1169 tmpd=$(mktemp -d)
1170 for f; do
1171 tmpf=$tmpd/"${f##*/}"
1172 ffmpeg -i "$f" -c:v copy -c:a copy $tmpf
1173 cat $tmpf >"$f"
1174 done
1175 rm -r $tmpd
1176 }
1177
1178
1179
1180 # absolute path of file/dir without resolving symlinks.
1181 #
1182 # This is what realpath -s does in most cases, but sometimes it
1183 # actually resolves symlinks, at least when they are in /.
1184 #
1185 # Note, if run on a dir, if the final component is relative, it won't
1186 # resolve that. Use the below fpd for that.
1187 #
1188 # note: we could make a variation of this which
1189 # assigns to a variable name using eval, so that we don't have to do
1190 # x=$(fp somepath), which might save subshell overhead and look nice,
1191 # but I'm not going to bother.
1192 fp() {
1193 local initial_oldpwd initial_pwd dir base
1194 initial_oldpwd="$OLDPWD"
1195 initial_pwd="$PWD"
1196 if [[ $1 == */* ]]; then
1197 dir="${1%/*}"
1198 base="/${1##*/}"
1199 # CDPATH because having it set will cause cd to possibly print output
1200 CDPATH= cd "$dir"
1201 printf "%s%s\n" "$PWD" "$base"
1202 CDPATH= cd "$initial_pwd"
1203 OLDPWD="$initial_oldpwd"
1204 else
1205 printf "%s/%s\n" "$PWD" "$1"
1206 fi
1207 }
1208 # full path of directory without resolving symlinks
1209 fpd() {
1210 local initial_oldpwd initial_pwd dir
1211 initial_oldpwd="$OLDPWD"
1212 initial_pwd="$PWD"
1213 dir="$1"
1214 CDPATH= cd "$dir"
1215 printf "%s%s\n" "$PWD" "$base"
1216 cd "$initial_pwd"
1217 OLDPWD="$initial_oldpwd"
1218 }
1219
1220
1221 # mail related
1222 frozen() {
1223 rm -rf /tmp/frozen
1224 sudo mailq |gr frozen|awk '{print $3}' | while read -r id; do
1225 sudo exim -Mvl $id
1226 echo
1227 sudo exim -Mvh $id
1228 echo
1229 sudo exim -Mvb $id
1230 echo -e '\n\n##############################\n'
1231 done | tee -a /tmp/frozen
1232 }
1233 frozenrm() {
1234 local ids=()
1235 while read -r line; do
1236 printf '%s\n' "$line"
1237 ids+=($(printf '%s\n' "$line" |gr frozen|awk '{print $3}'))
1238 done < <(s mailq)
1239 echo "sleeping for 2 in case you change your mind"
1240 sleep 2
1241 sudo exim -Mrm "${ids[@]}"
1242 }
1243
1244 funce() {
1245 # like -e for functions. returns on error.
1246 # at the end of the function, disable with:
1247 # trap ERR
1248 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
1249 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"
1250 trap ERR
1251 return' ERR
1252 }
1253
1254 getdir () {
1255 local help="Usage: getdir [--help] PATH
1256 Output the directory of PATH, or just PATH if it is a directory."
1257 if [[ $1 == --help ]]; then
1258 echo "$help"
1259 return 0
1260 fi
1261 if [[ $# -ne 1 ]]; then
1262 echo "getdir error: expected 1 argument, got $#"
1263 return 1
1264 fi
1265 if [[ -d $1 ]]; then
1266 echo "$1"
1267 else
1268 local dir
1269 dir="$(dirname "$1")"
1270 if [[ -d $dir ]]; then
1271 echo "$dir"
1272 else
1273 echo "getdir error: directory does not exist"
1274 return 1
1275 fi
1276 fi
1277 }
1278
1279 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
1280 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
1281 local root
1282 root=$(gitroot) || return 1 # function to set gitroot
1283 builtin cd "$root"
1284 git symbolic-ref HEAD refs/heads/$1
1285 rm .git/index
1286 git clean -fdx
1287 }
1288
1289 # shellcheck disable=SC2120
1290 gitroot() {
1291 local help="Usage: gitroot [--help]
1292 Print the full path to the root of the current git repo
1293
1294 Handles being within a .git directory, unlike git rev-parse --show-toplevel,
1295 and works in older versions of git which did not have that."
1296 if [[ $1 == --help ]]; then
1297 echo "$help"
1298 return
1299 fi
1300 local p
1301 p=$(git rev-parse --git-dir) || { echo "error: not in a git repo" ; return 1; }
1302 [[ $p != /* ]] && p=$PWD
1303 echo "${p%%/.git}"
1304 }
1305
1306 g() {
1307
1308 # todo: patch emacs so it will look elsewhere. this is kinda sad:
1309 # https://emacs.stackexchange.com/questions/4253/how-to-start-emacs-with-a-custom-user-emacs-directory
1310
1311 local args gdb=false
1312
1313 if [[ $EMACSDIR ]]; then
1314 path-add "$EMACSDIR/lib-src" "$EMACSDIR/src"
1315 fi
1316
1317 if [[ $DISPLAY ]]; then
1318 args=-n
1319 fi
1320
1321 if (( $# == 0 )); then
1322 args+=" -c"
1323 fi
1324 # duplicate -c, but oh well
1325 if ! pgrep -u $EUID emacsclient; then
1326 if (( $# == 0 )) && type -p gdb &>/dev/null; then
1327 gdb=true
1328 else
1329 args+=" -c"
1330 fi
1331 fi
1332 if [[ $EMACSDIR ]]; then
1333 # Alter the path here, otherwise the nfs mount gets triggered on the
1334 # first path lookup when emacs is not being used.
1335 PATH="$EMACSDIR/lib-src:$EMACSDIR/src:$PATH" EHOME=$HOME HOME=$EMACSDIR m emacsclient -a "" $args "$@"
1336 else
1337 if $gdb; then
1338 # due to a bug, we cant debug from the start unless we get a new gdb
1339 # https://sourceware.org/bugzilla/show_bug.cgi?id=24454
1340 # m gdb -ex="set follow-fork-mode child" -ex=r -ex=quit --args emacs --daemon
1341 m emacsclient -a "" $args "$@"
1342 sleep 1
1343 cd /a/opt/emacs-$(distro-name)$(distro-num)
1344 s gdb -p $(pgrep -f 'emacs --daemon') -ex c
1345 cd -
1346 else
1347 m emacsclient -a "" $args "$@"
1348 fi
1349 fi
1350 }
1351
1352 # force terminal version
1353 gn() {
1354 g -n "$@"
1355 }
1356
1357 gmacs() {
1358 # quit will prompt if the program crashes.
1359 gdb -ex=r -ex=quit --args emacs "$@"; r;
1360 }
1361
1362 gdkill() {
1363 # kill the emacs daemon
1364 pk1 emacs --daemon
1365 }
1366
1367 gr() {
1368 grep -iIP --color=auto "$@" || return $?
1369 }
1370 grr() { # grep recursive
1371 # Don't return 1 on nonmatch because this is meant to be
1372 # interactive, not in a conditional.
1373 if [[ ${#@} == 1 ]]; then
1374 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -riIP --color=auto "$@" . || [[ $? == 1 ]]
1375 else
1376 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -riIP --color=auto "$@" || [[ $? == 1 ]]
1377 fi
1378 }
1379 ccomp grep gr grr
1380
1381 rg() { grr "$@"; }
1382 ccomp grep rg
1383
1384 hr() { # horizontal row. used to break up output
1385 printf "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
1386 echo
1387 }
1388 # highlight
1389 hl() {
1390 local col input_len=0
1391 for arg; do
1392 input_len=$((input_len + 1 + ${#arg}))
1393 done
1394 col=$((60 - input_len))
1395 printf "\e[1;97;41m%s" "$*"
1396 if (( col > 0 )); then
1397 printf "\e[1;97;41m \e[0m%.0s" $(eval echo "{1..${col}}")
1398 fi
1399 echo
1400 }
1401 hlm() { hl "$*"; "$@"; }
1402
1403 hrcat() { local f; for f; do [[ -f $f ]] || continue; hr; echo "$f"; cat "$f"; done }
1404
1405
1406 # get latest hub and run it
1407 # main command to use:
1408 # hub pull-request --no-edit
1409 # --no-edit means to use the first commit\'s message as the pull request message.
1410 # If that fails, try doing
1411 # hub pull-request --no-edit -b UPSTREAM_OWNER:branch
1412 # where branch is usually master. it does the pr against your current branch.
1413 #
1414 # On first use, you input username/pass and it gets an oath token so you dont have to repeat
1415 # it\'s at ~/.config/hub
1416 hub() {
1417 local up uptar updir p v
1418 # example https://github.com/github/hub/releases/download/v2.14.2/hub-linux-amd64-2.14.2.tgz
1419 up=$(wget -q -O- https://api.github.com/repos/github/hub/releases/latest | jq -r .assets[].browser_download_url | grep linux-amd64)
1420 re='[[:space:]]'
1421 if [[ ! $up || $up == $re ]]; then
1422 echo "failed to get good update url. got: $up"
1423 fi
1424 uptar=${up##*/}
1425 updir=${uptar%.tgz}
1426 if [[ ! -e /a/opt/$updir ]]; then
1427 rm -rf /a/opt/hub-linux-amd64*
1428 wget -P /a/opt $up
1429 tar -C /a/opt -zxf /a/opt/$uptar
1430 rm -f /a/opt/$uptar
1431 fi
1432 if ! which hub &>/dev/null; then
1433 sudo /a/opt/$updir/install
1434 fi
1435
1436 # save token across computers
1437 if [[ ! -L ~/.config/hub ]]; then
1438 if [[ -e ~/.config/hub ]]; then
1439 mv ~/.config/hub /p/c/subdir_files/.config/
1440 fi
1441 if [[ -e /p/c/subdir_files/.config/hub ]]; then
1442 conflink
1443 fi
1444 fi
1445 command hub "$@"
1446 }
1447
1448 i() { git "$@"; }
1449 ccomp git i
1450
1451 # git status:
1452 # cvs -qn update
1453
1454 # git checkout FILE
1455 # cvs update -C FILE
1456
1457 # git pull
1458 # cvs up[date]
1459
1460 # potentially useful command translation
1461 # https://fling.seas.upenn.edu/~giesen/dynamic/wordpress/equivalent-commands-for-git-svn-and-cvs/
1462
1463 # importing cvs repo into git using git-cvs package:
1464 # /f/www $ /usr/lib/git-core/git-cvsimport -C /f/www-git
1465
1466 ic() {
1467 # fast commit all
1468 git commit -am "$*"
1469 }
1470
1471 ipp() {
1472 git pull
1473 git push
1474 }
1475
1476
1477 ifn() {
1478 # insensitive find
1479 # -L = follow symlinks
1480 find -L . -not \( -name .svn -prune -o -name .git -prune \
1481 -o -name .hg -prune -o -name .editor-backups -prune \
1482 -o -name .undo-tree-history -prune \) -iname "*$**" 2>/dev/null
1483 }
1484
1485 ifd() {
1486 # insensitive find directory
1487 find -L . -type d -not \( -name .svn -prune -o -name .git -prune \
1488 -o -name .hg -prune -o -name .editor-backups -prune \
1489 -o -name .undo-tree-history -prune \) -iname "*$**" 2>/dev/null
1490 }
1491
1492
1493 ipdrop() {
1494 sudo iptables -A INPUT -s $1 -j DROP
1495 }
1496
1497
1498 istext() {
1499 grep -Il "" "$@" &>/dev/null
1500 }
1501
1502 pst() {
1503 pstree -apnA
1504 }
1505
1506 jtail() {
1507 journalctl -n 10000 -f "$@"
1508 }
1509 jr() { journalctl "$@" ; }
1510 jrf() { journalctl -f "$@" ; }
1511 jru() {
1512 journalctl -u exim4 _SYSTEMD_INVOCATION_ID=$(systemctl show -p InvocationID --value $1)
1513 }
1514
1515
1516
1517 l() {
1518 if [[ $PWD == /[iap] ]]; then
1519 command ls -A --color=auto -I lost+found "$@"
1520 else
1521 command ls -A --color=auto "$@"
1522 fi
1523 }
1524
1525 lcn() { locate -i "*$**"; }
1526
1527 lg() { LC_COLLATE=C.UTF-8 ll --group-directories-first "$@"; }
1528
1529 lt() { ll -tr "$@"; }
1530
1531 lld() { ll -d "$@"; }
1532
1533 ccomp ls l lg lt lld ll
1534
1535 # low recursively
1536 lowr() {
1537 local f dirs i a
1538 local -a all
1539 for dirs in false true; do
1540 for f; do
1541 if [[ -d $f ]]; then
1542 all=("$f"/**)
1543 # reverse the order to rename the nested dirs first.
1544 # note: 0 element is the dir itself
1545 for ((i=${#all[@]}-1; i>=1; i--)); do
1546 a="${all[i]}"
1547 if $dirs && [[ -d $a ]]; then
1548 # e dirs low "$a" # debug
1549 low "$a"
1550 elif ! $dirs && [[ ! -d $a && -e $a ]]; then
1551 # debug
1552 # e not dirs low "$a" # debug
1553 low "$a"
1554 fi
1555 done
1556 fi
1557 # just rename all the top level args on the second pass
1558 if $dirs; then
1559 # e final dirs low "$f" # debug
1560 low "$f"
1561 fi
1562 done
1563 done
1564 }
1565
1566 low() { # make filenames lowercase, remove bad chars
1567 local arg new dir f
1568 for arg; do
1569 arg="${arg%%+(/)}" # remove trailing slashes. assumes we have extglob on.
1570 dir="${arg%/*}"
1571 if (( ${#dir} == ${#arg} )); then
1572 dir=.
1573 fi
1574 f="${arg##*/}"
1575 new="${f,,}" # downcase
1576 new="${new//[^a-zA-Z0-9._-]/_}" # sub bad chars
1577 new="${new#"${new%%[[:alnum:]]*}"}" # remove leading/trailing non-alnum
1578 new="${new%"${new##*[[:alnum:]]}"}"
1579 # remove bad underscores, like __ and _._
1580 new=$(echo $new | sed -r 's/__+/_/g;s/_+([.-])|([.-])_+/\1/g')
1581 safe_rename "$dir/$f" "$dir/$new" || return 1
1582 done
1583 return 0
1584 }
1585
1586 lower() { # make first letter of filenames lowercase.
1587 local x
1588 for x in "$@"; do
1589 if [[ ${x::1} == [A-Z] ]]; then
1590 y=$(tr '[:upper:]' '[:lower:]' <<<"${x::1}")"${x:1}"
1591 safe_rename "$x" "$y" || return 1
1592 fi
1593 done
1594 }
1595
1596
1597 k() { # history search
1598 grep -iP --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | tail -n 80 || [[ $? == 1 ]];
1599 }
1600 ks() { # history search with context
1601 # args are an extended regex used by sed
1602 history | sed -nr "h;s/^\s*(\S+\s+){4}//;/$*/{g;p}" | tail -n 80 || [[ $? == 1 ]];
1603 }
1604 ksu() { # history search unique
1605 grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | uniq || [[ $? == 1 ]];
1606 }
1607
1608 # todo: id like to do maybe a daily or hourly cronjob to
1609 # check that my history file size is increasing. Ive had it
1610 # inexplicably truncated in the past.
1611 histrm() {
1612 history -n
1613 HISTTIMEFORMAT= history | awk -v IGNORECASE=1 '{ a=$1; sub(/^ *[^ ]+ */, "") }; /'"$*"'/'
1614 read -p "press anything but contrl-c to delete"
1615 for entry in $(HISTTIMEFORMAT= history | awk -v IGNORECASE=1 '{ a=$1; sub(/^ *[^ ]+ */, "") }; /'"$*"'/ { print a }' | tac); do
1616 history -d $entry
1617 done
1618 history -w
1619 }
1620
1621 ccomp grep k ks ksu histrm
1622
1623
1624 make-targets() {
1625 # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
1626 make -qp | awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
1627 }
1628
1629 mkc() {
1630 mkdir "$1"
1631 c "$1"
1632 }
1633 ccomp mkdir mkc
1634
1635 mkct() {
1636 mkc $(mktemp -d)
1637 }
1638
1639 mkt() { # mkdir and touch file
1640 local path="$1"
1641 mkdir -p "$(dirname "$path")"
1642 touch "$path"
1643 }
1644
1645 # shellcheck disable=SC2032
1646 mkdir() { command mkdir -p "$@"; }
1647
1648 nags() {
1649 # https://github.com/HenriWahl/Nagstamon/issues/357
1650 if ! pgrep -f /usr/lib/notification-daemon/notification-daemon >/dev/null; then
1651 /usr/lib/notification-daemon/notification-daemon &
1652 fi
1653 /usr/bin/nagstamon &
1654 }
1655
1656 nmt() {
1657 # cant use s because sudo -i doesnt work for passwordless sudo command
1658 case $EUID in
1659 0)
1660 sudo nmtui-connect "$@"
1661 ;;
1662 *)
1663 nmtui-connect "$@"
1664 ;;
1665 esac
1666 }
1667
1668
1669 ngset() {
1670 if shopt nullglob >/dev/null; then
1671 ngreset=false
1672 else
1673 shopt -s nullglob
1674 ngreset=true
1675 fi
1676 }
1677 ngreset() {
1678 if $ngreset; then
1679 shopt -u nullglob
1680 fi
1681 }
1682
1683 nopanic() {
1684 # shellcheck disable=SC2024
1685 ngset
1686 for f in /var/log/exim4/paniclog /var/log/exim4/*panic; do
1687 base=${f##*/}
1688 if [[ -s $f ]]; then
1689 echo ================== $f =============
1690 s tee -a /var/log/exim4/$base-archive <$f
1691 s truncate -s0 $f
1692 fi
1693 done
1694 ngreset
1695 }
1696
1697
1698 ping() { command ping -O "$@"; }
1699 p8() { ping "$@" 8.8.8.8; }
1700 p6() { ping6 "$@" 2001:4860:4860::8888; }
1701
1702 pkx() { # package extract
1703 local pkg cached tmp f
1704 c $(mktemp -d)
1705 pkg=$1
1706 # shellcheck disable=SC2012
1707 cached=$(ls -t /var/cache/apt/archives/$pkg* | tail -n1 2>/dev/null) ||:
1708 if [[ $cached ]]; then
1709 cp $cached .
1710 else
1711 aptitude download $pkg || return 1
1712 fi
1713 tmp=(*); f=${tmp[0]} # only 1 expected
1714 ex $f
1715 rm -f $f
1716 }
1717
1718 # pgrep and kill
1719 pk1() {
1720 local pid
1721 pid=($(pgrep -f "$*"))
1722 case ${#pid[@]} in
1723 1)
1724 # shellcheck disable=SC2128
1725 {
1726 ps -F $pid
1727 m kill $pid
1728 }
1729 ;;
1730 0) echo "no pid found" ;;
1731 *)
1732 ps -F ${pid[@]}
1733 ;;
1734 esac
1735 }
1736
1737 psg () {
1738 local x y help
1739 help="Usage: psg [--help] GREP_ARGS
1740 grep ps and output in a nice format"
1741 if [[ $1 == --help ]]; then
1742 echo "$help"
1743 return
1744 fi
1745 x=$(ps -eF)
1746 # final grep is because some commands tend to have a lot of trailing spaces
1747 y=$(echo "$x" | grep -iP "$@" | grep -o '.*[^ ]') ||:
1748 if [[ $y ]]; then
1749 echo "$x" | head -n 1 || [[ $? == 141 ]]
1750 echo "$y"
1751 fi
1752 }
1753
1754 pubip() { curl -4s https://icanhazip.com; }
1755 pubip6() { curl -6s https://icanhazip.com; }
1756 whatismyip() { pubip; }
1757
1758
1759 q() { # start / launch a program in the backround and redir output to null
1760 "$@" &> /dev/null &
1761 }
1762
1763 # shellcheck disable=SC2120
1764 r() {
1765 if [[ $HISTFILE ]]; then
1766 history -a # save history
1767 fi
1768 trap ERR # this avoids a segfault
1769 exit ${1:0}
1770 # i had this redir, not sure why
1771 # exit "$@" 2>/dev/null
1772 }
1773
1774 # scp is insecure and deprecated.
1775 scp() {
1776 rsync --inplace "$@"
1777 }
1778 ccomp rsync scp
1779
1780 randport() {
1781 # available high ports are 1024-65535,
1782 # but lets skip things that are more likely to be in use
1783 python3 <<'EOF'
1784 import secrets
1785 print(secrets.SystemRandom().randrange(10002,65500))
1786 EOF
1787 }
1788
1789 # reapply bashrc
1790 reb() {
1791 source ~/.bashrc
1792 }
1793
1794 rl() {
1795 readlink -f "$@"
1796 }
1797 ccomp readlink rl
1798
1799 rsd() {
1800 # rsync, root is required to keep permissions right.
1801 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
1802 # --no-times --delete
1803 # basically, make an exact copy, use checksums instead of file times to be more accurate
1804 rsync -ahvic --delete "$@"
1805 }
1806 rsa() {
1807 # like rlu, but dont delete files on the target end which
1808 # do not exist on the original end.
1809 rsync -ahvic "$@"
1810 }
1811 rst() {
1812 # rl without preserving modification time.
1813 rsync -ahvic --delete --no-t "$@"
1814 }
1815 # [RSYNC_OPTS] HOST PATH
1816 rsu() {
1817 # eg. rsu -opts frodo /testpath
1818 # relative paths will expanded with readlink -f.
1819 opts=("${@:1:$#-2}") # 1 to last -2
1820 path="${*:$#}" # last
1821 host="${*:$#-1:1}" # last -1
1822 if [[ $path == .* ]]; then
1823 path=$(readlink -f $path)
1824 fi
1825 m rsync -ahvi --relative --no-implied-dirs "${opts[@]}" "$path" "root@$host:/";
1826 }
1827 ccomp rsync rsd rsa rst rsu
1828
1829 # find programs listening on a port
1830 ssp() {
1831 local port=$1
1832 # to figure out these args, i had to look at the man page from git version, as of 2022-04.
1833 s ss -lpn state listening sport = $port
1834 }
1835
1836 resolvcat() {
1837 local f
1838 if [[ $(systemctl is-active nscd ||:) != inactive ]]; then
1839 m s nscd -i hosts
1840 fi
1841 f=/etc/resolv.conf
1842 echo $f:; ccat $f
1843 hr; s ss -lpn sport = 53
1844 if systemctl is-enabled dnsmasq &>/dev/null || [[ $(systemctl is-active dnsmasq ||:) != inactive ]]; then
1845 # this will fail is dnsmasq is failed
1846 hr; m ser status dnsmasq | cat || :
1847 f=/etc/dnsmasq.conf
1848 hr; echo $f:; ccat $f
1849 hr; m grr '^ *(servers-file|server) *=|^ *no-resolv *$' /etc/dnsmasq.conf /etc/dnsmasq.d
1850 f=/etc/dnsmasq-servers.conf
1851 hr; echo $f:; ccat $f
1852 fi
1853 hr
1854 echo /etc/nsswitch.conf:
1855 grep '^ *hosts:' /etc/nsswitch.conf
1856 if systemctl is-enabled systemd-resolved &>/dev/null || [[ $(systemctl is-active systemd-resolved ||:) != inactive ]]; then
1857 hr; m ser status systemd-resolved | cat || :
1858 hr; m resolvectl status | cat
1859 fi
1860
1861 }
1862 rcat() {
1863 resolvcat | less
1864 }
1865 reresolv() {
1866 if [[ $(systemctl is-active nscd ||:) != inactive ]]; then
1867 m ser stop nscd
1868 sleep .5
1869 m ser start nscd
1870 m sudo nscd -i hosts
1871 fi
1872 if [[ $(systemctl is-active dnsmasq ||:) != inactive ]]; then
1873 m sudo systemctl restart dnsmasq
1874 fi
1875 if [[ $(systemctl is-active systemd-resolved ||:) != inactive ]]; then
1876 m sudo systemctl restart systemd-resolved
1877 fi
1878 if type -P resolvectl &>/dev/null; then
1879 resolvectl flush-caches
1880 fi
1881 }
1882
1883 rmstrips() {
1884 ssh fencepost head -n 300 /gd/gnuorg/EventAndTravelInfo/rms-current-trips.txt | less
1885 }
1886
1887 urun () {
1888 umask $1
1889 shift
1890 "$@"
1891 }
1892 sudo () {
1893 command sudo "$@" || return $?
1894 DID_SUDO=true
1895 }
1896 s() {
1897 # background
1898 # I use a function because otherwise we cant use in a script,
1899 # cant assign to variable.
1900 #
1901 # note: gksudo is recommended for X apps because it does not set the
1902 # home directory to the same, and thus apps writing to ~ fuck things up
1903 # with root owned files.
1904 #
1905 if [[ $EUID != 0 || $1 == -* ]]; then
1906 # shellcheck disable=SC2034
1907 SUDOD="$PWD" command sudo -i "$@"
1908 DID_SUDO=true
1909 else
1910 "$@"
1911 fi
1912 }
1913 sb() { # sudo bash -c
1914 # use sb instead of s is for sudo redirections,
1915 # eg. sb 'echo "ok fine" > /etc/file'
1916 # shellcheck disable=SC2034
1917 local SUDOD="$PWD"
1918 sudo -i bash -c "$@"
1919 }
1920 # secret sudo
1921 se() { s urun 0077 "$@"; }
1922 ccomp sudo s sb se
1923
1924 safe_rename() { # warn and dont rename if file exists.
1925 # mv -n exists, but it\'s silent
1926 if [[ $# != 2 ]]; then
1927 echo safe_rename error: $# args, need 2 >2
1928 return 1
1929 fi
1930 if [[ $1 != "$2" ]]; then # yes, we want to silently ignore this
1931 if [[ -e $2 || -L $2 ]]; then
1932 echo "Cannot rename $1 to $2 as it already exists."
1933 else
1934 mv -vi "$1" "$2"
1935 fi
1936 fi
1937 }
1938
1939
1940 sd() {
1941 sudo dd status=none of="$1"
1942 }
1943
1944 ser() {
1945 if type -p systemctl &>/dev/null; then
1946 s systemctl "$@"
1947 else
1948 if (( $# >= 3 )); then
1949 echo iank: ser expected 2 or less arguments
1950 return 1
1951 fi
1952 s service $2 $1
1953 fi
1954 }
1955 serstat() {
1956 systemctl -n 40 status "$@"
1957 }
1958
1959 seru() { systemctl --user "$@"; }
1960 # like restart, but do nothing if its not already started
1961 srestart() {
1962 local service=$1
1963 if [[ $(s systemctl --no-pager show -p ActiveState $service ) == ActiveState=active ]]; then
1964 systemctl restart $service
1965 fi
1966 }
1967
1968 setini() { # set a value in a .ini style file
1969 key="$1" value="$2" section="$3" file="$4"
1970 if [[ -s $file ]]; then
1971 sed -ri -f - "$file" <<EOF
1972 # remove existing keys
1973 / *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d}
1974 # add key
1975 /^\s*\[$section\]/a $key=$value
1976 # from section to eof, do nothing
1977 /^\s*\[$section\]/,\$b
1978 # on the last line, if we haven't found section yet, add section and key
1979 \$a [$section]\\
1980 $key=$value
1981 EOF
1982 else
1983 cat >"$file" <<EOF
1984 [$section]
1985 $key=$value
1986 EOF
1987 fi
1988 }
1989
1990 sgo() { # service go
1991 service=$1
1992 ser restart $service || return 1
1993 if type -p systemctl &>/dev/null; then
1994 ser enable $service
1995 fi
1996 }
1997 soff () {
1998 for service; do
1999 # ignore services that dont exist
2000 if systemctl cat $service &>/dev/null; then
2001 ser stop $service;
2002 ser disable $service
2003 fi
2004 done
2005 }
2006
2007 sgu() {
2008 systemctl list-unit-files | rg "$@"
2009 }
2010
2011
2012 sk() {
2013
2014
2015 # disable a warning with:
2016 # shellcheck disable=SC2206 # reasoning
2017
2018 # see bash-template/style-guide.md for justifications
2019
2020 local quotes others
2021 quotes=2048,2068,2086,2206
2022 others=2029,2033,2054,2164
2023 shellcheck -W 999 -x -e $quotes,$others "$@" || return $?
2024 }
2025
2026
2027 # sl: ssh, but firsh rsync our bashrc and related files to a special
2028 # directory on the remote host if needed.
2029
2030 # Some environment variables and files need to be setup for this to work
2031 # (mine are set at the beginning of this file)
2032
2033 # SL_FILES_DIR: Environment variable. Path to folder which should at
2034 # least have a .bashrc file or symlink. This dir will be rsynced to ~ on
2035 # remote hosts (top level symlinks are resolved) unless the host already
2036 # has a $SL_FILES_DIR/.bashrc. In that case, we assume it is a host you
2037 # control and sync files to separately and already has the ~/.bashrc you
2038 # want. The remote bash will also take its .inputrc config from this
2039 # folder (default of not existing is fine). Mine looks like this:
2040 # https://iankelling.org/git/?p=distro-setup;a=tree;f=sl/.iank
2041
2042 # SL_INFO_DIR: Environment variable. This folder stores info about what
2043 # we detected on the remote system and when we last synced. It will be created
2044 # if it does not exist. Sometimes you may want to forget about a
2045 # remote system, you can use sl --rsync, or the function for that slr
2046 # below.
2047
2048 # SL_TEST_CMD: Env var. Meant to be used to vary the files synced
2049 # depending on the remote host. Run this string on the remote host the
2050 # first time sl is run (or if we run slr). The result is passed to
2051 # SL_TEST_HOOK. For example,
2052 # export SL_TEST_CMD=". /etc/os-release ; echo \${VERSION//[^a-zA-Z0-9]/}"
2053
2054 # SL_TEST_HOOK: Env var. It is run as $SL_TEST_HOOK. This can set
2055 # $SL_FILES_DIR to vary the files synced.
2056
2057 # SL_RSYNC_ARGS: Env var. String of arguments passed to rsync. For
2058 # example to exclude files within a directory. Note, excluded
2059 # files wont be deleted on rsync, you can add --delete-excluded
2060 # to the rsync command if that is desired.
2061
2062 # SL_SSH_ARGS: Env var. Default arguments passed to ssh.
2063
2064 # For when ~/.bashrc is already customized on the remote server, you
2065 # might find it problematic that ~/.bashrc is sourced for ALL ssh
2066 # commands, even in scripts. This paragraph is all about that. bash
2067 # scripts dont source ~/.bashrc, but call ssh in scripts and you get
2068 # ~/.bashrc. You dont want this. .bashrc is meant for interactive shells
2069 # and if you customize it, probably has bugs from time to time. This is
2070 # bad. Here's how I fix it. I have a special condition to "return" in my
2071 # .bashrc for noninteractive ssh shells (copy that code). Then use this
2072 # function or similar that passes LC_USEBASHRC=t when sshing and I want
2073 # my bashrc. Also, I don't keep most of my bashrc in .bashrc, i source a
2074 # separate file because even if I return early on, the whole file gets
2075 # parsed which can fail if there is a syntax error.
2076 sl() {
2077 # Background on LC_USEBASHRC var (no need to read if you just want to
2078 # use this function): env variables sent across ssh are strictly
2079 # limited, but we get LC_* at least in debian based machines, so we
2080 # just make that * be something no normal program would use. Note, on
2081 # hosts that dont allow LC_* I start an inner shell with LC_USEBASHRC
2082 # set, and the inner shell also allows running a nondefault
2083 # .bashrc. This means the outer shell still ran the default .bashrc,
2084 # but that is the best we can do.
2085
2086 local now args remote dorsync haveinfo tmpa sshinfo tmp tmp2 type info_sec force_rsync \
2087 sync_dirname testcmd extra_info testbool files_sec sl_test_cmd sl_test_hook
2088 declare -a args tmpa
2089
2090 args=($SL_SSH_ARGS)
2091
2092 # ssh [-1246Antivivisectionist] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
2093 # [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address]
2094 # [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option]
2095 # [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname
2096 # [command]
2097
2098 # ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
2099 # [-D [bind_address:]port] [-E log_file] [-e escape_char]
2100 # [-F configfile] [-I pkcs11] [-i identity_file]
2101 # [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
2102 # [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
2103 # [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
2104
2105 force_rsync=false
2106 if [[ $1 == --rsync ]]; then
2107 force_rsync=true
2108 shift
2109 fi
2110
2111 sl_test_cmd=$SL_TEST_CMD
2112 sl_test_hook=$SL_TEST_HOOK
2113 sl_rsync_args=$SL_RSYNC_ARGS
2114 while [[ $1 ]]; do
2115 case "$1" in
2116 --rsync)
2117 force_rsync=true
2118 ;;
2119 --sl-test-cmd)
2120 sl_test_cmd="$2"
2121 shift
2122 ;;
2123 --sl-test-hook)
2124 sl_test_hook="$2"
2125 shift
2126 ;;
2127 --sl-rsync-args)
2128 sl_rsync_args="$2"
2129 shift
2130 ;;
2131 *)
2132 break
2133 ;;
2134 esac
2135 shift
2136 done
2137
2138 while [[ $1 ]]; do
2139 case "$1" in
2140 # note we dont support things like -4oOption
2141 -[46AaCfGgKkMNnqsTtVvXxYy]*)
2142 args+=("$1"); shift
2143 ;;
2144 -[bcDEeFIiJLlmOopQRSWw]*)
2145 # -oOption etc is valid
2146 if (( ${#1} >= 3 )); then
2147 args+=("$1"); shift
2148 else
2149 args+=("$1" "$2"); shift 2
2150 fi
2151 ;;
2152 *)
2153 break
2154 ;;
2155 esac
2156 done
2157 remote="$1"
2158 if [[ ! $remote ]]; then
2159 echo $0: error hostname required >&2
2160 return 1
2161 fi
2162 shift
2163
2164 if [[ ! $SL_INFO_DIR ]]; then
2165 echo error: missing '$SL_INFO_DIR' env var >&2
2166 return 1
2167 fi
2168
2169 dorsync=false
2170 haveinfo=false
2171 tmpa=($SL_INFO_DIR/???????????"$remote")
2172 sshinfo=${tmpa[0]}
2173 if [[ -e $sshinfo ]]; then
2174 if $force_rsync; then
2175 rm -f $sshinfo
2176 else
2177 haveinfo=true
2178 fi
2179 fi
2180 if $haveinfo; then
2181 tmp=${sshinfo[0]##*/}
2182 tmp2=${tmp::11}
2183 type=${tmp2: -1}
2184 extra_info=$(cat $sshinfo)
2185 else
2186 # we test for string to know ssh succeeded
2187 testbool="test -e $SL_FILES_DIR/.bashrc -a -L .bashrc -a -v LC_USEBASHRC"
2188 testcmd="if $testbool; then printf y; else printf n; fi"
2189 if ! tmp=$(LC_USEBASHRC=y command ssh "${args[@]}" "$remote" "$testcmd; $sl_test_cmd"); then
2190 echo failed sl test. doing plain ssh -v
2191 command ssh -v "${args[@]}" "$remote"
2192 fi
2193 if [[ $tmp == y* ]]; then
2194 type=a
2195 else
2196 dorsync=true
2197 type=b
2198 fi
2199 extra_info="${tmp:1}"
2200 fi
2201 if [[ $sl_test_hook ]]; then
2202 RSYNC_RSH="ssh ${args[*]}" $sl_test_hook "$extra_info" "$remote"
2203 fi
2204
2205 if $haveinfo && [[ $type == b ]]; then
2206 info_sec=${tmp::10}
2207 read files_sec _ < <(find -L $SL_FILES_DIR -printf "%T@ %p\n" | sort -nr || [[ $? == 141 || ${PIPESTATUS[0]} == 32 ]] )
2208 files_sec=${files_sec%%.*}
2209 if (( files_sec > info_sec )); then
2210 dorsync=true
2211 rm -f $sshinfo
2212 fi
2213 fi
2214
2215 sync_dirname=${SL_FILES_DIR##*/}
2216
2217 if [[ ! $SL_FILES_DIR ]]; then
2218 echo error: missing '$SL_FILES_DIR' env var >&2
2219 return 1
2220 fi
2221
2222 if $dorsync; then
2223 RSYNC_RSH="ssh ${args[*]}" m rsync -rptL --delete $sl_rsync_args $SL_FILES_DIR "$remote":
2224 fi
2225 if $dorsync || ! $haveinfo; then
2226 sshinfo=$SL_INFO_DIR/$EPOCHSECONDS$type"$remote"
2227 [[ -e $SL_INFO_DIR ]] || mkdir -p $SL_INFO_DIR
2228 printf "%s\n" "$extra_info" >$sshinfo
2229 chmod 666 $sshinfo
2230 fi
2231 if [[ $type == b ]]; then
2232 if (( ${#@} )); then
2233 # Theres a couple ways to pass arguments, im not sure whats best,
2234 # but relying on bash 4.4+ escape quoting seems most reliable.
2235 command ssh "${args[@]}" "$remote" \
2236 LC_USEBASHRC=t bash -c '.\ '$sync_dirname'/.bashrc\;"\"\$@\""' bash ${@@Q}
2237 elif [[ ! -t 0 ]]; then
2238 # This case is when commands are being piped to ssh.
2239 # Normally, no bashrc gets sourced.
2240 # But, since we are doing all this, lets source it because we can.
2241 cat <(echo . $sync_dirname/.bashrc) - | command ssh "${args[@]}" "$remote" LC_USEBASHRC=t bash
2242 else
2243 command ssh -t "${args[@]}" "$remote" LC_USEBASHRC=t INPUTRC=$sync_dirname/.inputrc bash --rcfile $sync_dirname/.bashrc
2244 fi
2245 else
2246 if [[ -t 0 ]]; then
2247 LC_USEBASHRC=t command ssh "${args[@]}" "$remote" ${@@Q}
2248 else
2249 command ssh "${args[@]}" "$remote" LC_USEBASHRC=t bash
2250 fi
2251 fi
2252 # this function inspired from https://github.com/Russell91/sshrc
2253 }
2254
2255 slr() {
2256 sl --rsync "$@"
2257 }
2258 sss() { # ssh solo
2259 sl -oControlMaster=no -oControlPath=/ "$@"
2260 }
2261 # kill off old shared socket then ssh
2262 ssk() {
2263 m ssh -O exit "$@" || [[ $? == 255 ]]
2264 m sl "$@"
2265 }
2266 ccomp ssh sl slr sss ssk
2267 # plain ssh
2268 ssh() {
2269 if [[ $TERM == alacritty || $TERM == xterm-kitty ]]; then
2270 TERM=xterm-256color LC_USEBASHRC=t command ssh "$@"
2271 else
2272 LC_USEBASHRC=t command ssh "$@"
2273 fi
2274 }
2275
2276
2277 slog() {
2278 # log with script. timing is $1.t and script is $1.s
2279 # -l to save to ~/typescripts/
2280 # -t to add a timestamp to the filenames
2281 local logdir do_stamp arg_base
2282 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
2283 logdir="/a/dt/"
2284 do_stamp=false
2285 while getopts "lt" option
2286 do
2287 case $option in
2288 l ) arg_base=$logdir ;;
2289 t ) do_stamp=true ;;
2290 esac
2291 done
2292 shift $((OPTIND - 1))
2293 arg_base+=$1
2294 [[ -e $logdir ]] || mkdir -p $logdir
2295 $do_stamp && arg_base+=$(date +%F.%T%z)
2296 script -t $arg_base.s 2> $arg_base.t
2297 }
2298 splay() { # script replay
2299 #logRoot="$HOME/typescripts/"
2300 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
2301 scriptreplay "$1.t" "$1.s"
2302 }
2303
2304 sr() {
2305 # sudo redo. be aware, this command may not work right on strange distros or earlier software
2306 if [[ $# == 0 ]]; then
2307 sudo -E bash -c -l "$(history -p '!!')"
2308 else
2309 echo this command redos last history item. no argument is accepted
2310 fi
2311 }
2312
2313 srm () {
2314 # with -ll, less secure but faster.
2315 command srm -ll "$@"
2316 }
2317
2318 srun() {
2319 scp $2 $1:/tmp
2320 ssh $1 /tmp/${2##*/} $(printf "%q\n" "${@:2}")
2321 }
2322
2323
2324 swap() {
2325 local tmp
2326 tmp=$(mktemp)
2327 mv $1 $tmp
2328 mv $2 $1
2329 mv $tmp $2
2330 }
2331
2332 tclock() { # terminal clock
2333 local x
2334 clear
2335 date +%l:%_M
2336 len=60
2337 # this goes to full width
2338 #len=${1:-$((COLUMNS -7))}
2339 x=1
2340 while true; do
2341 if (( x == len )); then
2342 end=true
2343 d="$(date +%l:%_M) "
2344 else
2345 end=false
2346 d=$(date +%l:%M:%_S)
2347 fi
2348 echo -en "\r"
2349 echo -n "$d"
2350 for ((i=0; i<x; i++)); do
2351 if (( i % 6 )); then
2352 echo -n _
2353 else
2354 echo -n .
2355 fi
2356 done
2357 if $end; then
2358 echo
2359 x=1
2360 else
2361 x=$((x+1))
2362 fi
2363 sleep 5
2364 done
2365 }
2366
2367
2368 te() {
2369 # test existence / exists
2370 local ret=0
2371 for x in "$@"; do
2372 [[ -e "$x" || -L "$x" ]] || ret=1
2373 done
2374 return $ret
2375 }
2376
2377 psoff() {
2378 # normally, i would just execute these commands in the function.
2379 # however, DEBUG is not inherited, so we need to run it outside a function.
2380 # And we want to run set -x afterwards to avoid spam, so we cram everything
2381 # in here, and then it will run after this function is done.
2382 PROMPT_COMMAND='trap DEBUG; unset PROMPT_COMMAND; PS1="\w \$ "'
2383 }
2384 pson() {
2385 PROMPT_COMMAND=prompt-command
2386 if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
2387 trap 'settitle "$BASH_COMMAND"' DEBUG
2388 fi
2389 }
2390
2391 tx() { # toggle set -x, and the prompt so it doesnt spam
2392 if [[ $- == *x* ]]; then
2393 set +x
2394 pson
2395 else
2396 psoff
2397 fi
2398 }
2399
2400 psnetns() {
2401 # show all processes in the network namespace $1.
2402 # blank entries appear to be subprocesses/threads
2403 local x netns
2404 netns=$1
2405 ps -w | head -n 1
2406 sudo find -L /proc/[1-9]*/task/*/ns/net -samefile /run/netns/$netns | cut -d/ -f5 | \
2407 while read -r l; do
2408 x=$(ps -w --no-headers -p $l);
2409 if [[ $x ]]; then echo "$x"; else echo $l; fi;
2410 done
2411 }
2412 nonet() {
2413 if ! s ip netns list | grep -Fx nonet &>/dev/null; then
2414 s ip netns add nonet
2415 fi
2416 sudo -E env /sbin/ip netns exec nonet sudo -E -u iank /bin/bash
2417 }
2418
2419 m() { printf "%s\n" "$*"; "$@"; }
2420
2421 # update file. note: duplicated in mail-setup
2422 u() {
2423 local tmp tmpdir dest="$1"
2424 local base="${dest##*/}"
2425 local dir="${dest%/*}"
2426 if [[ $dir != "$base" ]]; then
2427 # dest has a directory component
2428 mkdir -p "$dir"
2429 fi
2430 ur=false # u result
2431 tmpdir=$(mktemp -d)
2432 cat >$tmpdir/"$base"
2433 tmp=$(rsync -ic $tmpdir/"$base" "$dest")
2434 if [[ $tmp ]]; then
2435 printf "%s\n" "$tmp"
2436 ur=true
2437 if [[ $dest == /etc/systemd/system/* ]]; then
2438 reload=true
2439 fi
2440 fi
2441 rm -rf $tmpdir
2442 }
2443
2444
2445 uptime() {
2446 if type -p uprecords &>/dev/null; then
2447 uprecords -B
2448 else
2449 command uptime
2450 fi
2451 }
2452
2453 virshrm() {
2454 for x in "$@"; do virsh destroy "$x"; virsh undefine "$x"; done
2455 }
2456
2457 vm-set-listen(){
2458 local t
2459 t=$(mktemp)
2460 local vm=$1
2461 local ip=$2
2462 sudo virsh dumpxml $vm | sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
2463 sed -r "s/listen='[^']+/listen='$ip/"> $t
2464 sudo virsh undefine $vm
2465 sudo virsh define $t
2466 }
2467
2468
2469 vmshare() {
2470 vm-set-listen $1 0.0.0.0
2471 }
2472
2473
2474 vmunshare() {
2475 vm-set-listen $1 127.0.0.1
2476 }
2477
2478 myiwscan() {
2479 # find input, copy to pattern space, when we find the first field, print the copy in different order without newlines.
2480 # instead of using labels, we could just match a line and group, eg: /signal:/,{s/signal:(.*)/\1/h}
2481 sudo iw dev wls1 scan | sed -rn "
2482 s/^\Wcapability: (.*)/\1/;Ta;h;b
2483 :a;s/^\Wsignal: -([^.]+).*/\1/;Tb;H;b
2484 # padded to min width of 20
2485 :b;s/\WSSID: (.*)/\1 /;T;s/^(.{20}(.*[^ ])?) */\1/;H;g;s/(.*)\n(.*)\n(.*)/\2 \3 \1/gp;b
2486 "|sort -r
2487 }
2488
2489 # Run script by copying it to a temporary location first,
2490 # and changing directory, so we don't have any open
2491 # directories or files that could cause problems when
2492 # remounting.
2493 zr() {
2494 local tmp
2495 tmp=$(type -p "$1")
2496 if [[ $tmp ]]; then
2497 cd $(mktemp -d)
2498 cp -a "$tmp" .
2499 shift
2500 ./"${tmp##*/}" "$@"
2501 else
2502 "$@"
2503 fi
2504 }
2505
2506
2507 # * spark
2508 # spark 1 5 22 13 53
2509 # # => ▁▁▃▂▇
2510
2511 # The MIT License
2512 # Copyright (c) Zach Holman, https://zachholman.com
2513 # https://github.com/holman/spark
2514
2515 # As of 2022-10-28, I reviewed github forks that had several newer
2516 # commits, none had anything interesting. I did a little refactoring
2517 # mostly to fix emacs indent bug.
2518
2519 # Generates sparklines.
2520 _spark_echo()
2521 {
2522 if [ "X$1" = "X-n" ]; then
2523 shift
2524 printf "%s" "$*"
2525 else
2526 printf "%s\n" "$*"
2527 fi
2528 }
2529
2530
2531 spark()
2532 {
2533 local f tc
2534 local n numbers=
2535
2536 # find min/max values
2537 local min=0xffffffff max=0
2538
2539 for n in ${@//,/ }
2540 do
2541 # on Linux (or with bash4) we could use `printf %.0f $n` here to
2542 # round the number but that doesn't work on OS X (bash3) nor does
2543 # `awk '{printf "%.0f",$1}' <<< $n` work, so just cut it off
2544 n=${n%.*}
2545 (( n < min )) && min=$n
2546 (( n > max )) && max=$n
2547 numbers=$numbers${numbers:+ }$n
2548 done
2549
2550 # print ticks
2551 local ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
2552
2553 # use a high tick if data is constant
2554 (( min == max )) && ticks=(▅ ▆)
2555
2556 tc=${#ticks[@]}
2557 f=$(( ( (max-min) <<8)/( tc - 1) ))
2558 (( f < 1 )) && f=1
2559
2560 for n in $numbers
2561 do
2562 _spark_echo -n ${ticks[$(( ((($n-$min)<<8)/$f) ))]}
2563 done
2564 _spark_echo
2565 }
2566
2567
2568 # * misc stuff
2569
2570
2571 if $use_color && type -p tput &>/dev/null; then
2572 term_bold="$(tput bold)"
2573 term_red="$(tput setaf 1)"
2574 term_green="$(tput setaf 2)"
2575 term_yellow="$(tput setaf 3)"
2576 term_purple="$(tput setaf 5)"
2577 term_nocolor="$(tput sgr0)" # no font attributes
2578
2579 # unused so far. commented for shellcheck
2580 # term_underl="$(tput smul)"
2581 # term_blue="$(tput setaf 4)"
2582 # term_cyan="$(tput setaf 6)"
2583 fi
2584 # Try to keep environment pollution down, EPA loves us.
2585 unset safe_term match_lhs use_color
2586
2587 # * prompt
2588
2589
2590 if [[ $- == *i* ]]; then
2591
2592
2593 case $HOSTNAME in
2594 bk|je|li)
2595 if [[ $EUID == 1000 ]]; then
2596 system-status _ ||:
2597 fi
2598 ;;
2599 esac
2600
2601
2602 # this needs to come before next ps1 stuff
2603 # this stuff needs bash 4, feb 2009,
2604 # old enough to no longer condition on $BASH_VERSION anymore
2605 shopt -s autocd
2606 shopt -s dirspell
2607 PS1='\w'
2608 if [[ $- == *i* ]] && [[ ! $LC_INSIDE_EMACS ]]; then
2609 PROMPT_DIRTRIM=2
2610 bind -m vi-command B:shell-backward-word
2611 bind -m vi-command W:shell-forward-word
2612 fi
2613
2614 if [[ $SSH_CLIENT || $SUDO_USER ]]; then
2615 unset PROMPT_DIRTRIM
2616 PS1="\h:$PS1"
2617 fi
2618
2619 # emacs terminal has problems if this runs slowly,
2620 # so I've thrown a bunch of things at the wall to speed it up.
2621 prompt-command() {
2622 local return=$? # this MUST COME FIRST
2623 local ps_char ps_color
2624 unset IFS
2625
2626 if [[ $HISTFILE ]]; then
2627 history -a # save history
2628 fi
2629
2630 case $return in
2631 0) ps_color="$term_purple"
2632 ps_char='\$'
2633 ;;
2634 *) ps_color="$term_green"
2635 ps_char="$return \\$"
2636 ;;
2637 esac
2638 if [[ ! -O . ]]; then # not owner
2639 if [[ -w . ]]; then # writable
2640 ps_color="$term_bold$term_red"
2641 else
2642 ps_color="$term_bold$term_green"
2643 fi
2644 fi
2645
2646 # faster than sourceing the file im guessing
2647 if [[ -e /dev/shm/iank-status && ! -e /tmp/quiet-status ]]; then
2648 eval $(< /dev/shm/iank-status)
2649 fi
2650 if [[ $MAIL_HOST && $MAIL_HOST != "$HOSTNAME" ]]; then
2651 ps_char="@ $ps_char"
2652 fi
2653 jobs_char=
2654 if [[ $(jobs -p) ]]; then
2655 jobs_char='j\j '
2656 fi
2657 # We could test if sudo is active with sudo -nv
2658 # but then we get an email and log of lots of failed sudo commands.
2659 # We could turn those off, but seems better not to.
2660 if [[ $EUID != 0 ]] && [[ $DID_SUDO ]]; then
2661 psudo="\[$term_bold$term_red\]s\[$term_nocolor\] "
2662 fi
2663 if [[ ! $HISTFILE ]]; then
2664 ps_char="NOHIST $ps_char"
2665 fi
2666 PS1="${PS1%"${PS1#*[wW]}"} $jobs_char$psudo\[$ps_color\]$ps_char\[$term_nocolor\] "
2667
2668 # set titlebar. instead, using more advanced
2669 # titelbar below
2670 #echo -ne "$_title_escape $HOSTNAME ${PWD/#$HOME/~} \007"
2671 }
2672 PROMPT_COMMAND=prompt-command
2673
2674 if [[ $TERM == screen* ]]; then
2675 _title_escape="\033]..2;"
2676 else
2677 # somme sites recommend this, i dunno what the diff is.
2678 #_title_escape="\033]30;"
2679 _title_escape="\033]0;"
2680 fi
2681
2682 # make the titlebar be the last command and the current directory.
2683 settitle () {
2684
2685
2686 # These are some checks to help ensure we dont set the title at
2687 # times that the debug trap is running other than the case we
2688 # want. Some of them might not be needed.
2689 if (( ${#FUNCNAME[@]} != 1 || ${#BASH_ARGC[@]} != 2 || $BASH_SUBSHELL != 0 )); then
2690 return 0
2691 fi
2692 if [[ $1 == prompt-command ]]; then
2693 return 0
2694 fi
2695 echo -ne "$_title_escape ${PWD/#$HOME/~} "
2696 printf "%s" "$*"
2697 echo -ne "\007"
2698 }
2699
2700 # note, this wont work:
2701 # x=$(mktemp); cp a $x
2702 # I havnt figured out why, bigger fish to fry.
2703 #
2704 # for titlebar.
2705 # condition from the screen man page i think.
2706 # note: duplicated in tx()
2707 if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
2708 trap 'settitle "$BASH_COMMAND"' DEBUG
2709 else
2710 trap DEBUG
2711 fi
2712
2713 fi
2714
2715 # * stuff that makes sense to be at the end
2716
2717
2718 # best practice
2719 unset IFS
2720
2721 # shellcheck disable=SC1090
2722 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
2723
2724 # I had this idea to start a bash shell which would run an initial
2725 # command passed through this env variable, then continue on
2726 # interactively. But the use case I had in mind went away.
2727 #
2728 # if [[ $MY_INIT_CMD ]]; then
2729 # "${MY_INIT_CMD[@]}"
2730 # unset MY_INIT_CMD
2731 # fi
2732
2733 # ensure no bad programs appending to this file will have an affect
2734 return 0