From 7d5e9e87105fafa2243603f958c8b41cdaa7f0ad Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Tue, 5 Nov 2019 15:56:46 -0500 Subject: [PATCH] mostly fixes for new error handling --- brc | 43 ++++++++++++++++--- brc2 | 19 ++++---- conflink | 2 +- demohost-mount | 6 +-- desktop-20-autostart.sh | 4 +- disabled/kodi-setup | 3 +- disabled/phabricator-setup | 5 +-- distro-begin | 8 ++-- distro-end | 5 ++- distro-pkgs | 7 +-- dsremote | 4 +- dynamic-ip-update | 5 +-- fsf-vpn-dns-cleanup | 2 +- input-setup | 3 +- .../check-lets-encrypt-ssl-settings | 4 +- mail-route | 2 +- mail-setup | 4 +- music-tag-sync | 7 +-- sl/.iank/err | 1 + ssh-emacs-setup | 4 +- switch-mail-host | 14 +++--- trusted-network | 2 +- untrusted-network | 2 +- 23 files changed, 100 insertions(+), 56 deletions(-) create mode 120000 sl/.iank/err diff --git a/brc b/brc index e8737aa..f943988 100644 --- a/brc +++ b/brc @@ -3,7 +3,14 @@ # 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 # * settings @@ -195,12 +202,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 +303,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 +496,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 @@ -624,10 +632,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 +646,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 } @@ -836,6 +846,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 +893,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 @@ -919,6 +947,7 @@ s() { # with root owned files. # if [[ $EUID != 0 || $1 == -* ]]; then + # shellcheck disable=SC2034 SUDOD="$PWD" sudo -i "$@" else "$@" diff --git a/brc2 b/brc2 index 959e892..df695d2 100644 --- a/brc2 +++ b/brc2 @@ -29,7 +29,6 @@ export WCDHOME=/a source /a/bin/distro-functions/src/identify-distros source /a/bin/distro-functions/src/package-manager-abstractions source /a/bin/log-quiet/logq-function -source /a/bin/small-misc-bash/psg-function # for x in /a/bin/bash_unpublished/source-!(.#*); do echo source $x; done source /a/bin/bash_unpublished/source-semi-priv source /a/bin/bash_unpublished/source-state @@ -236,8 +235,9 @@ chrome() { # do all tee. # pipe to this, or just type like a shell +# todo: test this dat() { - tee >(ssh frodo.b8.nz bash -l) >(ssh x2 bash -l) >(ssh tp.b8.nz bash -l) >(ssh kw bash -l) >(ssh tp.b8.nz bash -l) + tee >(ssh frodo.b8.nz) >(ssh x2) >(ssh tp.b8.nz) >(ssh kw) >(ssh tp.b8.nz) } da() { # do all local host @@ -867,7 +867,7 @@ spend() { # By default .bashrc is sourced for ALL ssh commands. This is wonky. # Normally, this file is not sourced when a script is run, but we can -# override that by having #!/bin/bash -l. I want the same thing for ssh +# override that by sourcing ~/.bashrc. I want the same thing for ssh # commands. when a local script runs an ssh command, bashrc should not be # sourced, unless we use a modified command. # @@ -936,10 +936,13 @@ sl() { type=${tmp2: -1} if [[ $type == b ]]; then info_sec=${tmp::10} - if (( $(stat -c%Y /b/ds/brc) > info_sec || $(stat -c%Y /b/ds/.bashrc) > info_sec )); then - dorsync=true - rm -f $sshinfo - fi + for f in /b/ds/sl/.iank/*; do + if (( $(stat -L -c%Y $f) > info_sec )); then + dorsync=true + rm -f $sshinfo + break + fi + done fi else dorsync=true @@ -986,7 +989,7 @@ sl() { if [[ -t 0 ]]; then BRC=t command ssh "${args[@]}" "$remote" ${@@Q} else - command ssh "${args[@]}" "$remote" BRC=t bash -l + command ssh "${args[@]}" "$remote" BRC=t bash fi fi } diff --git a/conflink b/conflink index 093dc2d..55d7a02 100755 --- a/conflink +++ b/conflink @@ -1,7 +1,7 @@ #!/bin/bash source /a/bin/errhandle/err -_errcatch_cleanup() { +errcatch-cleanup() { echo 1 >~/.local/conflink } diff --git a/demohost-mount b/demohost-mount index 706a516..90d2b42 100755 --- a/demohost-mount +++ b/demohost-mount @@ -1,7 +1,5 @@ -#!/bin/bash -l -set -eE -o pipefail -trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR - +#!/bin/bash +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@" diff --git a/desktop-20-autostart.sh b/desktop-20-autostart.sh index 03be1c5..ed83642 100755 --- a/desktop-20-autostart.sh +++ b/desktop-20-autostart.sh @@ -1,4 +1,4 @@ -#!/bin/bash -l +#!/bin/bash # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,7 +14,7 @@ # limitations under the License. # first 2 alternatives showed under ubuntu 14.04, second 2 under arch at 11/2015 - +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi if [[ $1 ]]; then right_monitor_rotation=left else diff --git a/disabled/kodi-setup b/disabled/kodi-setup index ef4a4f8..6a4f8f8 100644 --- a/disabled/kodi-setup +++ b/disabled/kodi-setup @@ -1,6 +1,7 @@ -#!/bin/bash -l +#!/bin/bash # this is from distro-end +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi pi kodi diff --git a/disabled/phabricator-setup b/disabled/phabricator-setup index ebd9360..212ac62 100755 --- a/disabled/phabricator-setup +++ b/disabled/phabricator-setup @@ -1,4 +1,4 @@ -#!/bin/bash -l +#!/bin/bash # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,8 +18,7 @@ # Automated phabricator setup. Not currently using it, # but it worked last time I tried it. -set -eE -o pipefail -trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi set -x diff --git a/distro-begin b/distro-begin index 0377b1c..75b9b05 100755 --- a/distro-begin +++ b/distro-begin @@ -1,4 +1,4 @@ -#!/bin/bash -l +#!/bin/bash # Copyright (C) 2016 Ian Kelling # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -44,9 +44,9 @@ ### make ssh interactive shell run better. for when running line interactively line by line -sudo bash -c 'source /a/c/.bashrc && source /a/exe/ssh-emacs-setup' - +sudo bash -c '/a/exe/ssh-emacs-setup' +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi ##### setup error handling interactive=true # set this to false to force set -x [[ $- == *i* ]] || interactive=false @@ -55,7 +55,7 @@ if ! $interactive; then fi source /a/bin/errhandle/err -_errcatch_cleanup() { +errcatch-cleanup() { echo 1 >~/.local/distro-begin } diff --git a/distro-end b/distro-end index f8419aa..a057d77 100755 --- a/distro-end +++ b/distro-end @@ -1,6 +1,7 @@ -#!/bin/bash -l +#!/bin/bash # Copyright (C) 2019 Ian Kelling # SPDX-License-Identifier: AGPL-3.0-or-later +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi ### setup source /a/bin/errhandle/err @@ -11,7 +12,7 @@ if [[ $EUID == 0 ]]; then exit 1 fi -_errcatch_cleanup() { +errcatch-cleanup() { echo 1 >~/.local/distro-end } diff --git a/distro-pkgs b/distro-pkgs index 5fdd13c..b69af1a 100755 --- a/distro-pkgs +++ b/distro-pkgs @@ -1,6 +1,7 @@ -#!/bin/bash -l - -source /a/bin/errhandle/err +#!/bin/bash +# Copyright (C) 2019 Ian Kelling +# SPDX-License-Identifier: AGPL-3.0-or-later +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi usage() { diff --git a/dsremote b/dsremote index 2557b73..13a5176 100755 --- a/dsremote +++ b/dsremote @@ -1,4 +1,4 @@ -#!/bin/bash -l +#!/bin/bash # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi + set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR diff --git a/dynamic-ip-update b/dynamic-ip-update index 1f55f4c..e589f15 100755 --- a/dynamic-ip-update +++ b/dynamic-ip-update @@ -1,6 +1,5 @@ -#!/bin/bash -l -set -eE -o pipefail -trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR +#!/bin/bash +source ~/.bashrc fqdn=$(hostname -f) diff --git a/fsf-vpn-dns-cleanup b/fsf-vpn-dns-cleanup index be7fb58..d9e755c 100755 --- a/fsf-vpn-dns-cleanup +++ b/fsf-vpn-dns-cleanup @@ -6,6 +6,6 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR if ! echo | /a/exe/cedit fsf /etc/dnsmasq-servers.conf; then if systemctl is-active dnsmasq >/dev/null; then nscd -i hosts - systemctl reload dnsmasq + systemctl restart dnsmasq fi fi diff --git a/input-setup b/input-setup index ba390da..6c94a54 100755 --- a/input-setup +++ b/input-setup @@ -1,4 +1,5 @@ -#!/bin/bash -l +#!/bin/bash +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi set -x # Copyright (C) 2016 Ian Kelling diff --git a/machine_specific/kd/filesystem/etc/cron.daily/check-lets-encrypt-ssl-settings b/machine_specific/kd/filesystem/etc/cron.daily/check-lets-encrypt-ssl-settings index 87e45c0..a3fbfc6 100755 --- a/machine_specific/kd/filesystem/etc/cron.daily/check-lets-encrypt-ssl-settings +++ b/machine_specific/kd/filesystem/etc/cron.daily/check-lets-encrypt-ssl-settings @@ -1,4 +1,4 @@ -#!/bin/bash -l +#!/bin/bash # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +source ~/.bashrc + set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR diff --git a/mail-route b/mail-route index 332f1ed..2dc237d 100755 --- a/mail-route +++ b/mail-route @@ -60,7 +60,7 @@ up() { fi fi e() { echo "$0: $*"; "$@"; } - _errcatch_cleanup=stop + errcatch-cleanup() { stop; } modify # we leave it as is even when stopping, because we would like it to be default, but the only way # to change the default is for every device, and I want to avoid that, even though I wouldn't mind, others users of this script might. diff --git a/mail-setup b/mail-setup index 68478be..98d5c41 100755 --- a/mail-setup +++ b/mail-setup @@ -676,7 +676,7 @@ EOF server=/mail.iankelling.org/127.0.1.1 EOF if systemctl is-active dnsmasq >/dev/null; then - m systemctl reload dnsmasq + m systemctl restart dnsmasq m nscd -i hosts fi @@ -810,7 +810,7 @@ EOF echo | /a/exe/cedit mail /etc/dnsmasq-servers.conf || [[ $? == 1 ]] if systemctl is-active dnsmasq >/dev/null; then m nscd -i hosts - m systemctl reload dnsmasq + m systemctl restart dnsmasq # reload does not ensure new config is used fi m systemctl disable mailclean.timer &>/dev/null ||: diff --git a/music-tag-sync b/music-tag-sync index 0f6e7d9..06248e0 100644 --- a/music-tag-sync +++ b/music-tag-sync @@ -1,6 +1,7 @@ -#!/bin/bash -l -set -eE -o pipefail -trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR +#!/bin/bash +# Copyright (C) 2019 Ian Kelling +# SPDX-License-Identifier: AGPL-3.0-or-later +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi # cd /k/music # find -type f -name '*.flac' | while read -r f; do # mkdir -p "../flacs/$(dirname "$f")" diff --git a/sl/.iank/err b/sl/.iank/err new file mode 120000 index 0000000..c877367 --- /dev/null +++ b/sl/.iank/err @@ -0,0 +1 @@ +/a/bin/errhandle/err \ No newline at end of file diff --git a/ssh-emacs-setup b/ssh-emacs-setup index 80976f0..65679fd 100755 --- a/ssh-emacs-setup +++ b/ssh-emacs-setup @@ -1,4 +1,4 @@ -#!/bin/bash -l +#!/bin/bash # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,6 +17,8 @@ if [[ $EUID != 0 ]]; then sudo "$0" exit fi +if [[ -s ~/.bashrc ]];then . ~/.bashrc;fi + set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR diff --git a/switch-mail-host b/switch-mail-host index a0c590d..8f0912f 100755 --- a/switch-mail-host +++ b/switch-mail-host @@ -24,7 +24,7 @@ EOF restore_new_btrbk=false restore_old_btrbk=false -cleanup() { +errcatch-cleanup() { if $restore_new_btrbk; then e WARNING: due to failure, btrbk.timer may need manual restoration: e $new_shell sudo systemctl start btrbk.timer @@ -100,12 +100,10 @@ new_hostname=$($new_shell hostname) if $new_shell systemctl is-active btrbk.timer; then m $new_shell sudo systemctl stop btrbk.timer - _errcatch_cleanup=cleanup restore_new_btrbk=true fi if $old_shell systemctl is-active btrbk.timer; then m $old_shell sudo systemctl stop btrbk.timer - _errcatch_cleanup=cleanup restore_old_btrbk=true fi @@ -150,13 +148,19 @@ EOF # ipv6 i think. +if ! m btrbk-run -v $bbk_args $mp_args; then + ret=$? + err "failed initial btrbk" + exit $ret +fi + m $old_shell /a/exe/primary-setup $new_hostname -if ! m btrbk-run -v $bbk_args $mp_args; then +if ! m btrbk-run -v $bbk_args -m /o; then ret=$? bang="$(printf "$(tput setaf 5)█$(tput sgr0)%.0s" 1 2 3 4 5 6 7)" - e $bang failed btrbk. restoring old host as primary + e $bang failed btrbk of /o. restoring old host as primary m $old_shell /a/exe/primary-setup localhost exit $ret fi diff --git a/trusted-network b/trusted-network index 220c963..7cd754c 100755 --- a/trusted-network +++ b/trusted-network @@ -12,5 +12,5 @@ if [[ -e /etc/dnsmasq.d/untrusted-network.conf ]]; then rm -f /etc/dnsmasq.d/untrusted-network.conf nscd -i hosts - systemctl reload dnsmasq + systemctl restart dnsmasq fi diff --git a/untrusted-network b/untrusted-network index 9cbf4ab..b4fdb70 100755 --- a/untrusted-network +++ b/untrusted-network @@ -20,5 +20,5 @@ stop-dns-rebind EOF nscd -i hosts - systemctl reload dnsmasq + systemctl restart dnsmasq fi -- 2.30.2