various fixes and updates
[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 ~/.iank/err ]]; then
11 # shellcheck source=/a/bin/errhandle/err
12 source ~/.iank/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 # for testing error catching:
22 # t2() {
23 # echo t2
24 # grep sdf sdfd
25 # echo wtf
26 # }
27 # t1() {
28 # echo t1
29 # t2 a b c
30 # }
31
32 # * settings
33
34 CDPATH=.
35
36 # remove all aliases. aliases provided by the system tend to get in the way,
37 # for example, error happens if I try to define a function the same name as an alias
38 unalias -a
39
40 # remove gnome keyring warning messages
41 # there is probably a more proper way, but I didnt find any easily on google
42 # now using xfce+xmonad instead of vanilla xmonad, so disabling this
43 #unset GNOME_KEYRING_CONTROL
44
45 # use extra globing features.
46 shopt -s extglob
47 # include .files when globbing, but ignore files name . and ..
48 # setting this also sets dotglob.
49 export GLOBIGNORE="*/.:*/.."
50
51 # broken with bash_completion package. Saw a bug for this once. dont anymore.
52 # still broken in wheezy
53 # still buggered in latest stable from the web, version 2.1
54 # perhaps its fixed in newer git version, which fails to make for me
55 # this note is from 6-2014.
56 # still broken in flidas.
57 #shopt -s nullglob
58
59 # make tab on an empty line do nothing
60 shopt -s no_empty_cmd_completion
61
62 # fix spelling errors for cd, only in interactive shell
63 shopt -s cdspell
64 # append history instead of overwritting it
65 shopt -s histappend
66 # for compatibility, per gentoo/debian bashrc
67 shopt -s checkwinsize
68 # attempt to save multiline single commands as single history entries.
69 shopt -s cmdhist
70 # enable **
71 shopt -s globstar
72
73
74 # inside emacs fixes
75 if [[ $RLC_INSIDE_EMACS ]]; then
76 # EMACS is used by bash on startup, but we dont need it anymore.
77 # plus I hit a bug in a makefile which inherited it
78 unset EMACS
79 export RLC_INSIDE_EMACS
80 export PAGER=cat
81 export MANPAGER=cat
82 # scp completion does not work, but this doesnt fix it. todo, figure this out
83 #complete -r scp &> /dev/null
84 # todo, remote file completion fails, figure out how to turn it off
85 export NODE_DISABLE_COLORS=1
86 # This gets rid of ugly terminal escape chars in node repl
87 # sometime, Id like to have completion working in emacs shell for node
88 # the offending chars can be found in lib/readline.js,
89 # things that do like:
90 # stream.write('\x1b[' + (x + 1) + 'G');
91 # We can remove them and keep readline, for example by doing this
92 # to start a repl:
93 #!/usr/bin/env nodejs
94 # var readline = require('readline');
95 # readline.cursorTo = function(a,b,c) {};
96 # readline.clearScreenDown = function(a) {};
97 # const repl = require('repl');
98 # var replServer = repl.start('');
99 #
100 # no prompt, or else readline complete seems to be confused, based
101 # on our column being different? node probably needs to send
102 # different kind of escape sequence that is not ugly. Anyways,
103 # completion doesnt work yet even with the ugly prompt, so whatever
104 #
105 export NODE_NO_READLINE=1
106
107 fi
108
109 # emacs has a different default search path than the info command. This
110 # adds the info defaults to emacs, but not the reverse, because I dun
111 # care much about the cli. The search path is only on the cli if you run
112 # "info xxx", or in emacs if you run '(info xxx)', so not that
113 # important, but might as well fix it.
114
115 # info info says this path is what was compiled, and its not documented
116 # anywhere. Through source grepping, i found it in filesys.h of the info
117 # source in trisquel flidas.
118 #
119 # Traling : means for emacs to add its own stuff on to the end.
120
121 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:.:
122
123 if [[ $- == *i* ]]; then
124 # for readline-complete.el
125 if [[ $RLC_INSIDE_EMACS ]]; then
126 # all for readline-complete.el
127 stty echo
128 bind 'set horizontal-scroll-mode on'
129 bind 'set print-completions-horizontally on'
130 bind '"\C-i": self-insert'
131 else
132
133 if [[ $KONSOLE_PROFILE_NAME ]]; then
134 TERM=xterm-256color
135 fi
136
137 # todo: not sure this works in sakura
138 #stty werase undef
139 #bind "\C-w": kill-region
140 # sakura == xterm-256color
141 # konsole == xterm
142 if [[ $TERM == xterm* ]]; then
143 # control + arrow keys. for other terminals, see http://unix.stackexchange.com/questions/10806/how-to-change-previous-next-word-shortcut-in-bash
144 bind '"\e[1;5C": shell-forward-word' 2>/dev/null
145 bind '"\e[1;5D": shell-backward-word' 2>/dev/null
146 else
147 # make ctrl-backspace work. for konsole, i fixed it through
148 # /home/iank/.local/share/konsole/default.keytab
149 stty werase '^h'
150 bind '"\eOc": shell-forward-word'
151 bind '"\eOd": shell-backward-word'
152 fi
153 # i cant remember why i did this, probably to free up some keys to bind
154 # to other things in bash.
155 # other than C-c and C-z, the rest defined by stty -a are, at least in
156 # gnome-terminal, overridden by bash, or disabled by the system
157 stty lnext undef stop undef start undef
158 fi
159
160 fi
161
162 export BC_LINE_LENGTH=0
163
164 # ansible option
165 export PROFILE_TASKS_TASK_OUTPUT_LIMIT=100
166
167 # note, if I use a machine I dont want files readable by all users, set
168 # umask 077 # If fewer than 4 digits are entered, leading zeros are assumed
169
170 # i for insensitive. the rest from
171 # X means dont remove the current screenworth of output upon exit
172 # R means to show colors n things
173 export LESS=RXi
174 export SYSTEMD_LESS=$LESS
175
176
177 # * include files
178
179
180
181 # if someone exported $SOE (stop on error), catch errors.
182 #
183 # Note, on debian this results in the following warning when in ssh,
184 # hich I haven't figured out how to fix. It doesn't happen if we source
185 # after the shell has started
186 #
187 # bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
188 # bash: warning: cannot start debugger; debugging mode disabled
189 if [[ $SOE ]]; then
190 if [[ -e /a/bin/errhandle/err ]]; then
191 source /a/bin/errhandle/err
192 fi
193 fi
194
195 # based on readme.debian. dunno if this will break on other distros.
196 if [[ -s /usr/share/wcd/wcd-include.sh ]]; then
197 source /usr/share/wcd/wcd-include.sh
198 fi
199
200 if [[ -s /a/bin/small-misc-bash/ll-function ]]; then
201 source /a/bin/small-misc-bash/ll-function
202 elif [[ -s ~/.iank/ll-function ]]; then
203 # shellcheck source=/a/bin/small-misc-bash/ll-function
204 source ~/.iank/ll-function
205 fi
206
207
208 # * functions
209
210
211 ..() { c ..; }
212 ...() { c ../..; }
213 ....() { c ../../..; }
214 .....() { c ../../../..; }
215 ......() { c ../../../../..; }
216
217 # file cut copy and paste, like the text buffers :)
218 # I havnt tested these.
219 _fbufferinit() { # internal use
220 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
221 rm -rf "${my_f_tempdir:?}"/*
222 }
223 fcp() { # file cp
224 _fbufferinit
225 cp "$@" "$my_f_tempdir"/
226 }
227 fct() { # file cut
228 _fbufferinit
229 mv "$@" "$my_f_tempdir"/
230 }
231 fpst() { # file paste
232 [[ $2 ]] && { echo too many arguments; return 1; }
233 target=${1:-.}
234 cp "$my_f_tempdir"/* "$target"
235 }
236
237 _khfix_common() {
238 local host=${1##*@}
239 local ip port
240 read -r ip port < <(timeout 1 ssh -oBatchMode=yes -oControlMaster=no -oControlPath=/ -v $1 |& sed -rn "s/debug1: Connecting to $host \[([^\]*)] port ([0-9]+).*/\1 \2/p")
241 if [[ ! $ip ]]; then
242 echo "khfix: ssh failed"
243 return 1
244 fi
245 if [[ $port != 22 ]]; then
246 ip_entry="[$ip]:$port"
247 host_entry="[$host]:$port"
248 else
249 ip_entry=$ip
250 host_entry=$host
251 fi
252 ssh-keygen -R "$host_entry" -f $(readlink -f ~/.ssh/known_hosts)
253 echo "khfix: removing key for $ip_entry"
254 ssh-keygen -R "$ip_entry" -f $(readlink -f ~/.ssh/known_hosts)
255 }
256 khfix() { # known hosts fix
257 _khfix_common "$@" || return 1
258 ssh $1 :
259 }
260 khcopy() {
261 _khfix_common "$@"
262 ssh-copy-id $1
263 }
264
265 a() {
266 local x
267 x=$(readlink -nf "${1:-$PWD}")
268 # yes, its kinda dumb that xclip/xsel cant do this in one invocation
269 echo -n "$x" | xclip -selection clipboard
270 echo -n "$x" | xclip
271 }
272
273 ack() { ack-grep "$@"; }
274
275 b() {
276 # backwards
277 c -
278 }
279
280
281 # c. better cd
282 if type -p wcd &>/dev/null; then
283 if [[ $RLC_INSIDE_EMACS ]]; then
284 c() { wcd -c -z 50 -o "$@"; }
285 else
286 # lets see what the fancy terminal does from time to time
287 c() { wcd -c -z 50 "$@"; }
288 fi
289 else
290 c() { cd "$@"; }
291 fi
292
293 c4() { c /var/log/exim4; }
294
295 caa() { git commit --amend --no-edit -a; }
296
297 caf() {
298 # shellcheck disable=SC2033
299 find -L $1 -type f -not \( -name .svn -prune -o -name .git -prune \
300 -o -name .hg -prune -o -name .editor-backups -prune \
301 -o -name .undo-tree-history -prune \) \
302 -exec bash -c '. ~/.bashrc; hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
303
304 }
305
306 calc() { echo "scale=3; $*" | bc -l; }
307 # no having to type quotes, but also no command history:
308 clc() {
309 local x
310 read -r x
311 echo "scale=3; $x" | bc -l
312 }
313
314 cam() {
315 git commit -am "$*"
316 }
317
318
319 ccat () { # config cat. see a config without extra lines.
320 grep '^\s*[^;[:space:]#]' "$@" || [[ $? == 1 ]]
321 }
322
323
324 _cdiff-prep() {
325 # join options which are continued to multiples lines onto one line
326 local first=true
327 while IFS= read -r line; do
328 # remove leading spaces/tabs. assumes extglob
329 if [[ $line == "[ ]*" ]]; then
330 line="${line##+( )}"
331 fi
332 if $first; then
333 pastline="$line"
334 first=false
335 elif [[ $line == *=* ]]; then
336 echo "$pastline" >> "$2"
337 pastline="$line"
338 else
339 pastline="$pastline $line"
340 fi
341 done < <(grep -vE '^([ \t]*#|^[ \t]*$)' "$1")
342 echo "$pastline" >> "$2"
343 }
344
345 cdiff() {
346 # diff config files,
347 # setup for format of postfix, eg:
348 # option = stuff[,]
349 # [more stuff]
350 local pastline unified f1 f2
351 unified="$(mktemp)"
352 f1="$(mktemp)"
353 f2="$(mktemp)"
354 _cdiff-prep "$1" "$f1"
355 _cdiff-prep "$2" "$f2"
356 cat "$f1" "$f2" | grep -Po '^[^=]+=' | sort | uniq > "$unified"
357 while IFS= read -r line; do
358 # the default bright red / blue doesnt work in emacs shell
359 dwdiff -cblue,red -A best -d " ," <(grep "^$line" "$f1" || echo ) <(grep "^$line" "$f2" || echo ) | colordiff
360 done < "$unified"
361 }
362
363
364 cat-new-files() {
365 local start=$SECONDS
366 local dir="$1"
367 inotifywait -m "$dir" -e create -e moved_to |
368 # shellcheck disable=SC2030
369 while read -r filedir _ file; do
370 cat "$filedir$file"
371 hr
372 calc $((SECONDS - start)) / 60
373 sleep 5
374 done
375
376 }
377
378 # shellcheck disable=SC2032
379 chown() {
380 # makes it so chown -R symlink affects the symlink and its target.
381 if [[ $1 == -R ]]; then
382 shift
383 command chown -h "$@"
384 command chown -R "$@"
385 else
386 command chown "$@"
387 fi
388 }
389
390 cim() {
391 git commit -m "$*"
392 }
393
394 cl() {
395 # choose recent directory. cl = cd list
396 c =
397 }
398
399 d() { builtin bg; }
400 complete -A stopped -P '"%' -S '"' d
401
402
403 dc() {
404 diff --strip-trailing-cr -w "$@" # diff content
405 }
406
407 despace() {
408 local x y
409 for x in "$@"; do
410 y="${x// /_}"
411 safe_rename "$x" "$y"
412 done
413 }
414
415 dig() {
416 command dig +nostats +nocmd "$@"
417 }
418 # Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed.
419 digsort() {
420 local sec
421 sec=
422 dig +nordflag "$@" | sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' | while read -r l; do
423 if [[ $l == [^\;]* ]]; then
424 sec+="$l"$'\n'
425 else
426 if [[ $sec ]]; then
427 printf "%s" "$sec" | sort
428 sec=
429 fi
430 printf "%s\n" "$l"
431 fi
432 done
433 }
434 # compare digs to the 2 servers
435 # usage: digdiff @server1 @server2 DIG_ARGS
436 # note: only the soa master nameserver will respond with
437 # ra "recursive answer" flag. That difference is meaningless afaik.
438 digdiff() {
439 local s1 s2
440 s1=$1
441 shift
442 s2=$1
443 shift
444 digsort $s1 "$@" | tee /tmp/digdiff
445 diff -u /tmp/digdiff <(digsort $s2 "$@")
446 }
447
448 dt() {
449 date "+%A, %B %d, %r" "$@"
450 }
451
452 dus() { # du, sorted, default arg of
453 du -sh ${@:-*} | sort -h
454 }
455
456
457
458 e() { echo "$@"; }
459
460 # echo args
461 ea() {
462 if (( ! $# )); then
463 echo no args
464 fi
465 for arg; do
466 printf "%qEOL\n" "${arg}"
467 printf "%s" "${arg}" |& hexdump -C
468 done
469 }
470 # echo vars. print var including escapes, etc
471 ev() {
472 if (( ! $# )); then
473 echo no args
474 fi
475 for arg; do
476 printf "%qEOL\n" "${!arg}"
477 printf "%s" "${!arg}" |& hexdump -C
478 done
479 }
480
481
482 ediff() {
483 [[ ${#@} == 2 ]] || { echo "error: ediff requires 2 arguments"; return 1; }
484 emacs --eval "(ediff-files \"$1\" \"$2\")"
485 }
486
487 # mail related
488 etail() {
489 tail -F /var/log/exim4/mainlog -n 200
490 }
491 eless() {
492 less /var/log/exim4/mainlog
493 }
494 eqcat() {
495 exiqgrep -i | while read -r i; do
496 exim -Mvh $i; hr; exim -Mvb $i; hr;
497 exigrep $i /var/log/exim4/mainlog; hr
498 done
499 }
500
501
502 # shellcheck disable=SC2032
503 f() {
504 # cd forward
505 c +
506 }
507
508 fa() {
509 # find array. make an array of file names found by find into $x
510 # argument: find arguments
511 # return: find results in an array $x
512 while read -rd ''; do
513 x+=("$REPLY");
514 done < <(find "$@" -print0);
515 }
516
517 faf() { # find all files. use -L to follow symlinks
518 find $@ -not \( -name .svn -prune -o -name .git -prune \
519 -o -name .hg -prune -o -name .editor-backups -prune \
520 -o -name .undo-tree-history -prune \) -type f 2>/dev/null
521 }
522
523 # mail related
524 frozen() {
525 rm -rf /tmp/frozen
526 sudo mailq |gr frozen|awk '{print $3}' | while read -r id; do
527 sudo exim -Mvl $id
528 echo
529 sudo exim -Mvh $id
530 echo
531 sudo exim -Mvb $id
532 echo -e '\n\n##############################\n'
533 done | tee -a /tmp/frozen
534 }
535 frozenrm() {
536 local ids=()
537 while read -r line; do
538 printf '%s\n' "$line"
539 ids+=($(printf '%s\n' "$line" |gr frozen|awk '{print $3}'))
540 done < <(s mailq)
541 echo "sleeping for 2 in case you change your mind"
542 sleep 2
543 sudo exim -Mrm "${ids[@]}"
544 }
545
546 funce() {
547 # like -e for functions. returns on error.
548 # at the end of the function, disable with:
549 # trap ERR
550 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
551 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"
552 trap ERR
553 return' ERR
554 }
555
556 getdir () {
557 local help="Usage: getdir [--help] PATH
558 Output the directory of PATH, or just PATH if it is a directory."
559 if [[ $1 == --help ]]; then
560 echo "$help"
561 return 0
562 fi
563 if [[ $# -ne 1 ]]; then
564 echo "getdir error: expected 1 argument, got $#"
565 return 1
566 fi
567 if [[ -d $1 ]]; then
568 echo "$1"
569 else
570 local dir
571 dir="$(dirname "$1")"
572 if [[ -d $dir ]]; then
573 echo "$dir"
574 else
575 echo "getdir error: directory does not exist"
576 return 1
577 fi
578 fi
579 }
580
581 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
582 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
583 local root
584 root=$(gitroot) || return 1 # function to set gitroot
585 builtin cd "$root"
586 git symbolic-ref HEAD refs/heads/$1
587 rm .git/index
588 git clean -fdx
589 }
590
591 # shellcheck disable=SC2120
592 gitroot() {
593 local help="Usage: gitroot [--help]
594 Print the full path to the root of the current git repo
595
596 Handles being within a .git directory, unlike git rev-parse --show-toplevel,
597 and works in older versions of git which did not have that."
598 if [[ $1 == --help ]]; then
599 echo "$help"
600 return
601 fi
602 local p
603 p=$(git rev-parse --git-dir) || { echo "error: not in a git repo" ; return 1; }
604 [[ $p != /* ]] && p=$PWD
605 echo "${p%%/.git}"
606 }
607
608 gh() {
609 # i got an error, gh not found when doing a pull request, it seems like it wants itself in it\'s path.
610 local _oldpath="$PATH"
611 PATH="$PATH:$HOME/node_modules/.bin"
612 command gh "$@"
613 PATH="$_oldpath"
614 }
615
616 gmacs() {
617 # quit will prompt if the program crashes.
618 gdb -ex=r -ex=quit --args emacs "$@"; r;
619 }
620
621 gdkill() {
622 # kill the emacs daemon
623 pk1 emacs --daemon
624 }
625
626 gr() {
627 grep -iIP --color=auto "$@"
628 }
629
630 grr() { # grep recursive
631 # Don't return 1 on nonmatch because this is meant to be
632 # interactive, not in a conditional.
633 if [[ ${#@} == 1 ]]; then
634 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" . || [[ $? == 1 ]]
635 else
636 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" || [[ $? == 1 ]]
637 fi
638 }
639 rg() {
640 command rg -i -M 200 "$@"
641 }
642
643 hr() { # horizontal row. used to break up output
644
645 printf "$(tput setaf 5)â–ˆ$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
646 echo
647 }
648
649 hrcat() { local f; for f; do [[ -f $f ]] || continue; hr; echo "$f"; cat "$f"; done }
650
651 # get latest hub and run it
652 # main command to use:
653 # hub pull-request --no-edit
654 # --no-edit means to use the first commit\'s message as the pull request message.
655 # Also, you need to use a feature branch, not master in your fork.
656 # On first use, you input username/pass and it gets an oath token so you dont have to repeat
657 # it\'s at ~/.config/hub
658 hub() {
659 local up uptar updir p
660 p=/github/hub/releases/
661 up=https://github.com/$(curl -s https://github.com$p| grep -o $p'download/[^/]*/hub-linux-amd64[^"]*' | head -n1)
662 uptar=${up##*/}
663 updir=${uptar%.tgz}
664 if [[ ! -e /a/opt/$updir ]]; then
665 rm -rf /a/opt/hub-linux-amd64*
666 wget -P /a/opt $up
667 tar -C /a/opt -zxf /a/opt/$uptar
668 rm -f /a/opt/$uptar
669 sudo /a/opt/$updir/install
670 fi
671
672 # save token across computers
673 if [[ ! -L ~/.config/hub ]]; then
674 if [[ -e ~/.config/hub ]]; then
675 mv ~/.config/hub /p/c/subdir_files/.config/
676 fi
677 if [[ -e /p/c/subdir_files/.config/hub ]]; then
678 conflink
679 fi
680 fi
681 command hub "$@"
682 }
683
684 i() { git "$@"; }
685 # modified from ~/local/bin/git-completion.bash
686 # other completion commands are mostly taken from bash_completion package
687 complete -o bashdefault -o default -o nospace -F _git i 2>/dev/null \
688 || complete -o default -o nospace -F _git i
689
690 if ! type service &>/dev/null; then
691 service() {
692 echo actually running: systemctl $2 $1
693 systemctl $2 $1
694 }
695 fi
696
697
698
699 ic() {
700 # fast commit all
701 git commit -am "$*"
702 }
703
704
705
706 ifn() {
707 # insensitive find
708 find -L . -not \( -name .svn -prune -o -name .git -prune \
709 -o -name .hg -prune -o -name .editor-backups -prune \
710 -o -name .undo-tree-history -prune \) -iname "*$**" 2>/dev/null
711 }
712
713 ipdrop() {
714 sudo iptables -A INPUT -s $1 -j DROP
715 }
716
717
718 istext() {
719 grep -Il "" "$@" &>/dev/null
720 }
721
722 jtail() {
723 journalctl -n 10000 -f "$@"
724 }
725 jr() { journalctl "$@" ; }
726 jrf() { journalctl -f "$@" ; }
727
728 l() {
729 if [[ $PWD == /[iap] ]]; then
730 command ls -A --color=auto -I lost+found "$@"
731 else
732 command ls -A --color=auto "$@"
733 fi
734 }
735
736
737 lcn() { locate -i "*$**"; }
738
739 lg() { LC_COLLATE=C.UTF-8 ll --group-directories-first; }
740
741 lt() { ll -tr "$@"; }
742
743 lld() { ll -d "$@"; }
744
745 low() { # make filenames lowercase, remove bad chars
746 local f new
747 for f in "$@"; do
748 new="${f,,}" # downcase
749 new="${new//[^[:alnum:]._-]/_}" # sub bad chars
750 new="${new#"${new%%[[:alnum:]]*}"}" # remove leading/trailing non-alnum
751 new="${new%"${new##*[[:alnum:]]}"}"
752 # remove bad underscores, like __ and _._
753 new=$(echo $new | sed -r 's/__+/_/g;s/_+([.-])|([.-])_+/\1/g')
754 safe_rename "$f" "$new" || return 1
755 done
756 return 0
757 }
758
759 lower() { # make first letter of filenames lowercase.
760 local x
761 for x in "$@"; do
762 if [[ ${x::1} == [A-Z] ]]; then
763 y=$(tr '[:upper:]' '[:lower:]' <<<"${x::1}")"${x:1}"
764 safe_rename "$x" "$y" || return 1
765 fi
766 done
767 }
768
769
770 k() { # history search
771 grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | tail -n 80 || [[ $? == 1 ]];
772 }
773
774 ks() { # history search
775 grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | uniq || [[ $? == 1 ]];
776 }
777
778
779 make-targets() {
780 # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
781 make -qp | awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
782 }
783
784 mkc() {
785 mkdir "$1"
786 c "$1"
787 }
788
789 mkct() {
790 mkc $(mktemp -d)
791 }
792
793 mkt() { # mkdir and touch file
794 local path="$1"
795 mkdir -p "$(dirname "$path")"
796 touch "$path"
797 }
798
799 # shellcheck disable=SC2032
800 mkdir() { command mkdir -p "$@"; }
801
802 nopanic() {
803 # shellcheck disable=SC2024
804 sudo tee -a /var/log/exim4/paniclog-archive </var/log/exim4/paniclog; sudo truncate -s0 /var/log/exim4/paniclog
805 }
806
807 p8() { ping 8.8.8.8; }
808 p6() { ping6 2001:4860:4860::8888; }
809
810 pkx() { # package extract
811 local pkg cached tmp f
812 c $(mktemp -d)
813 pkg=$1
814 # shellcheck disable=SC2012
815 cached=$(ls -t /var/cache/apt/archives/$pkg* | tail -n1 2>/dev/null) ||:
816 if [[ $cached ]]; then
817 cp $cached .
818 else
819 aptitude download $pkg || return 1
820 fi
821 tmp=(*); f=${tmp[0]} # only 1 expected
822 ex $f
823 rm -f $f
824 }
825
826 # pgrep and kill
827 pk1() {
828 local pid
829 pid=($(pgrep -f "$*"))
830 case ${#pid[@]} in
831 1)
832 # shellcheck disable=SC2128
833 {
834 ps -F $pid
835 m kill $pid
836 }
837 ;;
838 0) echo "no pid found" ;;
839 *)
840 ps -F ${pid[@]}
841 ;;
842 esac
843 }
844
845 psg () {
846 local x y help
847 help="Usage: psg [--help] GREP_ARGS
848 grep ps and output in a nice format"
849 if [[ $1 == --help ]]; then
850 echo "$help"
851 return
852 fi
853 x=$(sudo ps -eF)
854 # final grep is because some commands tend to have a lot of trailing spaces
855 y=$(echo "$x" | grep -iP "$@" | grep -o '.*[^ ]') ||:
856 if [[ $y ]]; then
857 echo "$x" | head -n 1 || [[ $? == 141 ]]
858 echo "$y"
859 fi
860 }
861
862 pubip() { curl -4s https://icanhazip.com; }
863 pubip6() { curl -6s https://icanhazip.com; }
864 whatismyip() { pubip; }
865
866 pwgen() {
867 # -m = min length
868 # -x = max length
869 # -t = print pronunciation
870 apg -m 14 -x 17 -t
871 for (( i=0; i<10; i++ )); do
872 shuf -n3 /usr/share/hunspell/en_US.dic | sed 's,/.*,,' | paste -sd . -
873
874 done
875 }
876
877 pwlong() {
878 # -M CLN = use Caps, Lowercase, Numbers
879 # -n 1 = 1 password
880 # -a 1 = use random instead of pronounceable algorithm
881 apg -m 50 -x 70 -n 1 -a 1 -M CLN
882 }
883
884
885 q() { # start / launch a program in the backround and redir output to null
886 "$@" &> /dev/null &
887 }
888
889 # shellcheck disable=SC2120
890 r() {
891 if [[ $HISTFILE ]]; then
892 history -a # save history
893 fi
894 trap ERR # this avoids a segfault
895 exit ${1:0}
896 # i had this redir, not sure why
897 # exit "$@" 2>/dev/null
898 }
899
900 rl() {
901 # rsync, root is required to keep permissions right.
902 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
903 # --no-times --delete
904 # basically, make an exact copy, use checksums instead of file times to be more accurate
905 rsync -ahvic --delete "$@"
906 }
907 rld() {
908 # like rlu, but dont delete files on the target end which
909 # do not exist on the original end.
910 rsync -ahvic "$@"
911 }
912 complete -F _rsync -o nospace rld rl rlt
913
914 rlt() {
915 # rl without preserving modification time.
916 rsync -ahvic --delete --no-t "$@"
917 }
918
919 rlu() { # [OPTS] HOST PATH
920 # eg. rlu -opts frodo /testpath
921 # relative paths will expanded with readlink -f.
922 opts=("${@:1:$#-2}") # 1 to last -2
923 path="${*:$#}" # last
924 host="${*:$#-1:1}" # last -1
925 if [[ $path == .* ]]; then
926 path=$(readlink -f $path)
927 fi
928 # rync here uses checksum instead of time so we dont mess with
929 # unison relying on time as much. g is for group, same reason
930 # to keep up with unison.
931 sudo rsync -rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
932 }
933
934 rmstrips() {
935 ssh fencepost head -n 300 /gd/gnuorg/EventAndTravelInfo/rms-current-trips.txt | less
936 }
937
938 s() {
939 # background
940 # I use a function because otherwise we cant use in a script,
941 # cant assign to variable.
942 #
943 # note: gksudo is recommended for X apps because it does not set the
944 # home directory to the same, and thus apps writing to ~ fuck things up
945 # with root owned files.
946 #
947 if [[ $EUID != 0 || $1 == -* ]]; then
948 # shellcheck disable=SC2034
949 SUDOD="$PWD" command sudo -i "$@"
950 else
951 "$@"
952 fi
953 }
954
955 safe_rename() { # warn and dont rename if file exists.
956 # mv -n exists, but it\'s silent
957 if [[ $# != 2 ]]; then
958 echo safe_rename error: $# args, need 2 >2
959 return 1
960 fi
961 if [[ $1 != "$2" ]]; then # yes, we want to silently ignore this
962 if [[ -e $2 || -L $2 ]]; then
963 echo "Cannot rename $1 to $2 as it already exists."
964 else
965 mv -vi "$1" "$2"
966 fi
967 fi
968 }
969
970
971 sb() { # sudo bash -c
972 # use sb instead of s is for sudo redirections,
973 # eg. sb 'echo "ok fine" > /etc/file'
974 local SUDOD="$PWD"
975 sudo -i bash -c "$@"
976 }
977 complete -F _root_command s sb
978
979
980 ser() {
981 local s; [[ $EUID != 0 ]] && s=sudo
982 if type -p systemctl &>/dev/null; then
983 $s systemctl $1 $2
984 else
985 $s service $2 $1
986 fi
987 }
988 # like restart, but do nothing if its not already started
989 srestart() {
990 local service=$1
991 if [[ $(s systemctl --no-pager show -p ActiveState $service ) == ActiveState=active ]]; then
992 systemctl restart $service
993 fi
994 }
995
996 setini() { # set a value in a .ini style file
997 key="$1" value="$2" section="$3" file="$4"
998 if [[ -s $file ]]; then
999 sed -ri -f - "$file" <<EOF
1000 # remove existing keys
1001 / *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d}
1002 # add key
1003 /^\s*\[$section\]/a $key=$value
1004 # from section to eof, do nothing
1005 /^\s*\[$section\]/,\$b
1006 # on the last line, if we haven't found section yet, add section and key
1007 \$a [$section]\\
1008 $key=$value
1009 EOF
1010 else
1011 cat >"$file" <<EOF
1012 [$section]
1013 $key=$value
1014 EOF
1015 fi
1016 }
1017
1018 sgo() { # service go
1019 service=$1
1020 ser restart $service || return 1
1021 if type -p systemctl &>/dev/null; then
1022 ser enable $service
1023 fi
1024 }
1025
1026 sgu() {
1027 systemctl list-unit-files | rg "$@"
1028 }
1029
1030
1031 sk() {
1032 # 2086: unquoted $var
1033 # 2046: unquoted $(cmd)
1034 # 2068: Double quote array expansions to avoid re-splitting elements.
1035 # 2119: Functions with optional args get bad warnings when none are passed.
1036 # 2033: too many false positives for thing that will never work, passing shell function to find.
1037 # i had -x as an arg, but debian testing(stretch) doesn\'t support it
1038 shellcheck -x -e 2086,2046,2068,2119,2033 "$@" || return $?
1039 # had this before. not sure what it is 2119
1040 }
1041
1042
1043 slog() {
1044 # log with script. timing is $1.t and script is $1.s
1045 # -l to save to ~/typescripts/
1046 # -t to add a timestamp to the filenames
1047 local logdir do_stamp arg_base
1048 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
1049 logdir="/a/dt/"
1050 do_stamp=false
1051 while getopts "lt" option
1052 do
1053 case $option in
1054 l ) arg_base=$logdir ;;
1055 t ) do_stamp=true ;;
1056 esac
1057 done
1058 shift $((OPTIND - 1))
1059 arg_base+=$1
1060 [[ -e $logdir ]] || mkdir -p $logdir
1061 $do_stamp && arg_base+=$(date +%F.%T%z)
1062 script -t $arg_base.s 2> $arg_base.t
1063 }
1064 splay() { # script replay
1065 #logRoot="$HOME/typescripts/"
1066 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
1067 scriptreplay "$1.t" "$1.s"
1068 }
1069
1070 sr() {
1071 # sudo redo. be aware, this command may not work right on strange distros or earlier software
1072 if [[ $# == 0 ]]; then
1073 sudo -E bash -c -l "$(history -p '!!')"
1074 else
1075 echo this command redos last history item. no argument is accepted
1076 fi
1077 }
1078
1079 srm () {
1080 # with -ll, less secure but faster.
1081 command srm -ll "$@"
1082 }
1083
1084 srun() {
1085 scp $2 $1:/tmp
1086 ssh $1 /tmp/${2##*/} $(printf "%q\n" "${@:2}")
1087 }
1088
1089
1090 swap() {
1091 local tmp
1092 tmp=$(mktemp)
1093 mv $1 $tmp
1094 mv $2 $1
1095 mv $tmp $2
1096 }
1097
1098 tclock() { # terminal clock
1099 local x
1100 clear
1101 date +%l:%_M
1102 len=60
1103 # this goes to full width
1104 #len=${1:-$((COLUMNS -7))}
1105 x=1
1106 while true; do
1107 if (( x == len )); then
1108 end=true
1109 d="$(date +%l:%_M) "
1110 else
1111 end=false
1112 d=$(date +%l:%M:%_S)
1113 fi
1114 echo -en "\r"
1115 echo -n "$d"
1116 for ((i=0; i<x; i++)); do
1117 if (( i % 6 )); then
1118 echo -n _
1119 else
1120 echo -n .
1121 fi
1122 done
1123 if $end; then
1124 echo
1125 x=1
1126 else
1127 x=$((x+1))
1128 fi
1129 sleep 5
1130 done
1131 }
1132
1133
1134 te() {
1135 # test existence / exists
1136 local ret=0
1137 for x in "$@"; do
1138 [[ -e "$x" || -L "$x" ]] || ret=1
1139 done
1140 return $ret
1141 }
1142
1143
1144 tx() { # toggle set -x, and the prompt so it doesnt spam
1145 if [[ $- == *x* ]]; then
1146 set +x
1147 PROMPT_COMMAND=prompt-command
1148 # disabled due to issue on stretch, running ll we get error. something
1149 # about the DEBUG trap is broken
1150 # if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
1151 # trap 'settitle "$BASH_COMMAND"' DEBUG
1152 # fi
1153 else
1154 # normally, i would just execute these commands in the function.
1155 # however, DEBUG is not inherited, so we need to run it outside a function.
1156 # And we want to run set -x afterwards to avoid spam, so we cram everything
1157 # in here, and then it will run after this function is done.
1158 #PROMPT_COMMAND='trap DEBUG; unset PROMPT_COMMAND; PS1="\w \$ "; set -x'
1159
1160 unset PROMPT_COMMAND
1161 PS1="\w \$ "
1162 set -x
1163 fi
1164 }
1165
1166 psnetns() {
1167 # show all processes in the network namespace $1.
1168 # blank entries appear to be subprocesses/threads
1169 local x netns
1170 netns=$1
1171 ps -w | head -n 1
1172 sudo find -L /proc/[1-9]*/task/*/ns/net -samefile /run/netns/$netns | cut -d/ -f5 | \
1173 while read -r l; do
1174 x=$(ps -w --no-headers -p $l);
1175 if [[ $x ]]; then echo "$x"; else echo $l; fi;
1176 done
1177 }
1178
1179 m() { printf "%s\n" "$*"; "$@"; }
1180
1181
1182 virshrm() {
1183 for x in "$@"; do virsh destroy "$x"; virsh undefine "$x"; done
1184 }
1185
1186 vm-set-listen(){
1187 local t
1188 t=$(mktemp)
1189 local vm=$1
1190 local ip=$2
1191 sudo virsh dumpxml $vm | sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
1192 sed -r "s/listen='[^']+/listen='$ip/"> $t
1193 sudo virsh undefine $vm
1194 sudo virsh define $t
1195 }
1196
1197
1198 vmshare() {
1199 vm-set-listen $1 0.0.0.0
1200 }
1201
1202
1203 vmunshare() {
1204 vm-set-listen $1 127.0.0.1
1205 }
1206
1207 # * misc stuff
1208
1209
1210 # temporary variables to test colorization
1211 # some copied from gentoo /etc/bash/bashrc,
1212 use_color=false
1213 # dircolors --print-database uses its own built-in database
1214 # instead of using /etc/DIR_COLORS. Try to use the external file
1215 # first to take advantage of user additions.
1216 safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
1217 match_lhs=""
1218 [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
1219 [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
1220 [[ -z ${match_lhs} ]] \
1221 && type -P dircolors >/dev/null \
1222 && match_lhs=$(dircolors --print-database)
1223 # test if our $TERM is in the TERM values in dircolor
1224 [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
1225
1226
1227 if ${use_color} && [[ $- == *i* ]]; then
1228
1229 term_bold="$(tput bold)"
1230 term_red="$(tput setaf 1)"
1231 term_green="$(tput setaf 2)"
1232 term_yellow="$(tput setaf 3)"
1233 term_purple="$(tput setaf 5)"
1234 term_nocolor="$(tput sgr0)" # no font attributes
1235
1236 # unused so far. commented for shellcheck
1237 # term_underl="$(tput smul)"
1238 # term_blue="$(tput setaf 4)"
1239 # term_cyan="$(tput setaf 6)"
1240
1241 fi
1242 # Try to keep environment pollution down, EPA loves us.
1243 unset safe_term match_lhs use_color
1244
1245 # * prompt
1246
1247
1248 if [[ $- == *i* ]]; then
1249
1250 # this needs to come before next ps1 stuff
1251 # this stuff needs bash 4, feb 2009,
1252 # old enough to no longer condition on $BASH_VERSION anymore
1253 shopt -s autocd
1254 shopt -s dirspell
1255 PS1='\w'
1256 if [[ $- == *i* ]] && [[ ! $RLC_INSIDE_EMACS ]]; then
1257 PROMPT_DIRTRIM=2
1258 bind -m vi-command B:shell-backward-word
1259 bind -m vi-command W:shell-forward-word
1260 fi
1261
1262 if [[ $SSH_CLIENT || $SUDO_USER ]]; then
1263 PS1="\h $PS1"
1264 fi
1265
1266 # emacs terminal has problems if this runs slowly,
1267 # so I've thrown a bunch of things at the wall to speed it up.
1268 prompt-command() {
1269 local return=$? # this MUST COME FIRST
1270 local ps_char ps_color
1271 unset IFS
1272
1273 if [[ $HISTFILE ]]; then
1274 history -a # save history
1275 fi
1276
1277 case $return in
1278 0) ps_color="$term_purple"
1279 ps_char='\$'
1280 ;;
1281 1) ps_color="$term_green"
1282 ps_char="$return \\$"
1283 ;;
1284 *) ps_color="$term_yellow"
1285 ps_char="$return \\$"
1286 ;;
1287 esac
1288 if [[ ! -O . ]]; then # not owner
1289 if [[ -w . ]]; then # writable
1290 ps_color="$term_bold$term_red"
1291 else
1292 ps_color="$term_bold$term_green"
1293 fi
1294 fi
1295
1296 # faster than sourceing the file im guessing
1297 if [[ -e /dev/shm/iank-status ]]; then
1298 eval $(< /dev/shm/iank-status)
1299 fi
1300 if [[ ! $SSH_CLIENT && $MAIL_HOST != "$HOSTNAME" ]]; then
1301 ps_char="@ $ps_char"
1302 fi
1303 PS1="${PS1%"${PS1#*[wW]}"} \[$ps_color\]$ps_char\[$term_nocolor\] "
1304 }
1305 PROMPT_COMMAND=prompt-command
1306
1307 settitle () {
1308 if [[ $TERM == screen* ]]; then
1309 local title_escape="\033]..2;"
1310 else
1311 local title_escape="\033]0;"
1312 fi
1313 if [[ $0 != prompt-command ]]; then
1314 echo -ne "$title_escape$USER@$HOSTNAME ${PWD/#$HOME/~} "
1315 printf "%s" "$*"
1316 echo -ne "\007"
1317 fi
1318 }
1319
1320 # for titlebar.
1321 # condition from the screen man page i think.
1322 # note: duplicated in tx()
1323 # disabled. see note in tx
1324 # if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
1325 # trap 'settitle "$BASH_COMMAND"' DEBUG
1326 # else
1327 # trap DEBUG
1328 # fi
1329
1330 fi
1331
1332 # * stuff that makes sense to be at the end
1333
1334
1335 # best practice
1336 unset IFS
1337
1338 # shellcheck disable=SC1090
1339 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
1340
1341
1342 # ensure no bad programs appending to this file will have an affect
1343 return 0