various fixes
[distro-setup] / brc
diff --git a/brc b/brc
index e8737aaef201894a24cc29ac2dfe1fde45b6ab8b..556c5ef04e743bb2208cc54dd7ae3d7cd1569cbe 100644 (file)
--- a/brc
+++ b/brc
@@ -3,7 +3,26 @@
 # SPDX-License-Identifier: AGPL-3.0-or-later
 # this gets sourced. shebang is just for file mode detection
 
-source /a/bin/errhandle/err
+# 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
+
+t2() {
+  echo t2
+  popd
+  grep sdf sdfd
+  echo wtf
+}
+t1() {
+  echo t1
+  trap
+  t2 a b c
+}
 
 # * settings
 
@@ -195,12 +214,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
 
 
@@ -295,7 +315,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
 
 }
 
@@ -488,7 +508,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
@@ -519,12 +539,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
 }
@@ -536,7 +556,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() {
@@ -624,10 +644,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() {
@@ -636,7 +658,7 @@ rg() {
 
 hr() { # horizontal row. used to break up output
 
-  printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo {1..${COLUMNS:-60}})
+  printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
   echo
 }
 
@@ -660,7 +682,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
@@ -705,7 +727,7 @@ ifn() {
 }
 
 ipdrop() {
-  s iptables -A INPUT -s $1 -j DROP
+  sudo iptables -A INPUT -s $1 -j DROP
 }
 
 
@@ -836,6 +858,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
+    echo "$y"
+  fi
+}
+
 pubip() { curl -4s https://icanhazip.com; }
 pubip6() { curl -6s https://icanhazip.com; }
 whatismyip() { pubip; }
@@ -866,6 +905,7 @@ q() { # start / launch a program in the backround and redir output to null
 # shellcheck disable=SC2120
 r() {
   history -a # save history
+  trap ERR # this avoids a segfault
   exit ${1:0}
   # i had this redir, not sure why
   #  exit "$@" 2>/dev/null
@@ -902,7 +942,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() {
@@ -919,7 +959,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
@@ -951,7 +992,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
@@ -1008,7 +1049,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
 }
 
@@ -1142,7 +1183,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;
@@ -1161,10 +1202,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
 }