updates, especially for etiona
[distro-setup] / brc
diff --git a/brc b/brc
index 33e664616c36487ad7f0b426e6d3c25e303eea3f..a85cd66db70995f727d010fd7194e9782ef171f6 100644 (file)
--- a/brc
+++ b/brc
@@ -3,18 +3,37 @@
 # SPDX-License-Identifier: AGPL-3.0-or-later
 # this gets sourced. shebang is just for file mode detection
 
-# note, to catch errors in functions but not outside, do:
-# set -E -o pipefail
-# trap return ERR
-# trap 'trap ERR' RETURN
 
+# Use source ~/.bashrc instead of doing bash -l when running a script
+# so this can set extdebug and avoid the bash debugger.
+if [[ -s /a/bin/errhandle/err ]]; then
+  source /a/bin/errhandle/err
+elif [[ -s ~/.iank/err ]]; then
+  # shellcheck source=/a/bin/errhandle/err
+  source ~/.iank/err
+fi
+
+# In t8, it runs clear_console for login shells by default. I don't want
+# my console cleared. And linux ttys get cleared without this.
+if shopt login_shell >/dev/null && [[ -e ~/.bash_logout ]]; then
+  rm ~/.bash_logout
+fi
+
+# for testing error catching:
+# t2() {
+#   echo t2
+#   grep sdf sdfd
+#   echo wtf
+# }
+# t1() {
+#   echo t1
+#   t2 a b c
+# }
 
 # * settings
 
 CDPATH=.
 
-set -o pipefail
-
 # remove all aliases. aliases provided by the system tend to get in the way,
 # for example, error happens if I try to define a function the same name as an alias
 unalias -a
@@ -62,7 +81,7 @@ if [[ $RLC_INSIDE_EMACS ]]; then
   export PAGER=cat
   export MANPAGER=cat
   # scp completion does not work, but this doesnt fix it. todo, figure this out
-  complete -r scp &> /dev/null
+  #complete -r scp &> /dev/null
   # todo, remote file completion fails, figure out how to turn it off
   export NODE_DISABLE_COLORS=1
   # This gets rid of ugly terminal escape chars in node repl
@@ -141,28 +160,9 @@ if [[ $- == *i* ]]; then
 
 fi
 
-
-# history number. History expansion is good.
-PS4='$LINENO+ '
-# history file size limit, set to unlimited.
-# this needs to be different from the default because
-# default HISTFILESIZE is 500 and could clobber our history
-HISTFILESIZE=
-# max commands 1 session can append/read from history
-HISTSIZE=1000000
-# the time format display when doing the history command
-# also, setting this makes the history file record time
-# of each command as seconds from the epoch
-HISTTIMEFORMAT="%Y-%m-%d %I:%M %p "
-# consecutive duplicate lines dont go in history
-HISTCONTROL=ignoredups
-# works in addition to HISTCONTROL to do more flexible things
-# it could also do the same things as HISTCONTROL and thus replace it,
-# but meh. dunno why, but just " *" does glob expansion, so use [ ] to avoid it.
-HISTIGNORE='pass *:[ ]*:otp *:oathtool *'
-
 export BC_LINE_LENGTH=0
 
+# ansible option
 export PROFILE_TASKS_TASK_OUTPUT_LIMIT=100
 
 # note, if I use a machine I dont want files readable by all users, set
@@ -199,12 +199,13 @@ if [[ -s /usr/share/wcd/wcd-include.sh ]]; then
 fi
 
 if [[ -s /a/bin/small-misc-bash/ll-function ]]; then
-  # shellcheck source=/a/bin/small-misc-bash/ll-function
   source /a/bin/small-misc-bash/ll-function
 elif [[ -s ~/.iank/ll-function ]]; then
+  # shellcheck source=/a/bin/small-misc-bash/ll-function
   source ~/.iank/ll-function
 fi
 
+
 # * functions
 
 
@@ -299,7 +300,7 @@ caf() {
   find -L $1 -type f -not \( -name .svn -prune -o -name .git -prune \
        -o -name .hg -prune -o -name .editor-backups -prune \
        -o -name .undo-tree-history -prune \) \
-       -exec bash -lc 'hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
+       -exec bash -c '. ~/.bashrc; hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
 
 }
 
@@ -317,7 +318,7 @@ cam() {
 
 
 ccat () { # config cat. see a config without extra lines.
-  grep '^\s*[^;[:space:]#]' "$@"
+  grep '^\s*[^;[:space:]#]' "$@" || [[ $? == 1 ]]
 }
 
 
@@ -412,6 +413,39 @@ despace() {
   done
 }
 
+dig() {
+  command dig +nostats +nocmd "$@"
+}
+# Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed.
+digsort() {
+  local sec
+  sec=
+  dig +nordflag "$@" | sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' | while read -r l; do
+    if [[ $l == [^\;]* ]]; then
+      sec+="$l"$'\n'
+    else
+      if [[ $sec ]]; then
+        printf "%s" "$sec" | sort
+        sec=
+      fi
+      printf "%s\n" "$l"
+    fi
+  done
+}
+# compare digs to the 2 servers
+# usage: digdiff @server1 @server2 DIG_ARGS
+# note: only the soa master nameserver will respond with
+# ra "recursive answer" flag. That difference is meaningless afaik.
+digdiff() {
+  local s1 s2
+  s1=$1
+  shift
+  s2=$1
+  shift
+  digsort $s1 "$@" | tee /tmp/digdiff
+  diff -u /tmp/digdiff <(digsort $s2 "$@")
+}
+
 dt() {
   date "+%A, %B %d, %r" "$@"
 }
@@ -424,13 +458,27 @@ dus() { # du, sorted, default arg of
 
 e() { echo "$@"; }
 
-# echo var. print var including escapes, etc
+# echo args
+ea() {
+  if (( ! $# )); then
+    echo no args
+  fi
+  for arg; do
+    printf "%qEOL\n" "${arg}"
+    printf "%s" "${arg}" |& hexdump -C
+  done
+}
+# echo vars. print var including escapes, etc
 ev() {
-  printf "%qEOL\n" "${!1}"
-  printf "%s" "${!1}" |& hexdump -C
+  if (( ! $# )); then
+    echo no args
+  fi
+  for arg; do
+    printf "%qEOL\n" "${!arg}"
+    printf "%s" "${!arg}" |& hexdump -C
+  done
 }
 
-
 ediff() {
   [[ ${#@} == 2 ]] || { echo "error: ediff requires 2 arguments"; return 1; }
   emacs --eval "(ediff-files \"$1\" \"$2\")"
@@ -444,7 +492,7 @@ eless() {
   less /var/log/exim4/mainlog
 }
 eqcat() {
-  exiqgrep -i | while read i; do
+  exiqgrep -i | while read -r i; do
     exim -Mvh $i; hr; exim -Mvb $i; hr;
     exigrep $i /var/log/exim4/mainlog; hr
   done
@@ -475,12 +523,12 @@ faf() { # find all files. use -L to follow symlinks
 # mail related
 frozen() {
   rm -rf /tmp/frozen
-  s mailq |gr frozen|awk '{print $3}' | while read -r id; do
-    s exim -Mvl $id
+  sudo mailq |gr frozen|awk '{print $3}' | while read -r id; do
+    sudo exim -Mvl $id
     echo
-    s exim -Mvh $id
+    sudo exim -Mvh $id
     echo
-    s exim -Mvb $id
+    sudo exim -Mvb $id
     echo -e '\n\n##############################\n'
   done | tee -a /tmp/frozen
 }
@@ -492,7 +540,7 @@ frozenrm() {
   done < <(s mailq)
   echo "sleeping for 2 in case you change your mind"
   sleep 2
-  s exim -Mrm "${ids[@]}"
+  sudo exim -Mrm "${ids[@]}"
 }
 
 funce() {
@@ -580,10 +628,12 @@ gr() {
 }
 
 grr() { # grep recursive
+  # Don't return 1 on nonmatch because this is meant to be
+  # interactive, not in a conditional.
   if [[ ${#@} == 1 ]]; then
-    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" .
+    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" . || [[ $? == 1 ]]
   else
-    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@"
+    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" || [[ $? == 1 ]]
   fi
 }
 rg() {
@@ -591,7 +641,8 @@ rg() {
 }
 
 hr() { # horizontal row. used to break up output
-  printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(seq ${COLUMNS:-60})
+
+  printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
   echo
 }
 
@@ -615,7 +666,7 @@ hub() {
     wget -P /a/opt $up
     tar -C /a/opt -zxf /a/opt/$uptar
     rm -f /a/opt/$uptar
-    s /a/opt/$updir/install
+    sudo /a/opt/$updir/install
   fi
 
   # save token across computers
@@ -660,7 +711,7 @@ ifn() {
 }
 
 ipdrop() {
-  s iptables -A INPUT -s $1 -j DROP
+  sudo iptables -A INPUT -s $1 -j DROP
 }
 
 
@@ -717,11 +768,11 @@ lower() { # make first letter of filenames lowercase.
 
 
 k() { # history search
-  grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history}  | tail -n 80;
+  grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history}  | tail -n 80 || [[ $? == 1 ]];
 }
 
 ks() { # history search
-  grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history}  | uniq;
+  grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history}  | uniq || [[ $? == 1 ]];
 }
 
 
@@ -761,7 +812,7 @@ pkx() { # package extract
   c $(mktemp -d)
   pkg=$1
   # shellcheck disable=SC2012
-  cached=$(ls -t /var/cache/apt/archives/$pkg* | tail -n1 2>/dev/null)
+  cached=$(ls -t /var/cache/apt/archives/$pkg* | tail -n1 2>/dev/null) ||:
   if [[ $cached ]]; then
     cp $cached .
   else
@@ -791,6 +842,23 @@ pk1() {
   esac
 }
 
+psg () {
+  local x y help
+  help="Usage: psg [--help] GREP_ARGS
+grep ps and output in a nice format"
+  if [[ $1 == --help ]]; then
+    echo "$help"
+    return
+  fi
+  x=$(sudo ps -eF)
+  # final grep is because some commands tend to have a lot of trailing spaces
+  y=$(echo "$x" | grep -iP "$@" | grep -o '.*[^ ]') ||:
+  if [[ $y ]]; then
+    echo "$x" | head -n 1 || [[ $? == 141 ]]
+    echo "$y"
+  fi
+}
+
 pubip() { curl -4s https://icanhazip.com; }
 pubip6() { curl -6s https://icanhazip.com; }
 whatismyip() { pubip; }
@@ -820,7 +888,10 @@ q() { # start / launch a program in the backround and redir output to null
 
 # shellcheck disable=SC2120
 r() {
-  history -a # save history
+  if [[ $HISTFILE ]]; then
+    history -a # save history
+  fi
+  trap ERR # this avoids a segfault
   exit ${1:0}
   # i had this redir, not sure why
   #  exit "$@" 2>/dev/null
@@ -857,7 +928,7 @@ rlu() { # [OPTS] HOST PATH
   # rync here uses checksum instead of time so we dont mess with
   # unison relying on time as much. g is for group, same reason
   # to keep up with unison.
-  s rsync -rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
+  sudo rsync -rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
 }
 
 rmstrips() {
@@ -874,7 +945,8 @@ s() {
   # with root owned files.
   #
   if [[ $EUID != 0 || $1 == -* ]]; then
-    SUDOD="$PWD" sudo -i "$@"
+    # shellcheck disable=SC2034
+    SUDOD="$PWD" command sudo -i "$@"
   else
     "$@"
   fi
@@ -906,7 +978,7 @@ complete -F _root_command s sb
 
 
 ser() {
-  local s; [[ $EUID != 0 ]] && s=s
+  local s; [[ $EUID != 0 ]] && s=sudo
   if type -p systemctl &>/dev/null; then
     $s systemctl $1 $2
   else
@@ -963,7 +1035,7 @@ sk() {
   # 2119: Functions with optional args get bad warnings when none are passed.
   # 2033: too many false positives for thing that will never work, passing shell function to find.
   # i had -x as an arg, but debian testing(stretch) doesn\'t support it
-  shellcheck -x -e 2086,2046,2068,2119,2033 "$@"
+  shellcheck -x -e 2086,2046,2068,2119,2033 "$@" || return $?
   # had this before. not sure what it is 2119
 }
 
@@ -1097,7 +1169,7 @@ psnetns() {
   local x netns
   netns=$1
   ps -w | head -n 1
-  s find -L /proc/[1-9]*/task/*/ns/net -samefile /run/netns/$netns | cut -d/ -f5 | \
+  sudo find -L /proc/[1-9]*/task/*/ns/net -samefile /run/netns/$netns | cut -d/ -f5 | \
     while read -r l; do
       x=$(ps -w --no-headers -p $l);
       if [[ $x ]]; then echo "$x"; else echo $l; fi;
@@ -1116,10 +1188,10 @@ vm-set-listen(){
   t=$(mktemp)
   local vm=$1
   local ip=$2
-  s virsh dumpxml $vm | sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
+  sudo virsh dumpxml $vm | sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
     sed -r "s/listen='[^']+/listen='$ip/"> $t
-  s virsh undefine $vm
-  s virsh define $t
+  sudo virsh undefine $vm
+  sudo virsh define $t
 }
 
 
@@ -1197,7 +1269,17 @@ if [[ $- == *i* ]]; then
     local return=$? # this MUST COME FIRST
     local ps_char ps_color
     unset IFS
-    history -a # save history
+
+    if [[ $HISTFILE ]]; then
+      history -a # save history
+    fi
+
+    if [[ $jr_pid ]]; then
+      if [[ -e /proc/$jr_pid ]]; then
+        kill $jr_pid
+      fi
+      unset jr_pid
+    fi
 
     case $return in
       0) ps_color="$term_purple"
@@ -1263,6 +1345,14 @@ unset IFS
 # shellcheck disable=SC1090
 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
 
+# I had this idea to start a bash shell which would run an initial
+# command passed through this env variable, then continue on
+# interactively. But the use case I had in mind went away.
+#
+# if [[ $MY_INIT_CMD ]]; then
+#   "${MY_INIT_CMD[@]}"
+#   unset MY_INIT_CMD
+# fi
 
 # ensure no bad programs appending to this file will have an affect
 return 0