From: Ian Kelling Date: Tue, 12 Nov 2024 00:29:53 +0000 (-0500) Subject: fixes, improvements, shellcheck X-Git-Url: https://iankelling.org/git/?a=commitdiff_plain;h=cd4332aea47213a33e09261c618da4f9fe0e7f29;p=distro-setup fixes, improvements, shellcheck --- diff --git a/brc b/brc index a00bb8d..ed3f92d 100644 --- a/brc +++ b/brc @@ -855,8 +855,8 @@ d16fan() { (210, 70, 80) (255, 75, 32767) EOF - find /sys/ | grep 'temp[1-8]_input' | xargs -i echo hwmon {} >> /etc/thinkfan.conf - find /sys/module/w83795/drivers/*/*/pwm1 | sort | head -1 | xargs -i echo pwm_fan {} >> /etc/thinkfan.conf + find /sys/ | grep 'temp[1-8]_input' | xargs -I{} echo hwmon {} >> /etc/thinkfan.conf + find /sys/module/w83795/drivers/*/*/pwm1 | sort | head -1 | xargs -I{} echo pwm_fan {} >> /etc/thinkfan.conf systemctl restart thinkfan } @@ -1366,6 +1366,17 @@ ev() { done } + +# echo variable array +eva() { + local a arg="$1" + # shellcheck disable=all # this outputs code and is expected to confuse shellcheck + for a in \"\${$arg[@]}\"; do + echo "$a" + done +} + + # emacs ediff from cli ediff() { [[ ${#@} == 2 ]] || { echo "error: ediff requires 2 arguments"; return 1; } @@ -2250,11 +2261,13 @@ mkct() { } # mkdir the last arg, then cp the remaining args into it mkcp() { + # shellcheck disable=SC2124 # false positive mkdir -p "${@: -1}" cp "${@:1:$#-1}" "${@: -1}" } # mkdir the last arg, then mv the remaining args into it mkmv() { + # shellcheck disable=SC2124 # false positive mkdir -p "${@: -1}" mv "${@:1:$#-1}" "${@: -1}" } @@ -3051,7 +3064,7 @@ sl() { # .bashrc. This means the outer shell still ran the default .bashrc, # but that is the best we can do. - local now args remote dorsync haveinfo tmpa sshinfo tmp tmp2 host_type info_sec force_rsync \ + local verbose_arg now args remote do_rsync haveinfo tmpa sshinfo tmp tmp2 host_type info_sec force_rsync \ sync_dirname testcmd extra_info testbool files_sec sl_test_cmd sl_test_hook declare -a args tmpa @@ -3072,6 +3085,7 @@ sl() { # [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] # [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] + # shellcheck disable=SC3127 # todo: remove this once we use this var verbose_arg=false force_rsync=false if [[ $1 == --rsync ]]; then @@ -3114,6 +3128,7 @@ sl() { -[46AaCfGgKkMNnqsTtVvXxYy]*) if [[ $1 == *v* ]]; then # todo: try running slowdo if this is false + # shellcheck disable=SC2034 # todo: remove this once we use this var verbose_arg=true fi args+=("$1"); shift @@ -3927,10 +3942,12 @@ if [[ $- == *i* ]]; then if [[ ! $PS1 =~ 133 ]] ; then PS1='\[\e]133;A\a\]'"$PS1"'\[\e]133;B\a\]' - PS2='\[\e]133;A\a\]'"$PS2"'\[\e]133;B\a\]' # shellcheck disable=SC2034 # false positive PS0='\[\e]133;C\a\]' fi + if [[ ! $PS2 =~ 133 ]]; then + PS2='\[\e]133;A\a\]'"$PS2"'\[\e]133;B\a\]' + fi fi } @@ -4112,6 +4129,126 @@ brc3() { source /a/c/brc3 } +# Populate the array f with files, picking them by editing an ls file listing in emacs. +# +# usage: fs [options] [ls_args] +# -e EXCLUDED_STRING Exclude arg as a literal from ls output. +fs() { + local exclude tmpe tmpf=/home/iank/tmp-fs.log + if [[ $1 == -e ]]; then + exclude="$2" + shift 2 + # assume exclude is a directory and has ansi stuff, if we put that + # here as literals, I don't expect it to work right. Possible it + # could, but I'm not bothering to test. + tmpe=$(mktemp) + printf %s "$exclude" | cat /b/data/dir-ansi-prefix - /b/data/dir-ansi-suffix | tr -d '\n' >$tmpe + # shellcheck disable=SC2010 # i handle nonalphanumeric. + ls_out=$(ls -A --color=always "$@" | grep -vxFf $tmpe || [[ $? == 1 ]]) + else + ls_out=$(ls -A --color=always "$@") + fi + if [[ $tmpf == *?* ]]; then + echo "fs: error: found possible unprintable char ? in ls output. grep ?:" + printf "%s\n" "$ls_out" | grep -F "?" + return 1 + fi + printf "%s\n" "$ls_out" >$tmpf + emacsclient $tmpf + # A little hack: if we want to delete a bunch of lines all the way to + # the end of the file, instead, just open a blank line and we take + # care of it here. + sed -i '/^[[:space:]]*$/,$d' $tmpf + mapfile -t f <$tmpf +} + +# mv, but select source using fs +# +# This assumes no options given to mv. Any options will get passed to ls. +mvf() { + local dest file bad_file=false + + # the last arg. + # shellcheck disable=SC2124 # false positive + dest="${@: -1}" + # 2nd to last arg and earlier (if they exist) + fs -e "$dest" "${@:1:$#-1}" + + if (( ${#f[@]} == 0 )); then + echo "mvf: f is empty. aborting" + return 0 + fi + for file in "${f[@]}"; do + if [[ ! -e $file && ! -L $file ]]; then + echo "mvf: error: f array file:\"$file\" not found possibly due to special charater or accidental edit." >&2 + bad_file=true + fi + done + if $bad_file; then + return 1 + fi + mv "${f[@]}" "$dest" +} + +# f execute. speculating this will come in handy. +fe() { + "$@" "${f[@]}" +} + + +# Save some for loop typing. Run COMMAND on each of FILES, striping any +# leading paths. +# +# CD_DIRECTORY is assumed if FILES are in the current directory. +# +# usage: [ CD_DIRECTORY ] COMMAND... FILES.. +forf() { + local cdto f + local -a cmd files + while [[ $1 ]]; do + if [[ -e $1 ]]; then + if [[ $1 == */* ]]; then + for arg; do + files+=( "${arg##*/}" ) + done + else + if [[ -d $1 ]]; then + cdto="${cmd[0]}" + shift + fi + files=("$@") + fi + break + fi + cmd+=("$1") + shift + done + if [[ $cdto ]]; then + { + cd "$cdto" + for f in "${files[@]}"; do + "${cmd[@]:1}" "$f" + done + } + else + for f in "${files[@]}"; do + "${cmd[@]}" "$f" + done + fi +} + +# exists or print. aka exists predicate. +# +# Print any args that are nonexistent files. +ep() { + local f + for f; do + if [[ ! -e $f ]]; then + e "$f" + fi + done +} + # * stuff that makes sense to be at the end diff --git a/brc2 b/brc2 index b1bfb36..e44c101 100644 --- a/brc2 +++ b/brc2 @@ -250,7 +250,7 @@ rm-docker-iptables() { # s schroot -c flidas update-locale LANG=en_US.UTF-8 mkschroot() { - local sources force repo n distro + local sources force repo n codename force=false while [[ $1 == -* ]]; do case $1 in @@ -616,7 +616,7 @@ update annotation set rating = $rating beetrating() { local ssh_prefix source /p/c/domain-info - if [[ $HOSTNAME != $d_host ]]; then + if [[ $HOSTNAME != "$d_host" ]]; then ssh_prefix="ssh b8.nz" fi # shellcheck disable=SC2016 # obvious reason @@ -840,7 +840,7 @@ mpvrpc-loadfile() { # note: logic duplicated in beetpull local remote_p=true source /p/c/domain-info - if [[ $HOSTNAME == $d_host ]]; then + if [[ $HOSTNAME == "$d_host" ]]; then remote_p=false fi @@ -1348,7 +1348,7 @@ beetpull() { local sshfs_host sshfs_cmd sshfs_host=b8.nz source /p/c/domain-info - if [[ $HOSTNAME == $d_host ]]; then + if [[ $HOSTNAME == "$d_host" ]]; then return 0 fi if [[ ! -e /i ]]; then @@ -1367,7 +1367,7 @@ nav-rm-plists() { local tmpf id tmpf=$(mktemp) source /p/c/domain-info - if [[ $HOSTNAME != $d_host ]]; then + if [[ $HOSTNAME != "$d_host" ]]; then echo "error: run on kd" return 1 fi @@ -3078,7 +3078,7 @@ sdmn() { ns=$1 unit=$2 shift 2 - pid=$(servicepid -n $unit) + pid=$(servicepid $unit) env-tmpf "$@" if $alt_user; then final_args=("$@") @@ -3120,7 +3120,7 @@ mnsd() { # mount namespace + systemd network namespace shift 2 ## end command line args ## - pid=$(servicepid -n $unit) + pid=$(servicepid $unit) env-tmpf "$@" if $alt_user; then final_args=("$@") @@ -3440,8 +3440,9 @@ myprof() { pushd /home/iank/.local/share/profanity/chatlogs/iank_at_fsf.org/rooms/office_at_conference.fsf.org logs=(*) cd /home/iank/.local/share/profanity/chatlogs/iank_at_fsf.org/rooms/sys-private_at_conference.fsf.org - logs=+(*) - logs=( $( printf "%s\n" ${logs[*]} | sort -u ) ) + logs+=(*) + tmps=$( printf "%s\n" ${logs[*]} | sort -u ) + mapfile -t logs <<<"$tmps" cd /home/iank/.local/share/profanity/chatlogs/iank_at_fsf.org/rooms logcount=${#logs[@]} if (( logcount > 32 )); then @@ -3665,6 +3666,7 @@ j() { } # xorg copy. copy text piped into command, or copy file(s) if given +# shellcheck disable=SC2120 # intentional xc() { xclip -r -selection clipboard "$@" } @@ -4078,11 +4080,18 @@ trans-remote-route() { : } trg() { transmission-remote-gtk & r; } -# TODO: this wont work transmission.lan doesnt exist + +## transmission cli client +# examples: +# set global upload limit to 100 kilobytes: +# trc -u 100 +# get detailed info on all torrents (note: -j would be nice but it doesn't work). +# trc -t all -i trc() { - # example, set global upload limit to 100 kilobytes: - # trc -u 100 - TR_AUTH=":$(jq -r .profiles[0].password ~/.config/transmission-remote-gtk/config.json)" transmission-remote transmission.lan -ne "$@" + + source /p/c/domain-info + d_host_suffix=$(awk '$2 == "'$d_host'" {print $1}' /p/c/host-info) + TR_AUTH="transmission:$(

/dev/null; then +if [[ $HOSTNAME == "$d_host" ]] && ! mountpoint /d &>/dev/null; then cat <<'EOFOUTER' # if this is a fresh reinstall, need to run something like this # to restore data: diff --git a/distro-end b/distro-end index a2723c0..4e82b5c 100755 --- a/distro-end +++ b/distro-end @@ -1474,6 +1474,7 @@ fi # own umask based on it's settings file. Well, no harm leaving this # so it's set right from the beginning. sudo chfn debian-transmission -o umask=0002 +sudo usermod -a -G debian-transmission iank # note i had to do this, which is persistent: # cd /i/k @@ -1513,7 +1514,7 @@ sudo chown -R debian-transmission:debian-transmission $f sudo chown -R debian-transmission:debian-transmission /var/lib/transmission-daemon # # config file documented here, and it\'s the same config -# for daemon vs client, so it\'s documented in the gui. +# for daemon vs non-daemon, so it\'s documented in the gui. # https://trac.transmissionbt.com/wiki/EditConfigFiles#Options # # I originaly setup rpc-whitelist, but after using @@ -1524,15 +1525,15 @@ sudo chown -R debian-transmission:debian-transmission /var/lib/transmission-daem # It is a read & write cache. # # just fyi: default rpc port is 9091 -if ! systemctl is-active transmission-daemon-nn &>/dev/null && \ - ! systemctl is-active transmission-daemon; then - tmp=$(mktemp) - command sudo ruby <$tmp +d_host_suffix=$(awk '$2 == "'$d_host'" {print $1}' /p/c/host-info) +u ~/.config/transmission-daemon-iank.rb < false, - 'rpc-authentication-required' => false, + 'rpc-host-whitelist-enabled' => false, + 'rpc-authentication-required' => true, + 'rpc-bind-address' => '10.174.$d_host_suffix.2', 'incomplete-dir' => '$tdir/partial-torrents', 'incomplete-dir-enabled' => true, 'download-dir' => '$tdir/torrents', @@ -1542,11 +1543,30 @@ s = { "cache-size-mb" => 256, "ratio-limit" => 5.0, "ratio-limit-enabled" => false, + "umask" => "002", +$(if [[ -e /p/transmission-rpc-pass ]]; then echo '"rpc-password" => File.read("/p/transmission-rpc-pass").chomp,'; fi) } puts(JSON.pretty_generate(JSON.parse(File.read(p)).merge(s))) EOF - cat $tmp | sudo dd of=/etc/transmission-daemon/settings.json - +# shellcheck disable=SC2154 # false positive +if $ur || ! sudo test -s /etc/transmission-daemon/settings.json; then + restart_ser=false + ser=transmission-daemon-nn + if ser is-active $ser &>/dev/null; then + m ser stop $ser + restart_ser=true + fi + tmpf=$(mktemp) + # shellcheck disable=SC2024 # false positive + if sudo ruby <~/.config/transmission-daemon-iank.rb >$tmpf; then + sudo dd of=/etc/transmission-daemon/settings.json <$tmpf + else + echo "iank ruby transmission error" >&2 + exit 1 + fi + if $restart_ser; then + m ser start $ser + fi fi ####### end transmission @@ -1589,18 +1609,6 @@ if [[ -e /p/transmission-rpc-pass ]]; then # auto-connect # password - # the password is randomly generated on first run, i copied it out - # so it could be used by other hosts. - sudo ruby <<'EOF' -require 'json' -p = '/etc/transmission-daemon/settings.json' -s = JSON.parse(File.read(p)) -s["rpc-password"] = File.read("/p/transmission-rpc-pass").chomp -# default is 0022 (18 in decimal) -s["umask"] = 2 -File.write p, JSON.pretty_generate(s) -EOF - rpc_pass=$(

/dev/null ||: # ignore fail in bash < 4.4 set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR +slice_dir=/sys/fs/cgroup/system.slice +if [[ $1 == -p ]]; then + dir="$2" + shift 2 + slice_dir=$slice_dir/$dir +fi + +sleep 1 unit="$1" -pid=$(head -n1 /sys/fs/cgroup/system.slice/$unit.service/cgroup.procs) +pid=$(head -n1 "$slice_dir"/$unit.service/cgroup.procs) ns=$(readlink /proc/$pid/ns/net) default_ns=$(readlink /proc/1/ns/net) diff --git a/filesystem/usr/local/bin/prof b/filesystem/usr/local/bin/prof index 2a1c7ab..ee37bd4 100755 --- a/filesystem/usr/local/bin/prof +++ b/filesystem/usr/local/bin/prof @@ -29,7 +29,7 @@ if (( $# >= 1 )); then remote=$1 else remote=prof - if systemctl --user --quiet is-active profanity || [[ $HOSTNAME == $d_host ]]; then + if systemctl --user --quiet is-active profanity || [[ $HOSTNAME == "$d_host" ]]; then dossh=false fi fi diff --git a/filesystem/usr/local/bin/prof-backup b/filesystem/usr/local/bin/prof-backup index b57625c..369f9fa 100755 --- a/filesystem/usr/local/bin/prof-backup +++ b/filesystem/usr/local/bin/prof-backup @@ -53,7 +53,7 @@ fi source /p/c/domain-info -if [[ $HOSTNAME != $d_host ]]; then +if [[ $HOSTNAME != "$d_host" ]]; then shell_pre="ssh iank@b8.nz" rsync_pre=b8.nz: fi diff --git a/filesystem/usr/local/bin/rootsshsync b/filesystem/usr/local/bin/rootsshsync index 36fc19e..278aad7 100755 --- a/filesystem/usr/local/bin/rootsshsync +++ b/filesystem/usr/local/bin/rootsshsync @@ -63,7 +63,8 @@ if [[ -e $user_ssh_dir/config ]]; then # eg, in an ssh shell. confirm for regular user provides some protection # that a rouge user program cant use my ssh key. # - # HOME is not set in systemd services + # note: HOME is not set in systemd services + # shellcheck disable=SC2016 # intentional sed 's,^AddKeysToAgent confirm,AddKeysToAgent yes,;s,\${HOME},/root,g;/^UserKnownHostsFile /d' $user_ssh_dir/config >/root/.ssh/confighome # having a different control path avoids the problem of # forgetting to use confighome, and then after specifying it, diff --git a/filesystem/usr/local/bin/umount-funcs b/filesystem/usr/local/bin/umount-funcs index 8e3f68c..a7fce82 100644 --- a/filesystem/usr/local/bin/umount-funcs +++ b/filesystem/usr/local/bin/umount-funcs @@ -77,7 +77,7 @@ kill-dir() { local pids i sig first_pid if ! (( ${#kill_dirs[@]} >= 1 )); then - echo "kill-dir: error: ${kill_dirs[@]} is empty!" + echo "kill-dir: error: \${kill_dirs[@]} is empty!" exit 1 fi @@ -117,7 +117,7 @@ umount-kill() { local umount_kill_dir killed_dir for umount_kill_dir; do if ! mountpoint -q $umount_kill_dir; then - echo "expected mountpoint args, got $dir" + echo "umount-kill: expected mountpoint arg, got: $umount_kill_dir" return 1 fi done @@ -140,8 +140,10 @@ umount-kill() { umount_ret=true for killed_dir in ${kill_dirs[@]}; do if ! m umount -R $killed_dir; then - echo "$0: ERROR: failed to umount $dir." >&2 + echo "$0: ERROR: failed to umount killed_dir:$killed_dir." >&2 + # shellcheck disable=SC2034 # false positive umount_ret=false + # shellcheck disable=SC2034 # false positive ret=1 fi done diff --git a/g b/g index c6a037e..a88df4b 100755 --- a/g +++ b/g @@ -67,6 +67,8 @@ g() { sleep 1 cd "/a/opt/emacs-$(distro-name)$(distro-num)" sudo gdb -p "$(pgrep -f 'emacs --daemon')" -ex c + # note: this is dead code since this is called as a script. + # shellcheck disable=SC2103 # preference for simplicity. cd - else emacsclient -a "" $args "$@" diff --git a/i3-sway/common.conf b/i3-sway/common.conf index dfb6015..b7c95f0 100644 --- a/i3-sway/common.conf +++ b/i3-sway/common.conf @@ -63,7 +63,7 @@ bindsym $mod+e $ex "i3-emacs" #bindsym $mod+shift+e bindsym $mod+r $ex "/a/bin/ds/xl" -bindsym $mod+backslash $ex "gnome-screenshot" +bindsym $mod+backslash $ex "scrot" bindsym $mod+t $ex "i3-set-layout splitv" diff --git a/mail-setup b/mail-setup index bae2d81..054a276 100755 --- a/mail-setup +++ b/mail-setup @@ -2098,7 +2098,7 @@ JoinsNamespaceOf=mailnn.service StartLimitIntervalSec=0 [Service] -ExecStartPre=/usr/local/bin/joins-namespace-of-check mailnn +ExecStartPre=+/usr/local/bin/joins-namespace-of-check mailnn PrivateNetwork=true BindPaths=$bindpaths Restart=always diff --git a/pkgs b/pkgs index d3a74ae..bf104a7 100644 --- a/pkgs +++ b/pkgs @@ -137,6 +137,7 @@ p3=( # better du in t11+ duf duplicity + dwdiff elinks evince # used by digikam, it complains in stdout if its not there. diff --git a/stream-clip b/stream-clip index 267ca31..3a4d12d 100755 --- a/stream-clip +++ b/stream-clip @@ -54,7 +54,8 @@ case $type in shuf | head -n1) echo $clip >/tmp/last-$type else - clip=./* + # quoted just to satisfy shellcheck + clip="./*" fi ;; esac