From 0c7e499af62b2251e4479a9490259ace6892ff8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krizs=C3=A1n=20P=C3=A9ter?= Date: Sat, 23 Nov 2024 19:43:48 +0100 Subject: [PATCH] Beautified output of createFstab function --- libs/functions.sh | 63 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/libs/functions.sh b/libs/functions.sh index f23bbe8..ffbf2c1 100644 --- a/libs/functions.sh +++ b/libs/functions.sh @@ -49,34 +49,75 @@ createFstab() DIR_BOOT="/boot " fi - # Print out the header part of our fstab - printf "# Begin /etc/fstab\n\n" - printf '%-49s%-15s%-8s%-20s%-8s%-10s\n' "# File system (PARTUUID)" "mount-point" "type" "options" "dump" "fsck" - printf '#%104s\n\n' "order" + local GS=4 # Num of spaces between columns + local C0N="# " + local C1N="File system (PARTUUID)" + local C2N="mount-point" + local C3N="type" + local C4N="options" + local C5N="dump" + local C6N="fsck-order" + local C0S=${#C0N} + local C1S=${#C1N} + local C2S=${#C2N} + local C3S=${#C3N} + local C4S=${#C4N} + local C5S=${#C5N} + local C6S=${#C6N} local PUID - local FST local PAT - local PF="PARTUUID=%-40s%-15s%-8s%-20s%-8s%-10s\n" + local FST + local OPT + local DUMP=0 + local FSCK=0 local PRI=0 + local VALUES local IFS=$'\n' for L in $(lsblk -l -o MOUNTPOINT,PATH,NAME,PKNAME,PARTUUID,FSTYPE | tail -n +2) ; do PUID=$(awk '{print $5}' <<< "$L") if [ -z "$PUID" ] ; then continue fi + PUID="PARTUUID=$PUID" FST=$(awk '{print $6}' <<< "$L") - PAT=$(awk '{print $2}' <<< "$L" | sed "s|/dev/|/mnt/|g") + OPT="defaults" + DUMP=0 + FSCK=0 + if [[ $L == "$DIR_ROOT"* ]] ; then - printf "$PF" "$PUID" / "$FST" defaults 1 1 + PAT="/" + DUMP=1 + FSCK=1 elif [[ $L == "$DIR_BOOT"* ]] ; then - printf "$PF" "$PUID" /boot "$FST" noauto,defaults 0 0 + PAT="/boot" + OPT="noauto,$OPT" elif [[ $L == "[SWAP]"* ]] ; then + PAT="swap" PRI=$((PRI+1)) - printf "$PF" "$PUID" swap "$FST" "pri=$PRI" 0 0 + OPT="pri=$PRI" else - printf "$PF" "$PUID" "$PAT" "$FST" defaults 0 0 + PAT=$(awk '{print $2}' <<< "$L" | sed "s|/dev/|/mnt/|g") fi + + VALUES="$VALUES$(printf '%s' "$PUID $PAT $FST $OPT $DUMP $FSCK")\n" + + if [ ${#PUID} -gt "$C1S" ]; then C1S=${#PUID} ; fi + if [ ${#PAT} -gt "$C2S" ]; then C2S=${#PAT} ; fi + if [ ${#FST} -gt "$C3S" ]; then C3S=${#FST} ; fi + if [ ${#OPT} -gt "$C4S" ]; then C4S=${#OPT} ; fi + if [ ${#DUMP} -gt "$C5S" ]; then C5S=${#DUMP} ; fi + if [ ${#FSCK} -gt "$C6S" ]; then C6S=${#FSCK} ; fi + done + + # Print out the header part of our fstab + local FMT="%-$((C1S+GS))s%-$((C2S+GS))s%-$((C3S+GS))s%-$((C4S+GS))s%-$((C5S+GS))s%-$((C6S+GS))s\n" + printf "# Begin /etc/fstab\n\n" + printf "%-${C0S}s$FMT\n" "$C0N" "$C1N" "$C2N" "$C3N" "$C4N" "$C5N" "$C6N" + + for V in $(echo -e "$VALUES") ; do + for ((i = 1; i <= C0S; i++)) ; do printf " " ; done + xargs printf "$FMT" <<< "$V" done # Print out the closing line of fstab