From 8e7f56b80faaa118c964709e9f3e440ca7959a6c Mon Sep 17 00:00:00 2001 From: Curtis Doty Date: Sun, 15 Dec 2024 16:46:18 -0800 Subject: [PATCH] De-spaghettifi shell code - reduce excesive subshells/piplines - improve readability - speed up performance - fixes #397 --- syno_hdd_db.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/syno_hdd_db.sh b/syno_hdd_db.sh index e68bb26..a2716ed 100755 --- a/syno_hdd_db.sh +++ b/syno_hdd_db.sh @@ -378,11 +378,24 @@ echo "Running from: ${scriptpath}/$scriptfile" # Warn if script located on M.2 drive -scriptvol=$(echo "$scriptpath" | cut -d"/" -f2) -vg=$(lvdisplay | grep /volume_"${scriptvol#volume}" | cut -d"/" -f3) -md=$(pvdisplay | grep -B 1 -E '[ ]'"$vg" | grep /dev/ | cut -d"/" -f3) -# shellcheck disable=SC2002 # Don't warn about "Useless cat" -if cat /proc/mdstat | grep "$md" | grep -q nvme; then +get_script_vol() { + local script_root vol_num vg_name + script_root="${scriptpath#/*}" + script_root="${script_root%%/*}" + if [[ $script_root =~ ^volume ]] + then + vol_num="${script_root:6}" + vg_name=$(lvs --noheadings --select=lv_name="volume_$vol_num" --options=vg_name) + vg_name="${vg_name// }" + vol_name=$(pvs --noheadings --select=vg_name="$vg_name" --options=pv_name) + vol_name="${vol_name// }" + else + vol_name=$(df --output=source "/$script_root" |sed 1d) + fi +} +get_script_vol # sets $vol_name to /dev/whatever +if grep -qE "^${vol_name#/dev/} .+ nvme" /proc/mdstat +then echo -e "\n${Yellow}WARNING${Off} Don't store this script on an NVMe volume!" fi