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
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
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
21 # if [[ -s /usr/share/bash-completion/completions/git ]]; then
22 # source /usr/share/bash-completion/completions/git
24 # if [[ -s /usr/share/bash-completion/completions/gitk ]]; then
25 # source /usr/share/bash-completion/completions/gitk
28 # for testing error catching:
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
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
53 # use extra globing features.
55 # include .files when globbing, but ignore files name . and ..
56 # setting this also sets dotglob.
57 export GLOBIGNORE
="*/.:*/.."
59 # Useful info. see man bash.
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.
71 # make tab on an empty line do nothing
72 shopt -s no_empty_cmd_completion
74 # fix spelling errors for cd, only in interactive shell
76 # append history instead of overwritting it
78 # for compatibility, per gentoo/debian bashrc
80 # attempt to save multiline single commands as single history entries.
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
91 export LC_INSIDE_EMACS
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
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('');
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
117 export NODE_NO_READLINE
=1
121 export SSH_CONFIG_FILE_OVERRIDE
=/root
/.ssh
/confighome
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.
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.
133 # Traling : means for emacs to add its own stuff on to the end.
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
:.
:
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
145 if [[ $
- == *i
* ]]; then
146 # for readline-complete.el
147 if [[ $LC_INSIDE_EMACS ]]; then
148 # all for readline-complete.el
150 bind 'set horizontal-scroll-mode on'
151 bind 'set print-completions-horizontally on'
152 bind '"\C-i": self-insert'
156 if [[ $TERM != dumb
]] && test -t 1; then
160 if [[ $KONSOLE_PROFILE_NAME ]]; then
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
170 # copying from the alacritty example above,
171 if [[ $TERM == xterm-kitty
]]; then
172 if [[ ! -e /usr
/share
/terminfo
/x
/xterm-kitty
]]; then
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
182 # todo: not sure this works in sakura
184 #bind "\C-w": kill-region
185 # sakura == xterm-256color
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
192 # make ctrl-backspace work. for konsole, i fixed it through
193 # /home/iank/.local/share/konsole/default.keytab
195 bind '"\eOc": shell-forward-word'
196 bind '"\eOd": shell-backward-word'
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
208 # fixup broken backspace in chroots
209 xterm-kitty|alacritty
)
211 TERM
=xterm-256color
command chroot
"$@"
216 export BC_LINE_LENGTH
=0
219 export PROFILE_TASKS_TASK_OUTPUT_LIMIT
=100
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
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
228 export SYSTEMD_LESS
=$LESS
230 export NNN_COLORS
=2136
232 export SL_FILES_DIR
=/b
/ds
/sl
/.iank
233 export SL_INFO_DIR
=/p
/sshinfo
238 if [[ -s $bashrc_dir/path-add-function
]]; then
239 source $bashrc_dir/path-add-function
240 if [[ $SSH_CLIENT ]]; then
241 if grep -qF /home
/iank
/.iank
/e
/e
/etc
/exports
&>/dev
/null
; then
242 export EMACSDIR
=/home
/iank
/.iank
/e
/e
248 # if someone exported $SOE (stop on error), catch errors.
250 # Note, on debian this results in the following warning when in ssh,
251 # hich I haven't figured out how to fix. It doesn't happen if we source
252 # after the shell has started
254 # bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
255 # bash: warning: cannot start debugger; debugging mode disabled
257 if [[ -e /a
/bin
/errhandle
/err
]]; then
258 source /a
/bin
/errhandle
/err
262 # based on readme.debian. dunno if this will break on other distros.
263 if [[ -s /usr
/share
/wcd
/wcd-include.sh
]]; then
264 source /usr
/share
/wcd
/wcd-include.sh
273 if [[ -s $path ]]; then
275 elif [[ -s $bashrc_dir/$file ]]; then
276 source $bashrc_dir/$file
281 mysrc
/a
/bin
/small-misc-bash
/ll-function
282 mysrc
/a
/bin
/distro-functions
/src
/package-manager-abstractions
286 ccomp
() { # copy completion
290 if ! c
=$
(complete
-p $src 2>/dev
/null
); then
291 _completion_loader
$src &>/dev
/null ||
:
292 c
=$
(complete
-p $src 2>/dev
/null
) ||
return 0
303 ....
() { c ..
/..
/..
; }
304 .....
() { c ..
/..
/..
/..
; }
305 ......
() { c ..
/..
/..
/..
/..
; }
310 path
=$
(readlink
-e "$f")
311 echo "cat >$path <<'EOF'"
318 # file cut copy and paste, like the text buffers :)
319 # I havnt tested these.
320 _fbufferinit
() { # internal use
321 ! [[ $my_f_tempdir ]] && my_f_tempdir
=$
(mktemp
-d)
322 rm -rf "${my_f_tempdir:?}"/*
326 cp "$@" "$my_f_tempdir"/
330 mv "$@" "$my_f_tempdir"/
332 fpst
() { # file paste
333 [[ $2 ]] && { echo too many arguments
; return 1; }
335 cp "$my_f_tempdir"/* "$target"
339 local host ip port
file key
340 read -r host ip port
< <(timeout
-s 9 2 ssh -oBatchMode=yes -oControlMaster=no
-oControlPath=/ -v $1 |
& sed -rn "s/debug1: Connecting to ([^ ]+) \[([^\]*)] port ([0-9]+).*/\1 \2 \3/p" ||
: )
341 file=$
(readlink
-f ~
/.ssh
/known_hosts
)
343 echo "khfix: ssh failed"
346 if [[ $port != 22 ]]; then
347 ip_entry
="[$ip]:$port"
348 host_entry
="[$host]:$port"
354 if [[ $host != $ip ]]; then
355 key
=$
(ssh-keygen
-F "$host_entry" -f $file |
sed -r 's/^.*([^ ]+ +[^ ]+) *$/\1/')
357 grep -Fv "$key" "$file" | sponge
"$file"
360 key
=$
(ssh-keygen
-F "$ip_entry" -f $file |
sed -r 's/^.*([^ ]+ +[^ ]+) *$/\1/')
362 grep -Fv "$key" "$file" | sponge
"$file"
364 ll ~
/.ssh
/known_hosts
367 khfix
() { # known hosts fix
368 _khfix_common
"$@" ||
return 1
378 x
=$
(readlink
-nf "${1:-$PWD}")
379 # yes, its kinda dumb that xclip/xsel cant do this in one invocation
380 echo -n "$x" | xclip
-selection clipboard
384 # a1 = awk {print $1}
385 for field
in {1.
.20}; do
386 eval a
$field"() { awk '{print \$$field}'; }"
389 for num
in {1.
.9}; do
390 eval h
$num"() { head -n$num; }"
400 printf '%d.%d.%d.%d\n' $
(echo $1 |
sed 's/../0x& /g')
404 local f out outdir
in
418 m ffmpeg
-threads 0 -i $in -g 192 -vcodec libvpx-vp9
-vf scale
=-1:720 -max_muxing_queue_size 9999 -b:v
750K
-pass 1 -an -f null
/dev
/null
419 m ffmpeg
-y -threads 0 -i $in -g 192 -vcodec libvpx-vp9
-vf scale
=-1:720 -max_muxing_queue_size 9999 -b:v
750K
-pass 2 -c:a libvorbis
-qscale:a
5 $out
424 utcl
() { # utc 24 hour time to local hour 24 hour time
425 echo "print( ($1 $(date +%z | sed -r 's/..$//;s/^(-?)0*/\1/')) % 24)"|python3
429 if ! type -t c
&>/dev
/null
; then
430 if type -p wcd
&>/dev
/null
; then
431 if [[ $LC_INSIDE_EMACS ]]; then
432 c
() { wcd
-c -z 50 -o "$@"; }
434 # lets see what the fancy terminal does from time to time
435 c
() { wcd
-c -z 50 "$@"; }
443 c4
() { c
/var
/log
/exim4
; }
445 caa
() { git commit
--amend --no-edit -a; }
456 # shellcheck disable=SC2033
457 find -L "$@" -type f
-not \
( -name .svn
-prune -o -name .git
-prune \
458 -o -name .hg
-prune -o -name .editor-backups
-prune \
459 -o -name .undo-tree-history
-prune \
) \
460 -exec bash
-c '. ~/.bashrc; hr; echo "$1"; hr; cat "$1"' _
{} \
; 2>/dev
/null
465 calc
() { echo "scale=3; $*" |
bc -l; }
466 # no having to type quotes, but also no command history:
470 echo "scale=3; $x" |
bc -l
477 ccat
() { # config cat. see a config without extra lines.
478 sed -r '/^[[:space:]]*([;#]|--|\/\/|$)/d' "$@"
484 # dev/pts needed for pacman signature check
485 for d
in dev proc sys dev
/pts
; do
487 if ! mountpoint
$d &>/dev
/null
; then
488 m s mount
-o bind /$d $d
494 # dev/pts needed for pacman signature check
495 for d
in dev proc sys dev
/pts
; do
497 if mountpoint
$d &>/dev
/null
; then
505 # join options which are continued to multiples lines onto one line
507 while IFS
= read -r line
; do
508 # remove leading spaces/tabs. assumes extglob
509 if [[ $line == "[ ]*" ]]; then
515 elif [[ $line == *=* ]]; then
516 echo "$pastline" >> "$2"
519 pastline
="$pastline $line"
521 done < <(grep -vE '^([ \t]*#|^[ \t]*$)' "$1")
522 echo "$pastline" >> "$2"
527 # setup for format of postfix, eg:
530 local pastline unified f1 f2
534 _cdiff-prep
"$1" "$f1"
535 _cdiff-prep
"$2" "$f2"
536 cat "$f1" "$f2" |
grep -Po '^[^=]+=' |
sort |
uniq > "$unified"
537 while IFS
= read -r line
; do
538 # the default bright red / blue doesnt work in emacs shell
539 dwdiff
-cblue,red
-A best
-d " ," <(grep "^$line" "$f1" ||
echo ) <(grep "^$line" "$f2" ||
echo ) | colordiff
547 inotifywait
-m "$dir" -e create
-e moved_to |
548 # shellcheck disable=SC2030
549 while read -r filedir _
file; do
552 calc $
((SECONDS
- start
)) / 60
558 # shellcheck disable=SC2032
560 # makes it so chown -R symlink affects the symlink and its target.
561 if [[ $1 == -R ]]; then
563 command chown
-h "$@"
564 command chown
-R "$@"
575 # choose recent directory. cl = cd list
579 d
() { builtin bg "$@"; }
583 diff --strip-trailing-cr -w "$@" # diff content
591 safe_rename
"$x" "$y"
596 command dig +nostats
+nocmd
"$@"
598 # Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed.
602 dig +nordflag
"$@" |
sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' |
while read -r l
; do
603 if [[ $l == [^\
;]* ]]; then
607 printf "%s" "$sec" |
sort
615 # compare digs to the 2 servers
616 # usage: digdiff @server1 @server2 DIG_ARGS
617 # note: only the soa master nameserver will respond with
618 # ra "recursive answer" flag. That difference is meaningless afaik.
625 digsort
$s1 "$@" |
tee /tmp
/digdiff
626 diff -u /tmp
/digdiff
<(digsort
$s2 "$@")
630 date "+%A, %B %d, %r" "$@"
634 dus
() { # du, sorted, default arg of
635 du
-sh ${@:-*} |
sort -h
648 printf "%qEOL\n" "${arg}"
649 printf "%s" "${arg}" |
& hexdump -C
652 # echo vars. print var including escapes, etc
658 if [[ -v $arg ]]; then
659 printf "%qEOL\n" "${!arg}"
660 printf "%s" "${!arg}" |
& hexdump -C
662 echo arg
$arg is
unset
668 [[ ${#@} == 2 ]] ||
{ echo "error: ediff requires 2 arguments"; return 1; }
669 emacs
--eval "(ediff-files \"$1\" \"$2\")"
674 tail -F /var
/log
/exim
4/mainlog
-n 200 "$@"
678 # print exim old pids
680 local configtime pid piduptime now daemonpid
681 printf -v now
'%(%s)T' -1
682 configtime
=$
(stat
-c%Y
/var
/lib
/exim
4/config.autogenerated
)
683 if [[ -s /run
/exim
4/exim.pid
]]; then
684 daemonpid
=$
(cat /run
/exim
4/exim.pid
)
686 for pid
in $
(pgrep
-f '^/usr/sbin/exim4( |$)'); do
687 # the daemonpid gets reexeced on HUP (service reloads), keeping its same old timestamp
688 if [[ $pid == $daemonpid ]]; then
691 piduptime
=$
(awk -v ticks
="$(getconf CLK_TCK)" 'NR==1 { now=$1; next } END { printf "%9.0f\n", now - ($20/ticks) }' /proc
/uptime RS
=')' /proc
/$pid/stat
) ||
: # sometimes pids disappear pretty fast
692 if (( configtime
> now
- piduptime
)); then
698 # exim tail but only watch lines from new pids
701 for pid
in $
(eoldpids
); do
704 if [[ $oldpids ]]; then
705 etail |
awk '$3 !~ /^\[('"${oldpids%|}"')\]$/'
710 # exim watch as old pids go away
712 local configtime pid piduptime now
717 oldpids
=($
(eoldpids
))
718 if (( ! ${#oldpids[@]} )); then
721 # print the date every 20 iterations
722 if (( ! count
% 20 )); then
726 ps
-f -p "${oldpids[*]}"
732 less /var
/log
/exim
4/mainlog
736 exiqgrep
-i -o 60 |
while read -r i
; do
739 hlm exigrep
$i /var
/log
/exim
4/mainlog |
cat ||
:
743 exiqgrep
-i |
xargs exim
-Mrm
748 mkdir
-p /tmp
/edev
/etc
749 cp -ra /etc
/exim4
/tmp
/edev
/etc
750 cp -ra /etc
/alias* /tmp
/edev
/etc
751 find /tmp
/edev
/etc
/exim4
-type f
-execdir sed -i "s,/etc/,/tmp/edev/etc/,g" '{}' +
755 update-exim4.conf
-d /tmp
/edev
/etc
/exim4
-o /tmp
/edev
/e.conf
760 # shellcheck disable=SC2032
767 # find array. make an array of file names found by find into $x
768 # argument: find arguments
769 # return: find results in an array $x
770 while read -rd ''; do
772 done < <(find "$@" -print0);
775 faf
() { # find all files. use -L to follow symlinks
776 find "$@" -not \
( -name .svn
-prune -o -name .git
-prune \
777 -o -name .hg
-prune -o -name .editor-backups
-prune \
778 -o -name .undo-tree-history
-prune \
) -type f
2>/dev
/null
781 # todo: id like to do maybe a daily or hourly cronjob to
782 # check that my history file size is increasing. Ive had it
783 # inexplicably truncated in the past.
786 history |
awk -v IGNORECASE
=1 '{ a=$1; sub(/^( *[^ ]+){4} */, "") }; /'"$*"'/'
787 read -p "press anything but contrl-c to delete"
788 for entry
in $
(history |
awk -v IGNORECASE
=1 '{ a=$1; sub(/^( *[^ ]+){4} */, "") }; /'"$*"'/ { print a }' |
tac); do
797 sudo mailq |gr frozen|
awk '{print $3}' |
while read -r id
; do
803 echo -e '\n\n##############################\n'
804 done |
tee -a /tmp
/frozen
808 while read -r line
; do
809 printf '%s\n' "$line"
810 ids
+=($
(printf '%s\n' "$line" |gr frozen|
awk '{print $3}'))
812 echo "sleeping for 2 in case you change your mind"
814 sudo exim
-Mrm "${ids[@]}"
818 # like -e for functions. returns on error.
819 # at the end of the function, disable with:
821 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
822 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"
828 local help="Usage: getdir [--help] PATH
829 Output the directory of PATH, or just PATH if it is a directory."
830 if [[ $1 == --help ]]; then
834 if [[ $# -ne 1 ]]; then
835 echo "getdir error: expected 1 argument, got $#"
842 dir
="$(dirname "$1")"
843 if [[ -d $dir ]]; then
846 echo "getdir error: directory does not exist"
852 git_empty_branch
() { # start an empty git branch. carefull, it deletes untracked files.
853 [[ $# == 1 ]] ||
{ echo 'need a branch name!'; return 1;}
855 root
=$
(gitroot
) ||
return 1 # function to set gitroot
857 git symbolic-ref HEAD refs
/heads
/$1
862 # shellcheck disable=SC2120
864 local help="Usage: gitroot [--help]
865 Print the full path to the root of the current git repo
867 Handles being within a .git directory, unlike git rev-parse --show-toplevel,
868 and works in older versions of git which did not have that."
869 if [[ $1 == --help ]]; then
874 p
=$
(git rev-parse
--git-dir) ||
{ echo "error: not in a git repo" ; return 1; }
875 [[ $p != /* ]] && p
=$PWD
881 # todo: patch emacs so it will look elsewhere. this is kinda sad:
882 # https://emacs.stackexchange.com/questions/4253/how-to-start-emacs-with-a-custom-user-emacs-directory
886 if [[ $EMACSDIR ]]; then
887 path-add
"$EMACSDIR/lib-src" "$EMACSDIR/src"
890 if [[ $DISPLAY ]]; then
894 if (( $# == 0 )); then
897 # duplicate -c, but oh well
898 if ! pgrep
-u $EUID emacsclient
; then
899 if (( $# == 0 )) && type -p gdb
&>/dev
/null
; then
905 if [[ $EMACSDIR ]]; then
906 # Alter the path here, otherwise the nfs mount gets triggered on the
907 # first path lookup when emacs is not being used.
908 PATH
="$EMACSDIR/lib-src:$EMACSDIR/src:$PATH" EHOME
=$HOME HOME
=$EMACSDIR m emacsclient
-a "" $args "$@"
911 # due to a bug, we cant debug from the start unless we get a new gdb
912 # https://sourceware.org/bugzilla/show_bug.cgi?id=24454
913 # m gdb -ex="set follow-fork-mode child" -ex=r -ex=quit --args emacs --daemon
914 m emacsclient
-a "" $args "$@"
916 cd /a
/opt
/emacs-$
(distro-name
)$
(distro-num
)
917 s gdb
-p $
(pgrep
-f 'emacs --daemon') -ex c
920 m emacsclient
-a "" $args "$@"
925 # force terminal version
931 # quit will prompt if the program crashes.
932 gdb
-ex=r
-ex=quit
--args emacs
"$@"; r
;
936 # kill the emacs daemon
941 grep -iIP --color=auto
"$@" ||
return $?
943 grr
() { # grep recursive
944 # Don't return 1 on nonmatch because this is meant to be
945 # interactive, not in a conditional.
946 if [[ ${#@} == 1 ]]; then
947 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -riIP --color=auto
"$@" . ||
[[ $?
== 1 ]]
949 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -riIP --color=auto
"$@" ||
[[ $?
== 1 ]]
957 hr
() { # horizontal row. used to break up output
958 printf "$(tput setaf 5 2>/dev/null ||:)â–ˆ$(tput sgr0 2>/dev/null||:)%.0s" $
(eval echo "{1..${COLUMNS:-60}}")
963 local col input_len
=0
965 input_len
=$
((input_len
+ 1 + ${#arg}))
967 col=$
((60 - input_len
))
968 printf "\e[1;97;41m%s" "$*"
969 if (( col > 0 )); then
970 printf "\e[1;97;41m \e[0m%.0s" $
(eval echo "{1..${col}}")
974 hlm
() { hl
"$*"; "$@"; }
976 hrcat
() { local f
; for f
; do [[ -f $f ]] ||
continue; hr
; echo "$f"; cat "$f"; done }
979 # get latest hub and run it
980 # main command to use:
981 # hub pull-request --no-edit
982 # --no-edit means to use the first commit\'s message as the pull request message.
983 # If that fails, try doing
984 # hub pull-request --no-edit -b UPSTREAM_OWNER:branch
985 # where branch is usually master. it does the pr against your current branch.
987 # On first use, you input username/pass and it gets an oath token so you dont have to repeat
988 # it\'s at ~/.config/hub
990 local up uptar updir p
991 p
=/github
/hub
/releases
/
992 up
=https
://github.com
/$
(curl
-s https
://github.com
$p|
grep -o $p'download/[^/]*/hub-linux-amd64[^"]*' |
head -n1)
995 if [[ ! -e /a
/opt
/$updir ]]; then
996 rm -rf /a
/opt
/hub-linux-amd64
*
998 tar -C /a
/opt
-zxf /a
/opt
/$uptar
1001 if ! which hub
&>/dev
/null
; then
1002 sudo
/a
/opt
/$updir/install
1005 # save token across computers
1006 if [[ ! -L ~
/.config
/hub
]]; then
1007 if [[ -e ~
/.config
/hub
]]; then
1008 mv ~
/.config
/hub
/p
/c
/subdir_files
/.config
/
1010 if [[ -e /p
/c
/subdir_files
/.config
/hub
]]; then
1033 find -L .
-not \
( -name .svn
-prune -o -name .git
-prune \
1034 -o -name .hg
-prune -o -name .editor-backups
-prune \
1035 -o -name .undo-tree-history
-prune \
) -iname "*$**" 2>/dev
/null
1039 # insensitive find directory
1040 find -L .
-type d
-not \
( -name .svn
-prune -o -name .git
-prune \
1041 -o -name .hg
-prune -o -name .editor-backups
-prune \
1042 -o -name .undo-tree-history
-prune \
) -iname "*$**" 2>/dev
/null
1047 sudo iptables
-A INPUT
-s $1 -j DROP
1052 grep -Il "" "$@" &>/dev
/null
1056 journalctl
-n 10000 -f "$@"
1058 jr
() { journalctl
"$@" ; }
1059 jrf
() { journalctl
-f "$@" ; }
1061 journalctl
-u exim4 _SYSTEMD_INVOCATION_ID
=$
(systemctl show
-p InvocationID
--value $1)
1065 if [[ $PWD == /[iap
] ]]; then
1066 command ls -A --color=auto
-I lost
+found
"$@"
1068 command ls -A --color=auto
"$@"
1072 lcn
() { locate -i "*$**"; }
1074 lg
() { LC_COLLATE
=C.UTF-8 ll
--group-directories-first "$@"; }
1076 lt
() { ll
-tr "$@"; }
1078 lld
() { ll
-d "$@"; }
1080 ccomp
ls l lg lt lld ll
1086 for dirs in false true
; do
1088 if [[ -d $f ]]; then
1090 # reverse the order to rename the nested dirs first.
1091 # note: 0 element is the dir itself
1092 for ((i
=${#all[@]}-1; i
>=1; i--
)); do
1094 if $dirs && [[ -d $a ]]; then
1095 # e dirs low "$a" # debug
1097 elif ! $dirs && [[ ! -d $a && -e $a ]]; then
1099 # e not dirs low "$a" # debug
1104 # just rename all the top level args on the second pass
1106 # e final dirs low "$f" # debug
1113 low
() { # make filenames lowercase, remove bad chars
1116 arg
="${arg%%+(/)}" # remove trailing slashes. assumes we have extglob on.
1118 if (( ${#dir} == ${#arg} )); then
1122 new
="${f,,}" # downcase
1123 new
="${new//[^a-zA-Z0-9._-]/_}" # sub bad chars
1124 new
="${new#"${new%%[[:alnum:]]*}"}" # remove leading/trailing non-alnum
1125 new
="${new%"${new##*[[:alnum:]]}"}"
1126 # remove bad underscores, like __ and _._
1127 new
=$
(echo $new |
sed -r 's/__+/_/g;s/_+([.-])|([.-])_+/\1/g')
1128 safe_rename
"$dir/$f" "$dir/$new" ||
return 1
1133 lower
() { # make first letter of filenames lowercase.
1136 if [[ ${x::1} == [A-Z
] ]]; then
1137 y
=$
(tr '[:upper:]' '[:lower:]' <<<"${x::1}")"${x:1}"
1138 safe_rename
"$x" "$y" ||
return 1
1144 k
() { # history search
1145 grep -iP --binary-files=text
"$@" ${HISTFILE:-~/.bash_history} |
tail -n 80 ||
[[ $?
== 1 ]];
1147 ks
() { # history search
1148 grep -P --binary-files=text
"$@" ${HISTFILE:-~/.bash_history} |
uniq ||
[[ $?
== 1 ]];
1154 # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
1155 make -qp |
awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
1168 mkt
() { # mkdir and touch file
1170 mkdir
-p "$(dirname "$path")"
1174 # shellcheck disable=SC2032
1175 mkdir
() { command mkdir
-p "$@"; }
1178 # https://github.com/HenriWahl/Nagstamon/issues/357
1179 if ! pgrep
-f /usr
/lib
/notification-daemon
/notification-daemon
>/dev
/null
; then
1180 /usr
/lib
/notification-daemon
/notification-daemon
&
1182 /usr
/bin
/nagstamon
&
1186 s nmtui-connect
"$@"
1190 # shellcheck disable=SC2024
1191 sudo
tee -a /var
/log
/exim
4/paniclog-archive
</var
/log
/exim
4/paniclog
; sudo truncate
-s0 /var
/log
/exim
4/paniclog
1194 p8
() { ping "$@" 8.8.8.8; }
1195 p6
() { ping6
"$@" 2001:4860:4860::8888; }
1197 pkx
() { # package extract
1198 local pkg cached tmp f
1201 # shellcheck disable=SC2012
1202 cached
=$
(ls -t /var
/cache
/apt
/archives
/$pkg* |
tail -n1 2>/dev
/null
) ||
:
1203 if [[ $cached ]]; then
1206 aptitude download
$pkg ||
return 1
1208 tmp
=(*); f
=${tmp[0]} # only 1 expected
1216 pid
=($
(pgrep
-f "$*"))
1219 # shellcheck disable=SC2128
1225 0) echo "no pid found" ;;
1234 help="Usage: psg [--help] GREP_ARGS
1235 grep ps and output in a nice format"
1236 if [[ $1 == --help ]]; then
1241 # final grep is because some commands tend to have a lot of trailing spaces
1242 y
=$
(echo "$x" |
grep -iP "$@" |
grep -o '.*[^ ]') ||
:
1244 echo "$x" |
head -n 1 ||
[[ $?
== 141 ]]
1249 pubip
() { curl
-4s https
://icanhazip.com
; }
1250 pubip6
() { curl
-6s https
://icanhazip.com
; }
1251 whatismyip
() { pubip
; }
1254 q
() { # start / launch a program in the backround and redir output to null
1258 # shellcheck disable=SC2120
1260 if [[ $HISTFILE ]]; then
1261 history -a # save history
1263 trap ERR
# this avoids a segfault
1265 # i had this redir, not sure why
1266 # exit "$@" 2>/dev/null
1269 # scp is insecure and deprecated.
1271 rsync
--inplace "$@"
1275 # available high ports are 1024-65535,
1276 # but lets skip things that are more likely to be in use
1279 print(secrets.SystemRandom().randrange(10002,65500))
1294 # rsync, root is required to keep permissions right.
1295 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
1296 # --no-times --delete
1297 # basically, make an exact copy, use checksums instead of file times to be more accurate
1298 rsync
-ahvic --delete "$@"
1301 # like rlu, but dont delete files on the target end which
1302 # do not exist on the original end.
1306 # rl without preserving modification time.
1307 rsync
-ahvic --delete --no-t "$@"
1309 rsu
() { # [OPTS] HOST PATH
1310 # eg. rlu -opts frodo /testpath
1311 # relative paths will expanded with readlink -f.
1312 opts
=("${@:1:$#-2}") # 1 to last -2
1313 path
="${*:$#}" # last
1314 host="${*:$#-1:1}" # last -1
1315 if [[ $path == .
* ]]; then
1316 path
=$
(readlink
-f $path)
1318 # rync here uses checksum instead of time so we dont mess with
1319 # unison relying on time as much. g is for group, same reason
1320 # to keep up with unison.
1321 m s rsync
-rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
1323 ccomp rsync rsd rsa rst rsu
1325 # find programs listening on a port
1328 # to figure out these args, i had to look at the man page from git version, as of 2022-04.
1329 s ss
-lpn state listening sport
= $port
1334 if [[ $
(systemctl is-active nscd ||
:) != inactive
]]; then
1339 hr
; s ss
-lpn sport
= 53
1340 if systemctl is-enabled dnsmasq
&>/dev
/null ||
[[ $
(systemctl is-active dnsmasq ||
:) != inactive
]]; then
1341 # this will fail is dnsmasq is failed
1342 hr
; m ser status dnsmasq |
cat ||
:
1344 hr
; echo $f:; ccat
$f
1345 hr
; m grr
'^ *(servers-file|server) *=|^ *no-resolv *$' /etc
/dnsmasq.conf
/etc
/dnsmasq.d
1346 f
=/etc
/dnsmasq-servers.conf
1347 hr
; echo $f:; ccat
$f
1350 echo /etc
/nsswitch.conf
:
1351 grep '^ *hosts:' /etc
/nsswitch.conf
1352 if systemctl is-enabled systemd-resolved
&>/dev
/null ||
[[ $
(systemctl is-active systemd-resolved ||
:) != inactive
]]; then
1353 hr
; m ser status systemd-resolved |
cat ||
:
1354 hr
; m systemd-resolve
--status |
cat
1362 if [[ $
(systemctl is-active nscd ||
:) != inactive
]]; then
1366 m sudo nscd
-i hosts
1368 if [[ $
(systemctl is-active dnsmasq ||
:) != inactive
]]; then
1369 m sudo systemctl restart dnsmasq
1371 if [[ $
(systemctl is-active systemd-resolved ||
:) != inactive
]]; then
1372 m sudo systemctl restart systemd-resolved
1374 if type -P resolvectl
&>/dev
/null
; then
1375 resolvectl flush-caches
1380 ssh fencepost
head -n 300 /gd
/gnuorg
/EventAndTravelInfo
/rms-current-trips.txt |
less
1384 command sudo
"$@" ||
return $?
1389 # I use a function because otherwise we cant use in a script,
1390 # cant assign to variable.
1392 # note: gksudo is recommended for X apps because it does not set the
1393 # home directory to the same, and thus apps writing to ~ fuck things up
1394 # with root owned files.
1396 if [[ $EUID != 0 ||
$1 == -* ]]; then
1397 # shellcheck disable=SC2034
1398 SUDOD
="$PWD" command sudo
-i "$@"
1404 sb
() { # sudo bash -c
1405 # use sb instead of s is for sudo redirections,
1406 # eg. sb 'echo "ok fine" > /etc/file'
1407 # shellcheck disable=SC2034
1409 sudo
-i bash
-c "$@"
1413 safe_rename
() { # warn and dont rename if file exists.
1414 # mv -n exists, but it\'s silent
1415 if [[ $# != 2 ]]; then
1416 echo safe_rename error
: $# args
, need
2 >2
1419 if [[ $1 != "$2" ]]; then # yes, we want to silently ignore this
1420 if [[ -e $2 ||
-L $2 ]]; then
1421 echo "Cannot rename $1 to $2 as it already exists."
1430 sudo
dd status
=none of
="$1"
1434 if type -p systemctl
&>/dev
/null
; then
1437 if (( $# >= 3 )); then
1438 echo iank
: ser expected
2 or
less arguments
1445 systemctl
-n 40 status
"$@"
1448 seru
() { systemctl
--user "$@"; }
1449 # like restart, but do nothing if its not already started
1452 if [[ $
(s systemctl
--no-pager show
-p ActiveState
$service ) == ActiveState
=active
]]; then
1453 systemctl restart
$service
1457 setini
() { # set a value in a .ini style file
1458 key
="$1" value
="$2" section
="$3" file="$4"
1459 if [[ -s $file ]]; then
1460 sed -ri -f - "$file" <<EOF
1461 # remove existing keys
1462 / *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d}
1464 /^\s*\[$section\]/a $key=$value
1465 # from section to eof, do nothing
1466 /^\s*\[$section\]/,\$b
1467 # on the last line, if we haven't found section yet, add section and key
1479 sgo
() { # service go
1481 ser restart
$service ||
return 1
1482 if type -p systemctl
&>/dev
/null
; then
1488 # ignore services that dont exist
1489 if systemctl
cat $service &>/dev
/null
; then
1491 ser disable
$service
1497 systemctl list-unit-files | rg
"$@"
1504 # note, if you do something like this
1506 # then disable the warning with:
1507 # shellcheck disable=SC2206 # globbing is intended
1509 # 2029: "unescaped, this expands on the client side.": yes, I know how ssh works
1510 # 2164: "Use 'cd ... || exit' or 'cd ... || return' in case cd fails.": i have automatic error handling
1511 # 2086: unquoted $var: Quoting every var I set is way too much quotes.
1512 # 2068: Double quote array expansions to avoid re-splitting elements: same as above.
1513 # 2033: command arg is a function name: too many false positives.
1516 # these ones I had disabled, but without a good written explanation, so enabling them temporarily
1517 # 2046: unquoted $(cmd)
1518 # 2119: Functions with optional args get bad warnings when none are passed.
1520 shellcheck
-W 999 -x -e 2029,2164,2086,2068,2033 "$@" ||
return $?
1524 # sl: ssh, but firsh rsync our bashrc and related files to a special
1525 # directory on the remote host if needed.
1527 # Some environment variables and files need to be setup for this to work
1528 # (mine are set at the beginning of this file)
1530 # SL_FILES_DIR: Environment variable. Path to folder which should at
1531 # least have a .bashrc file or symlink. This dir will be rsynced to ~ on
1532 # remote hosts (top level symlinks are resolved) unless the host already
1533 # has a $SL_FILES_DIR/.bashrc. In that case, we assume it is a host you
1534 # control and sync files to separately and already has the ~/.bashrc you
1535 # want. The remote bash will also take its .inputrc config from this
1536 # folder (default of not existing is fine). Mine looks like this:
1537 # https://iankelling.org/git/?p=distro-setup;a=tree;f=sl/.iank
1539 # SL_INFO_DIR: Environment variable. This folder stores info about what
1540 # we detected on the remote system and when we last synced. It will be created
1541 # if it does not exist. Sometimes you may want to forget about a
1542 # remote system, you can use sl --rsync, or the function for that slr
1545 # SL_TEST_CMD: Env var. Meant to be used to vary the files synced
1546 # depending on the remote host. Run this string on the remote host the
1547 # first time sl is run (or if we run slr). The result is passed to
1548 # SL_TEST_HOOK. For example,
1549 # export SL_TEST_CMD=". /etc/os-release ; echo \${VERSION//[^a-zA-Z0-9]/}"
1551 # SL_TEST_HOOK: Env var. It is run as $SL_TEST_HOOK. This can set
1552 # $SL_FILES_DIR to vary the files synced.
1554 # SL_RSYNC_ARGS: Env var. String of arguments passed to rsync. For
1555 # example to exclude files within a directory. Note, excluded
1556 # files wont be deleted on rsync, you can add --delete-excluded
1557 # to the rsync command if that is desired.
1559 # SL_SSH_ARGS: Env var. Default arguments passed to ssh.
1561 # For when ~/.bashrc is already customized on the remote server, you
1562 # might find it problematic that ~/.bashrc is sourced for ALL ssh
1563 # commands, even in scripts. This paragraph is all about that. bash
1564 # scripts dont source ~/.bashrc, but call ssh in scripts and you get
1565 # ~/.bashrc. You dont want this. .bashrc is meant for interactive shells
1566 # and if you customize it, probably has bugs from time to time. This is
1567 # bad. Here's how I fix it. I have a special condition to "return" in my
1568 # .bashrc for noninteractive ssh shells (copy that code). Then use this
1569 # function or similar that passes LC_USEBASHRC=t when sshing and I want
1570 # my bashrc. Also, I don't keep most of my bashrc in .bashrc, i source a
1571 # separate file because even if I return early on, the whole file gets
1572 # parsed which can fail if there is a syntax error.
1574 # Background on LC_USEBASHRC var (no need to read if you just want to
1575 # use this function): env variables sent across ssh are strictly
1576 # limited, but we get LC_* at least in debian based machines, so we
1577 # just make that * be something no normal program would use. Note, on
1578 # hosts that dont allow LC_* I start an inner shell with LC_USEBASHRC
1579 # set, and the inner shell also allows running a nondefault
1580 # .bashrc. This means the outer shell still ran the default .bashrc,
1581 # but that is the best we can do.
1583 local now args remote dorsync haveinfo tmpa sshinfo tmp tmp2
type info_sec force_rsync \
1584 sync_dirname testcmd extra_info testbool files_sec sl_test_cmd sl_test_hook
1585 declare -a args tmpa
1589 # ssh [-1246Antivivisectionist] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
1590 # [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address]
1591 # [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option]
1592 # [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname
1595 # ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
1596 # [-D [bind_address:]port] [-E log_file] [-e escape_char]
1597 # [-F configfile] [-I pkcs11] [-i identity_file]
1598 # [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
1599 # [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
1600 # [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
1603 if [[ $1 == --rsync ]]; then
1608 sl_test_cmd
=$SL_TEST_CMD
1609 sl_test_hook
=$SL_TEST_HOOK
1610 sl_rsync_args
=$SL_RSYNC_ARGS
1637 # note we dont support things like -4oOption
1638 -[46AaCfGgKkMNnqsTtVvXxYy
]*)
1641 -[bcDEeFIiJLlmOopQRSWw
]*)
1642 # -oOption etc is valid
1643 if (( ${#1} >= 3 )); then
1646 args
+=("$1" "$2"); shift 2
1655 if [[ ! $remote ]]; then
1656 echo $0: error hostname required
>&2
1661 if [[ ! $SL_INFO_DIR ]]; then
1662 echo error
: missing
'$SL_INFO_DIR' env var
>&2
1668 tmpa
=($SL_INFO_DIR/???????????
"$remote")
1670 if [[ -e $sshinfo ]]; then
1671 if $force_rsync; then
1678 tmp
=${sshinfo[0]##*/}
1681 extra_info
=$
(cat $sshinfo)
1683 # we test for string to know ssh succeeded
1684 testbool
="test -e $SL_FILES_DIR/.bashrc -a -L .bashrc -a -v LC_USEBASHRC"
1685 testcmd
="if $testbool; then printf y; else printf n; fi"
1686 if ! tmp
=$
(LC_USEBASHRC
=y
command ssh "${args[@]}" "$remote" "$testcmd; $sl_test_cmd"); then
1687 echo failed sl
test. doing plain
ssh -v
1688 command ssh -v "${args[@]}" "$remote"
1690 if [[ $tmp == y
* ]]; then
1696 extra_info
="${tmp:1}"
1698 if [[ $sl_test_hook ]]; then
1699 RSYNC_RSH
="ssh ${args[*]}" $sl_test_hook "$extra_info" "$remote"
1702 if $haveinfo && [[ $type == b
]]; then
1704 read files_sec _
< <(find -L $SL_FILES_DIR -printf "%T@ %p\n" |
sort -nr ||
[[ $?
== 141 ||
${PIPESTATUS[0]} == 32 ]] )
1705 files_sec
=${files_sec%%.*}
1706 if (( files_sec
> info_sec
)); then
1712 sync_dirname
=${SL_FILES_DIR##*/}
1714 if [[ ! $SL_FILES_DIR ]]; then
1715 echo error
: missing
'$SL_FILES_DIR' env var
>&2
1720 RSYNC_RSH
="ssh ${args[*]}" m rsync
-rptL --delete $sl_rsync_args $SL_FILES_DIR "$remote":
1722 if $dorsync ||
! $haveinfo; then
1723 sshinfo
=$SL_INFO_DIR/$EPOCHSECONDS$type"$remote"
1724 [[ -e $SL_INFO_DIR ]] || mkdir
-p $SL_INFO_DIR
1725 printf "%s\n" "$extra_info" >$sshinfo
1728 if [[ $type == b
]]; then
1729 if (( ${#@} )); then
1730 # Theres a couple ways to pass arguments, im not sure whats best,
1731 # but relying on bash 4.4+ escape quoting seems most reliable.
1732 command ssh "${args[@]}" "$remote" \
1733 LC_USEBASHRC
=t bash
-c '.\ '$sync_dirname'/.bashrc\;"\"\$@\""' bash
${@@Q}
1734 elif [[ ! -t 0 ]]; then
1735 # This case is when commands are being piped to ssh.
1736 # Normally, no bashrc gets sourced.
1737 # But, since we are doing all this, lets source it because we can.
1738 cat <(echo .
$sync_dirname/.bashrc
) - |
command ssh "${args[@]}" "$remote" LC_USEBASHRC
=t bash
1740 command ssh -t "${args[@]}" "$remote" LC_USEBASHRC
=t INPUTRC
=$sync_dirname/.inputrc bash
--rcfile $sync_dirname/.bashrc
1744 LC_USEBASHRC
=t
command ssh "${args[@]}" "$remote" ${@@Q}
1746 command ssh "${args[@]}" "$remote" LC_USEBASHRC
=t bash
1749 # this function inspired from https://github.com/Russell91/sshrc
1756 sl
-oControlMaster=no
-oControlPath=/ "$@"
1758 # kill off old shared socket then ssh
1760 m
ssh -O exit "$@" ||
[[ $?
== 255 ]]
1763 ccomp
ssh sl slr sss ssk
1766 if [[ $TERM == alacritty ||
$TERM == xterm-kitty
]]; then
1767 TERM
=xterm-256color LC_USEBASHRC
=t
command ssh "$@"
1769 LC_USEBASHRC
=t
command ssh "$@"
1775 # log with script. timing is $1.t and script is $1.s
1776 # -l to save to ~/typescripts/
1777 # -t to add a timestamp to the filenames
1778 local logdir do_stamp arg_base
1779 (( $# >= 1 )) ||
{ echo "arguments wrong"; return 1; }
1782 while getopts "lt" option
1785 l
) arg_base
=$logdir ;;
1786 t
) do_stamp
=true
;;
1789 shift $
((OPTIND
- 1))
1791 [[ -e $logdir ]] || mkdir
-p $logdir
1792 $do_stamp && arg_base
+=$
(date +%F.
%T
%z
)
1793 script -t $arg_base.s
2> $arg_base.t
1795 splay
() { # script replay
1796 #logRoot="$HOME/typescripts/"
1797 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
1798 scriptreplay
"$1.t" "$1.s"
1802 # sudo redo. be aware, this command may not work right on strange distros or earlier software
1803 if [[ $# == 0 ]]; then
1804 sudo
-E bash
-c -l "$(history -p '!!')"
1806 echo this
command redos last
history item. no argument is accepted
1811 # with -ll, less secure but faster.
1812 command srm
-ll "$@"
1817 ssh $1 /tmp
/${2##*/} $
(printf "%q\n" "${@:2}")
1829 tclock
() { # terminal clock
1834 # this goes to full width
1835 #len=${1:-$((COLUMNS -7))}
1838 if (( x
== len
)); then
1840 d
="$(date +%l:%_M) "
1843 d
=$
(date +%l
:%M
:%_S
)
1847 for ((i
=0; i
<x
; i
++)); do
1848 if (( i
% 6 )); then
1866 # test existence / exists
1869 [[ -e "$x" ||
-L "$x" ]] || ret
=1
1875 # normally, i would just execute these commands in the function.
1876 # however, DEBUG is not inherited, so we need to run it outside a function.
1877 # And we want to run set -x afterwards to avoid spam, so we cram everything
1878 # in here, and then it will run after this function is done.
1879 PROMPT_COMMAND
='trap DEBUG; unset PROMPT_COMMAND; PS1="\w \$ "'
1882 PROMPT_COMMAND
=prompt-command
1883 if [[ $TERM == *(screen
*|xterm
*|rxvt
*) ]]; then
1884 trap 'settitle "$BASH_COMMAND"' DEBUG
1888 tx
() { # toggle set -x, and the prompt so it doesnt spam
1889 if [[ $
- == *x
* ]]; then
1898 # show all processes in the network namespace $1.
1899 # blank entries appear to be subprocesses/threads
1903 sudo
find -L /proc
/[1-9]*/task
/*/ns
/net
-samefile /run
/netns
/$netns | cut
-d/ -f5 | \
1905 x
=$
(ps
-w --no-headers -p $l);
1906 if [[ $x ]]; then echo "$x"; else echo $l; fi;
1910 if ! s ip netns list |
grep -Fx nonet
&>/dev
/null
; then
1911 s ip netns add nonet
1913 sudo
-E env
/sbin
/ip netns
exec nonet sudo
-E -u iank
/bin
/bash
1916 m
() { printf "%s\n" "$*"; "$@"; }
1919 if type -p uprecords
&>/dev
/null
; then
1927 for x
in "$@"; do virsh destroy
"$x"; virsh undefine
"$x"; done
1935 sudo virsh dumpxml
$vm |
sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
1936 sed -r "s/listen='[^']+/listen='$ip/"> $t
1937 sudo virsh undefine
$vm
1938 sudo virsh define
$t
1943 vm-set-listen
$1 0.0.0.0
1948 vm-set-listen
$1 127.0.0.1
1952 # find input, copy to pattern space, when we find the first field, print the copy in different order without newlines.
1953 # instead of using labels, we could just match a line and group, eg: /signal:/,{s/signal:(.*)/\1/h}
1954 sudo iw dev wls1 scan |
sed -rn "
1955 s/^\Wcapability: (.*)/\1/;Ta;h;b
1956 :a;s/^\Wsignal: -([^.]+).*/\1/;Tb;H;b
1957 # padded to min width of 20
1958 :b;s/\WSSID: (.*)/\1 /;T;s/^(.{20}(.*[^ ])?) */\1/;H;g;s/(.*)\n(.*)\n(.*)/\2 \3 \1/gp;b
1965 if $use_color && type -p tput &>/dev
/null
; then
1966 term_bold
="$(tput bold)"
1967 term_red
="$(tput setaf 1)"
1968 term_green
="$(tput setaf 2)"
1969 term_yellow
="$(tput setaf 3)"
1970 term_purple
="$(tput setaf 5)"
1971 term_nocolor
="$(tput sgr0)" # no font attributes
1973 # unused so far. commented for shellcheck
1974 # term_underl="$(tput smul)"
1975 # term_blue="$(tput setaf 4)"
1976 # term_cyan="$(tput setaf 6)"
1978 # Try to keep environment pollution down, EPA loves us.
1979 unset safe_term match_lhs use_color
1984 if [[ $
- == *i
* ]]; then
1989 if [[ $EUID == 1000 ]]; then
1996 # this needs to come before next ps1 stuff
1997 # this stuff needs bash 4, feb 2009,
1998 # old enough to no longer condition on $BASH_VERSION anymore
2002 if [[ $
- == *i
* ]] && [[ ! $LC_INSIDE_EMACS ]]; then
2004 bind -m vi-command B
:shell-backward-word
2005 bind -m vi-command W
:shell-forward-word
2008 if [[ $SSH_CLIENT ||
$SUDO_USER ]]; then
2009 unset PROMPT_DIRTRIM
2013 # emacs terminal has problems if this runs slowly,
2014 # so I've thrown a bunch of things at the wall to speed it up.
2016 local return=$?
# this MUST COME FIRST
2017 local ps_char ps_color
2020 if [[ $HISTFILE ]]; then
2021 history -a # save history
2025 # shellcheck disable=SC1303
2026 if [[ $jr_pid ]]; then
2027 if [[ -e /proc
/$jr_pid ]]; then
2034 0) ps_color
="$term_purple"
2037 *) ps_color
="$term_green"
2038 ps_char
="$return \\$"
2041 if [[ ! -O .
]]; then # not owner
2042 if [[ -w .
]]; then # writable
2043 ps_color
="$term_bold$term_red"
2045 ps_color
="$term_bold$term_green"
2049 # faster than sourceing the file im guessing
2050 if [[ -e /dev
/shm
/iank-status
&& ! -e /tmp
/quiet-status
]]; then
2051 eval $
(< /dev
/shm
/iank-status
)
2053 if [[ $MAIL_HOST && $MAIL_HOST != "$HOSTNAME" ]]; then
2054 ps_char
="@ $ps_char"
2056 # We could test if sudo is active with sudo -nv
2057 # but then we get an email and log of lots of failed sudo commands.
2058 # We could turn those off, but seems better not to.
2059 if [[ $EUID != 0 ]] && [[ $DID_SUDO ]]; then
2060 psudo
="\[$term_bold$term_red\]s\[$term_nocolor\] "
2062 if [[ ! $HISTFILE ]]; then
2063 ps_char
="NOHIST $ps_char"
2065 PS1
="${PS1%"${PS1#*[wW]}"} $psudo\[$ps_color\]$ps_char\[$term_nocolor\] "
2067 # set titlebar. instead, using more advanced
2069 #echo -ne "$_title_escape $HOSTNAME ${PWD/#$HOME/~} \007"
2071 PROMPT_COMMAND
=prompt-command
2073 if [[ $TERM == screen
* ]]; then
2074 _title_escape
="\033]..2;"
2076 # somme sites recommend this, i dunno what the diff is.
2077 #_title_escape="\033]30;"
2078 _title_escape
="\033]0;"
2082 # this makes it so we show the current command if
2083 # one is running, otherwise, show nothing
2085 if [[ $1 == prompt-command
]]; then
2088 if (( ${#BASH_ARGC[@]} == 1 && BASH_SUBSHELL
== 0 )); then
2089 echo -ne "$_title_escape ${PWD/#$HOME/~} "
2095 # note, this wont work:
2096 # x=$(mktemp); cp a $x
2097 # I havnt figured out why, bigger fish to fry.
2100 # condition from the screen man page i think.
2101 # note: duplicated in tx()
2102 if [[ $TERM == *(screen
*|xterm
*|rxvt
*) ]]; then
2103 trap 'settitle "$BASH_COMMAND"' DEBUG
2110 # * stuff that makes sense to be at the end
2116 # shellcheck disable=SC1090
2117 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
2119 # I had this idea to start a bash shell which would run an initial
2120 # command passed through this env variable, then continue on
2121 # interactively. But the use case I had in mind went away.
2123 # if [[ $MY_INIT_CMD ]]; then
2124 # "${MY_INIT_CMD[@]}"
2128 # ensure no bad programs appending to this file will have an affect