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 source /a
/bin
/errhandle
/err
14 # remove all aliases. aliases provided by the system tend to get in the way,
15 # for example, error happens if I try to define a function the same name as an alias
18 # remove gnome keyring warning messages
19 # there is probably a more proper way, but I didnt find any easily on google
20 # now using xfce+xmonad instead of vanilla xmonad, so disabling this
21 #unset GNOME_KEYRING_CONTROL
23 # use extra globing features.
25 # include .files when globbing, but ignore files name . and ..
26 # setting this also sets dotglob.
27 export GLOBIGNORE
="*/.:*/.."
29 # broken with bash_completion package. Saw a bug for this once. dont anymore.
30 # still broken in wheezy
31 # still buggered in latest stable from the web, version 2.1
32 # perhaps its fixed in newer git version, which fails to make for me
33 # this note is from 6-2014.
34 # still broken in flidas.
37 # make tab on an empty line do nothing
38 shopt -s no_empty_cmd_completion
40 # fix spelling errors for cd, only in interactive shell
42 # append history instead of overwritting it
44 # for compatibility, per gentoo/debian bashrc
46 # attempt to save multiline single commands as single history entries.
53 if [[ $RLC_INSIDE_EMACS ]]; then
54 # EMACS is used by bash on startup, but we dont need it anymore.
55 # plus I hit a bug in a makefile which inherited it
57 export RLC_INSIDE_EMACS
60 # scp completion does not work, but this doesnt fix it. todo, figure this out
61 #complete -r scp &> /dev/null
62 # todo, remote file completion fails, figure out how to turn it off
63 export NODE_DISABLE_COLORS
=1
64 # This gets rid of ugly terminal escape chars in node repl
65 # sometime, Id like to have completion working in emacs shell for node
66 # the offending chars can be found in lib/readline.js,
67 # things that do like:
68 # stream.write('\x1b[' + (x + 1) + 'G');
69 # We can remove them and keep readline, for example by doing this
72 # var readline = require('readline');
73 # readline.cursorTo = function(a,b,c) {};
74 # readline.clearScreenDown = function(a) {};
75 # const repl = require('repl');
76 # var replServer = repl.start('');
78 # no prompt, or else readline complete seems to be confused, based
79 # on our column being different? node probably needs to send
80 # different kind of escape sequence that is not ugly. Anyways,
81 # completion doesnt work yet even with the ugly prompt, so whatever
83 export NODE_NO_READLINE
=1
87 # emacs has a different default search path than the info command. This
88 # adds the info defaults to emacs, but not the reverse, because I dun
89 # care much about the cli. The search path is only on the cli if you run
90 # "info xxx", or in emacs if you run '(info xxx)', so not that
91 # important, but might as well fix it.
93 # info info says this path is what was compiled, and its not documented
94 # anywhere. Through source grepping, i found it in filesys.h of the info
95 # source in trisquel flidas.
97 # Traling : means for emacs to add its own stuff on to the end.
99 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
:.
:
101 if [[ $
- == *i
* ]]; then
102 # for readline-complete.el
103 if [[ $RLC_INSIDE_EMACS ]]; then
104 # all for readline-complete.el
106 bind 'set horizontal-scroll-mode on'
107 bind 'set print-completions-horizontally on'
108 bind '"\C-i": self-insert'
111 if [[ $KONSOLE_PROFILE_NAME ]]; then
115 # todo: not sure this works in sakura
117 #bind "\C-w": kill-region
118 # sakura == xterm-256color
120 if [[ $TERM == xterm
* ]]; then
121 # control + arrow keys. for other terminals, see http://unix.stackexchange.com/questions/10806/how-to-change-previous-next-word-shortcut-in-bash
122 bind '"\e[1;5C": shell-forward-word' 2>/dev
/null
123 bind '"\e[1;5D": shell-backward-word' 2>/dev
/null
125 # make ctrl-backspace work. for konsole, i fixed it through
126 # /home/iank/.local/share/konsole/default.keytab
128 bind '"\eOc": shell-forward-word'
129 bind '"\eOd": shell-backward-word'
131 # i cant remember why i did this, probably to free up some keys to bind
132 # to other things in bash.
133 # other than C-c and C-z, the rest defined by stty -a are, at least in
134 # gnome-terminal, overridden by bash, or disabled by the system
135 stty lnext undef stop undef start undef
141 # history number. History expansion is good.
143 # history file size limit, set to unlimited.
144 # this needs to be different from the default because
145 # default HISTFILESIZE is 500 and could clobber our history
147 # max commands 1 session can append/read from history
149 # the time format display when doing the history command
150 # also, setting this makes the history file record time
151 # of each command as seconds from the epoch
152 HISTTIMEFORMAT
="%Y-%m-%d %I:%M %p "
153 # consecutive duplicate lines dont go in history
154 HISTCONTROL
=ignoredups
155 # works in addition to HISTCONTROL to do more flexible things
156 # it could also do the same things as HISTCONTROL and thus replace it,
157 # but meh. dunno why, but just " *" does glob expansion, so use [ ] to avoid it.
158 HISTIGNORE
='pass *:[ ]*:otp *:oathtool *'
160 export BC_LINE_LENGTH
=0
162 export PROFILE_TASKS_TASK_OUTPUT_LIMIT
=100
164 # note, if I use a machine I dont want files readable by all users, set
165 # umask 077 # If fewer than 4 digits are entered, leading zeros are assumed
167 # i for insensitive. the rest from
168 # X means dont remove the current screenworth of output upon exit
169 # R means to show colors n things
171 export SYSTEMD_LESS
=$LESS
178 # if someone exported $SOE (stop on error), catch errors.
180 # Note, on debian this results in the following warning when in ssh,
181 # hich I haven't figured out how to fix. It doesn't happen if we source
182 # after the shell has started
184 # bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
185 # bash: warning: cannot start debugger; debugging mode disabled
187 if [[ -e /a
/bin
/errhandle
/err
]]; then
188 source /a
/bin
/errhandle
/err
192 # based on readme.debian. dunno if this will break on other distros.
193 if [[ -s /usr
/share
/wcd
/wcd-include.sh
]]; then
194 source /usr
/share
/wcd
/wcd-include.sh
197 if [[ -s /a
/bin
/small-misc-bash
/ll-function
]]; then
198 # shellcheck source=/a/bin/small-misc-bash/ll-function
199 source /a
/bin
/small-misc-bash
/ll-function
200 elif [[ -s ~
/.iank
/ll-function
]]; then
201 source ~
/.iank
/ll-function
209 ....
() { c ..
/..
/..
; }
210 .....
() { c ..
/..
/..
/..
; }
211 ......
() { c ..
/..
/..
/..
/..
; }
213 # file cut copy and paste, like the text buffers :)
214 # I havnt tested these.
215 _fbufferinit
() { # internal use
216 ! [[ $my_f_tempdir ]] && my_f_tempdir
=$
(mktemp
-d)
217 rm -rf "${my_f_tempdir:?}"/*
221 cp "$@" "$my_f_tempdir"/
225 mv "$@" "$my_f_tempdir"/
227 fpst
() { # file paste
228 [[ $2 ]] && { echo too many arguments
; return 1; }
230 cp "$my_f_tempdir"/* "$target"
236 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")
238 echo "khfix: ssh failed"
241 if [[ $port != 22 ]]; then
242 ip_entry
="[$ip]:$port"
243 host_entry
="[$host]:$port"
248 ssh-keygen
-R "$host_entry" -f $
(readlink
-f ~
/.ssh
/known_hosts
)
249 echo "khfix: removing key for $ip_entry"
250 ssh-keygen
-R "$ip_entry" -f $
(readlink
-f ~
/.ssh
/known_hosts
)
252 khfix
() { # known hosts fix
253 _khfix_common
"$@" ||
return 1
263 x
=$
(readlink
-nf "${1:-$PWD}")
264 # yes, its kinda dumb that xclip/xsel cant do this in one invocation
265 echo -n "$x" | xclip
-selection clipboard
269 ack
() { ack-grep
"$@"; }
278 if type -p wcd
&>/dev
/null
; then
279 if [[ $RLC_INSIDE_EMACS ]]; then
280 c
() { wcd
-c -z 50 -o "$@"; }
282 # lets see what the fancy terminal does from time to time
283 c
() { wcd
-c -z 50 "$@"; }
289 c4
() { c
/var
/log
/exim4
; }
291 caa
() { git commit
--amend --no-edit -a; }
294 # shellcheck disable=SC2033
295 find -L $1 -type f
-not \
( -name .svn
-prune -o -name .git
-prune \
296 -o -name .hg
-prune -o -name .editor-backups
-prune \
297 -o -name .undo-tree-history
-prune \
) \
298 -exec bash
-lc 'hr; echo "$1"; hr; cat "$1"' _
{} \
; 2>/dev
/null
302 calc
() { echo "scale=3; $*" |
bc -l; }
303 # no having to type quotes, but also no command history:
307 echo "scale=3; $x" |
bc -l
315 ccat
() { # config cat. see a config without extra lines.
316 grep '^\s*[^;[:space:]#]' "$@"
321 # join options which are continued to multiples lines onto one line
323 while IFS
= read -r line
; do
324 # remove leading spaces/tabs. assumes extglob
325 if [[ $line == "[ ]*" ]]; then
331 elif [[ $line == *=* ]]; then
332 echo "$pastline" >> "$2"
335 pastline
="$pastline $line"
337 done < <(grep -vE '^([ \t]*#|^[ \t]*$)' "$1")
338 echo "$pastline" >> "$2"
343 # setup for format of postfix, eg:
346 local pastline unified f1 f2
350 _cdiff-prep
"$1" "$f1"
351 _cdiff-prep
"$2" "$f2"
352 cat "$f1" "$f2" |
grep -Po '^[^=]+=' |
sort |
uniq > "$unified"
353 while IFS
= read -r line
; do
354 # the default bright red / blue doesnt work in emacs shell
355 dwdiff
-cblue,red
-A best
-d " ," <(grep "^$line" "$f1" ||
echo ) <(grep "^$line" "$f2" ||
echo ) | colordiff
363 inotifywait
-m "$dir" -e create
-e moved_to |
364 # shellcheck disable=SC2030
365 while read -r filedir _
file; do
368 calc $
((SECONDS
- start
)) / 60
374 # shellcheck disable=SC2032
376 # makes it so chown -R symlink affects the symlink and its target.
377 if [[ $1 == -R ]]; then
379 command chown
-h "$@"
380 command chown
-R "$@"
391 # choose recent directory. cl = cd list
396 complete
-A stopped
-P '"%' -S '"' d
400 diff --strip-trailing-cr -w "$@" # diff content
407 safe_rename
"$x" "$y"
412 command dig +nostats
+nocmd
"$@"
414 # Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed.
418 dig +nordflag
"$@" |
sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' |
while read -r l
; do
419 if [[ $l == [^\
;]* ]]; then
423 printf "%s" "$sec" |
sort
430 # compare digs to the 2 servers
431 # usage: digdiff @server1 @server2 DIG_ARGS
432 # note: only the soa master nameserver will respond with
433 # ra "recursive answer" flag. That difference is meaningless afaik.
440 digsort
$s1 "$@" |
tee /tmp
/digdiff
441 diff -u /tmp
/digdiff
<(digsort
$s2 "$@")
445 date "+%A, %B %d, %r" "$@"
448 dus
() { # du, sorted, default arg of
449 du
-sh ${@:-*} |
sort -h
462 printf "%qEOL\n" "${arg}"
463 printf "%s" "${arg}" |
& hexdump -C
466 # echo vars. print var including escapes, etc
472 printf "%qEOL\n" "${!arg}"
473 printf "%s" "${!arg}" |
& hexdump -C
479 [[ ${#@} == 2 ]] ||
{ echo "error: ediff requires 2 arguments"; return 1; }
480 emacs
--eval "(ediff-files \"$1\" \"$2\")"
485 tail -F /var
/log
/exim
4/mainlog
-n 200
488 less /var
/log
/exim
4/mainlog
491 exiqgrep
-i |
while read i
; do
492 exim
-Mvh $i; hr
; exim
-Mvb $i; hr
;
493 exigrep
$i /var
/log
/exim
4/mainlog
; hr
498 # shellcheck disable=SC2032
505 # find array. make an array of file names found by find into $x
506 # argument: find arguments
507 # return: find results in an array $x
508 while read -rd ''; do
510 done < <(find "$@" -print0);
513 faf
() { # find all files. use -L to follow symlinks
514 find $@
-not \
( -name .svn
-prune -o -name .git
-prune \
515 -o -name .hg
-prune -o -name .editor-backups
-prune \
516 -o -name .undo-tree-history
-prune \
) -type f
2>/dev
/null
522 s mailq |gr frozen|
awk '{print $3}' |
while read -r id
; do
528 echo -e '\n\n##############################\n'
529 done |
tee -a /tmp
/frozen
533 while read -r line
; do
534 printf '%s\n' "$line"
535 ids
+=($
(printf '%s\n' "$line" |gr frozen|
awk '{print $3}'))
537 echo "sleeping for 2 in case you change your mind"
539 s exim
-Mrm "${ids[@]}"
543 # like -e for functions. returns on error.
544 # at the end of the function, disable with:
546 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
547 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"
553 local help="Usage: getdir [--help] PATH
554 Output the directory of PATH, or just PATH if it is a directory."
555 if [[ $1 == --help ]]; then
559 if [[ $# -ne 1 ]]; then
560 echo "getdir error: expected 1 argument, got $#"
567 dir
="$(dirname "$1")"
568 if [[ -d $dir ]]; then
571 echo "getdir error: directory does not exist"
577 git_empty_branch
() { # start an empty git branch. carefull, it deletes untracked files.
578 [[ $# == 1 ]] ||
{ echo 'need a branch name!'; return 1;}
580 root
=$
(gitroot
) ||
return 1 # function to set gitroot
582 git symbolic-ref HEAD refs
/heads
/$1
587 # shellcheck disable=SC2120
589 local help="Usage: gitroot [--help]
590 Print the full path to the root of the current git repo
592 Handles being within a .git directory, unlike git rev-parse --show-toplevel,
593 and works in older versions of git which did not have that."
594 if [[ $1 == --help ]]; then
599 p
=$
(git rev-parse
--git-dir) ||
{ echo "error: not in a git repo" ; return 1; }
600 [[ $p != /* ]] && p
=$PWD
605 # i got an error, gh not found when doing a pull request, it seems like it wants itself in it\'s path.
606 local _oldpath
="$PATH"
607 PATH
="$PATH:$HOME/node_modules/.bin"
613 # quit will prompt if the program crashes.
614 gdb
-ex=r
-ex=quit
--args emacs
"$@"; r
;
618 # kill the emacs daemon
623 grep -iIP --color=auto
"$@"
626 grr
() { # grep recursive
627 if [[ ${#@} == 1 ]]; then
628 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto
"$@" .
630 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto
"$@"
634 command rg
-i -M 200 "$@"
637 hr
() { # horizontal row. used to break up output
639 printf "$(tput setaf 5)â–ˆ$(tput sgr0)%.0s" $
(eval echo {1..
${COLUMNS:-60}})
643 hrcat
() { local f
; for f
; do [[ -f $f ]] ||
continue; hr
; echo "$f"; cat "$f"; done }
645 # get latest hub and run it
646 # main command to use:
647 # hub pull-request --no-edit
648 # --no-edit means to use the first commit\'s message as the pull request message.
649 # Also, you need to use a feature branch, not master in your fork.
650 # On first use, you input username/pass and it gets an oath token so you dont have to repeat
651 # it\'s at ~/.config/hub
653 local up uptar updir p
654 p
=/github
/hub
/releases
/
655 up
=https
://github.com
/$
(curl
-s https
://github.com
$p|
grep -o $p'download/[^/]*/hub-linux-amd64[^"]*' |
head -n1)
658 if [[ ! -e /a
/opt
/$updir ]]; then
659 rm -rf /a
/opt
/hub-linux-amd64
*
661 tar -C /a
/opt
-zxf /a
/opt
/$uptar
663 s
/a
/opt
/$updir/install
666 # save token across computers
667 if [[ ! -L ~
/.config
/hub
]]; then
668 if [[ -e ~
/.config
/hub
]]; then
669 mv ~
/.config
/hub
/p
/c
/subdir_files
/.config
/
671 if [[ -e /p
/c
/subdir_files
/.config
/hub
]]; then
679 # modified from ~/local/bin/git-completion.bash
680 # other completion commands are mostly taken from bash_completion package
681 complete
-o bashdefault
-o default
-o nospace
-F _git i
2>/dev
/null \
682 || complete
-o default
-o nospace
-F _git i
684 if ! type service
&>/dev
/null
; then
686 echo actually running
: systemctl
$2 $1
702 find -L .
-not \
( -name .svn
-prune -o -name .git
-prune \
703 -o -name .hg
-prune -o -name .editor-backups
-prune \
704 -o -name .undo-tree-history
-prune \
) -iname "*$**" 2>/dev
/null
708 s iptables
-A INPUT
-s $1 -j DROP
713 grep -Il "" "$@" &>/dev
/null
717 journalctl
-n 10000 -f "$@"
719 jr
() { journalctl
"$@" ; }
720 jrf
() { journalctl
-f "$@" ; }
723 if [[ $PWD == /[iap
] ]]; then
724 command ls -A --color=auto
-I lost
+found
"$@"
726 command ls -A --color=auto
"$@"
731 lcn
() { locate -i "*$**"; }
733 lg
() { LC_COLLATE
=C.UTF-8 ll
--group-directories-first; }
735 lt
() { ll
-tr "$@"; }
737 lld
() { ll
-d "$@"; }
739 low
() { # make filenames lowercase, remove bad chars
742 new
="${f,,}" # downcase
743 new
="${new//[^[:alnum:]._-]/_}" # sub bad chars
744 new
="${new#"${new%%[[:alnum:]]*}"}" # remove leading/trailing non-alnum
745 new
="${new%"${new##*[[:alnum:]]}"}"
746 # remove bad underscores, like __ and _._
747 new
=$
(echo $new |
sed -r 's/__+/_/g;s/_+([.-])|([.-])_+/\1/g')
748 safe_rename
"$f" "$new" ||
return 1
753 lower
() { # make first letter of filenames lowercase.
756 if [[ ${x::1} == [A-Z
] ]]; then
757 y
=$
(tr '[:upper:]' '[:lower:]' <<<"${x::1}")"${x:1}"
758 safe_rename
"$x" "$y" ||
return 1
764 k
() { # history search
765 grep -P --binary-files=text
"$@" ${HISTFILE:-~/.bash_history} |
tail -n 80;
768 ks
() { # history search
769 grep -P --binary-files=text
"$@" ${HISTFILE:-~/.bash_history} |
uniq;
774 # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
775 make -qp |
awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
787 mkt
() { # mkdir and touch file
789 mkdir
-p "$(dirname "$path")"
793 # shellcheck disable=SC2032
794 mkdir
() { command mkdir
-p "$@"; }
797 # shellcheck disable=SC2024
798 sudo
tee -a /var
/log
/exim
4/paniclog-archive
</var
/log
/exim
4/paniclog
; sudo truncate
-s0 /var
/log
/exim
4/paniclog
801 p8
() { ping 8.8.8.8; }
802 p6
() { ping6
2001:4860:4860::8888; }
804 pkx
() { # package extract
805 local pkg cached tmp f
808 # shellcheck disable=SC2012
809 cached
=$
(ls -t /var
/cache
/apt
/archives
/$pkg* |
tail -n1 2>/dev
/null
)
810 if [[ $cached ]]; then
813 aptitude download
$pkg ||
return 1
815 tmp
=(*); f
=${tmp[0]} # only 1 expected
823 pid
=($
(pgrep
-f "$*"))
826 # shellcheck disable=SC2128
832 0) echo "no pid found" ;;
839 pubip
() { curl
-4s https
://icanhazip.com
; }
840 pubip6
() { curl
-6s https
://icanhazip.com
; }
841 whatismyip
() { pubip
; }
846 # -t = print pronunciation
848 for (( i
=0; i
<10; i
++ )); do
849 shuf
-n3 /usr
/share
/hunspell
/en_US.dic |
sed 's,/.*,,' |
paste -sd .
-
855 # -M CLN = use Caps, Lowercase, Numbers
857 # -a 1 = use random instead of pronounceable algorithm
858 apg
-m 50 -x 70 -n 1 -a 1 -M CLN
862 q
() { # start / launch a program in the backround and redir output to null
866 # shellcheck disable=SC2120
868 history -a # save history
870 # i had this redir, not sure why
871 # exit "$@" 2>/dev/null
875 # rsync, root is required to keep permissions right.
876 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
877 # --no-times --delete
878 # basically, make an exact copy, use checksums instead of file times to be more accurate
879 rsync
-ahvic --delete "$@"
882 # like rlu, but dont delete files on the target end which
883 # do not exist on the original end.
886 complete
-F _rsync
-o nospace rld rl rlt
889 # rl without preserving modification time.
890 rsync
-ahvic --delete --no-t "$@"
893 rlu
() { # [OPTS] HOST PATH
894 # eg. rlu -opts frodo /testpath
895 # relative paths will expanded with readlink -f.
896 opts
=("${@:1:$#-2}") # 1 to last -2
897 path
="${*:$#}" # last
898 host="${*:$#-1:1}" # last -1
899 if [[ $path == .
* ]]; then
900 path
=$
(readlink
-f $path)
902 # rync here uses checksum instead of time so we dont mess with
903 # unison relying on time as much. g is for group, same reason
904 # to keep up with unison.
905 s rsync
-rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
909 ssh fencepost
head -n 300 /gd
/gnuorg
/EventAndTravelInfo
/rms-current-trips.txt |
less
914 # I use a function because otherwise we cant use in a script,
915 # cant assign to variable.
917 # note: gksudo is recommended for X apps because it does not set the
918 # home directory to the same, and thus apps writing to ~ fuck things up
919 # with root owned files.
921 if [[ $EUID != 0 ||
$1 == -* ]]; then
922 SUDOD
="$PWD" sudo
-i "$@"
928 safe_rename
() { # warn and dont rename if file exists.
929 # mv -n exists, but it\'s silent
930 if [[ $# != 2 ]]; then
931 echo safe_rename error
: $# args
, need
2 >2
934 if [[ $1 != "$2" ]]; then # yes, we want to silently ignore this
935 if [[ -e $2 ||
-L $2 ]]; then
936 echo "Cannot rename $1 to $2 as it already exists."
944 sb
() { # sudo bash -c
945 # use sb instead of s is for sudo redirections,
946 # eg. sb 'echo "ok fine" > /etc/file'
950 complete
-F _root_command s sb
954 local s
; [[ $EUID != 0 ]] && s
=s
955 if type -p systemctl
&>/dev
/null
; then
961 # like restart, but do nothing if its not already started
964 if [[ $
(s systemctl
--no-pager show
-p ActiveState
$service ) == ActiveState
=active
]]; then
965 systemctl restart
$service
969 setini
() { # set a value in a .ini style file
970 key
="$1" value
="$2" section
="$3" file="$4"
971 if [[ -s $file ]]; then
972 sed -ri -f - "$file" <<EOF
973 # remove existing keys
974 / *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d}
976 /^\s*\[$section\]/a $key=$value
977 # from section to eof, do nothing
978 /^\s*\[$section\]/,\$b
979 # on the last line, if we haven't found section yet, add section and key
993 ser restart
$service ||
return 1
994 if type -p systemctl
&>/dev
/null
; then
1000 systemctl list-unit-files | rg
"$@"
1005 # 2086: unquoted $var
1006 # 2046: unquoted $(cmd)
1007 # 2068: Double quote array expansions to avoid re-splitting elements.
1008 # 2119: Functions with optional args get bad warnings when none are passed.
1009 # 2033: too many false positives for thing that will never work, passing shell function to find.
1010 # i had -x as an arg, but debian testing(stretch) doesn\'t support it
1011 shellcheck
-x -e 2086,2046,2068,2119,2033 "$@"
1012 # had this before. not sure what it is 2119
1017 # log with script. timing is $1.t and script is $1.s
1018 # -l to save to ~/typescripts/
1019 # -t to add a timestamp to the filenames
1020 local logdir do_stamp arg_base
1021 (( $# >= 1 )) ||
{ echo "arguments wrong"; return 1; }
1024 while getopts "lt" option
1027 l
) arg_base
=$logdir ;;
1028 t
) do_stamp
=true
;;
1031 shift $
((OPTIND
- 1))
1033 [[ -e $logdir ]] || mkdir
-p $logdir
1034 $do_stamp && arg_base
+=$
(date +%F.
%T
%z
)
1035 script -t $arg_base.s
2> $arg_base.t
1037 splay
() { # script replay
1038 #logRoot="$HOME/typescripts/"
1039 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
1040 scriptreplay
"$1.t" "$1.s"
1044 # sudo redo. be aware, this command may not work right on strange distros or earlier software
1045 if [[ $# == 0 ]]; then
1046 sudo
-E bash
-c -l "$(history -p '!!')"
1048 echo this
command redos last
history item. no argument is accepted
1053 # with -ll, less secure but faster.
1054 command srm
-ll "$@"
1059 ssh $1 /tmp
/${2##*/} $
(printf "%q\n" "${@:2}")
1071 tclock
() { # terminal clock
1076 # this goes to full width
1077 #len=${1:-$((COLUMNS -7))}
1080 if (( x
== len
)); then
1082 d
="$(date +%l:%_M) "
1085 d
=$
(date +%l
:%M
:%_S
)
1089 for ((i
=0; i
<x
; i
++)); do
1090 if (( i
% 6 )); then
1108 # test existence / exists
1111 [[ -e "$x" ||
-L "$x" ]] || ret
=1
1117 tx
() { # toggle set -x, and the prompt so it doesnt spam
1118 if [[ $
- == *x
* ]]; then
1120 PROMPT_COMMAND
=prompt-command
1121 # disabled due to issue on stretch, running ll we get error. something
1122 # about the DEBUG trap is broken
1123 # if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
1124 # trap 'settitle "$BASH_COMMAND"' DEBUG
1127 # normally, i would just execute these commands in the function.
1128 # however, DEBUG is not inherited, so we need to run it outside a function.
1129 # And we want to run set -x afterwards to avoid spam, so we cram everything
1130 # in here, and then it will run after this function is done.
1131 #PROMPT_COMMAND='trap DEBUG; unset PROMPT_COMMAND; PS1="\w \$ "; set -x'
1133 unset PROMPT_COMMAND
1140 # show all processes in the network namespace $1.
1141 # blank entries appear to be subprocesses/threads
1145 s
find -L /proc
/[1-9]*/task
/*/ns
/net
-samefile /run
/netns
/$netns | cut
-d/ -f5 | \
1147 x
=$
(ps
-w --no-headers -p $l);
1148 if [[ $x ]]; then echo "$x"; else echo $l; fi;
1152 m
() { printf "%s\n" "$*"; "$@"; }
1156 for x
in "$@"; do virsh destroy
"$x"; virsh undefine
"$x"; done
1164 s virsh dumpxml
$vm |
sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
1165 sed -r "s/listen='[^']+/listen='$ip/"> $t
1166 s virsh undefine
$vm
1172 vm-set-listen
$1 0.0.0.0
1177 vm-set-listen
$1 127.0.0.1
1183 # temporary variables to test colorization
1184 # some copied from gentoo /etc/bash/bashrc,
1186 # dircolors --print-database uses its own built-in database
1187 # instead of using /etc/DIR_COLORS. Try to use the external file
1188 # first to take advantage of user additions.
1189 safe_term
=${TERM//[^[:alnum:]]/?} # sanitize TERM
1191 [[ -f ~
/.dir_colors
]] && match_lhs
="${match_lhs}$(<~/.dir_colors)"
1192 [[ -f /etc
/DIR_COLORS
]] && match_lhs
="${match_lhs}$(</etc/DIR_COLORS)"
1193 [[ -z ${match_lhs} ]] \
1194 && type -P dircolors
>/dev
/null \
1195 && match_lhs
=$
(dircolors
--print-database)
1196 # test if our $TERM is in the TERM values in dircolor
1197 [[ $
'\n'${match_lhs} == *$
'\n'"TERM "${safe_term}* ]] && use_color
=true
1200 if ${use_color} && [[ $
- == *i
* ]]; then
1202 term_bold
="$(tput bold)"
1203 term_red
="$(tput setaf 1)"
1204 term_green
="$(tput setaf 2)"
1205 term_yellow
="$(tput setaf 3)"
1206 term_purple
="$(tput setaf 5)"
1207 term_nocolor
="$(tput sgr0)" # no font attributes
1209 # unused so far. commented for shellcheck
1210 # term_underl="$(tput smul)"
1211 # term_blue="$(tput setaf 4)"
1212 # term_cyan="$(tput setaf 6)"
1215 # Try to keep environment pollution down, EPA loves us.
1216 unset safe_term match_lhs use_color
1221 if [[ $
- == *i
* ]]; then
1223 # this needs to come before next ps1 stuff
1224 # this stuff needs bash 4, feb 2009,
1225 # old enough to no longer condition on $BASH_VERSION anymore
1229 if [[ $
- == *i
* ]] && [[ ! $RLC_INSIDE_EMACS ]]; then
1231 bind -m vi-command B
:shell-backward-word
1232 bind -m vi-command W
:shell-forward-word
1235 if [[ $SSH_CLIENT ||
$SUDO_USER ]]; then
1239 # emacs terminal has problems if this runs slowly,
1240 # so I've thrown a bunch of things at the wall to speed it up.
1242 local return=$?
# this MUST COME FIRST
1243 local ps_char ps_color
1246 history -a # save history
1249 0) ps_color
="$term_purple"
1252 1) ps_color
="$term_green"
1253 ps_char
="$return \\$"
1255 *) ps_color
="$term_yellow"
1256 ps_char
="$return \\$"
1259 if [[ ! -O .
]]; then # not owner
1260 if [[ -w .
]]; then # writable
1261 ps_color
="$term_bold$term_red"
1263 ps_color
="$term_bold$term_green"
1267 # faster than sourceing the file im guessing
1268 if [[ -e /dev
/shm
/iank-status
]]; then
1269 eval $
(< /dev
/shm
/iank-status
)
1271 if [[ ! $SSH_CLIENT && $MAIL_HOST != "$HOSTNAME" ]]; then
1272 ps_char
="@ $ps_char"
1274 PS1
="${PS1%"${PS1#*[wW]}"} \[$ps_color\]$ps_char\[$term_nocolor\] "
1276 PROMPT_COMMAND
=prompt-command
1279 if [[ $TERM == screen
* ]]; then
1280 local title_escape
="\033]..2;"
1282 local title_escape
="\033]0;"
1284 if [[ $0 != prompt-command
]]; then
1285 echo -ne "$title_escape$USER@$HOSTNAME ${PWD/#$HOME/~} "
1292 # condition from the screen man page i think.
1293 # note: duplicated in tx()
1294 # disabled. see note in tx
1295 # if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
1296 # trap 'settitle "$BASH_COMMAND"' DEBUG
1303 # * stuff that makes sense to be at the end
1309 # shellcheck disable=SC1090
1310 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
1313 # ensure no bad programs appending to this file will have an affect