changeset 631:ce0616aecc6c

common.subr: "_get_mounts_at_directory()" not accepts an optional "-r" option for reverse output
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 24 Sep 2024 18:55:17 +0200
parents 87d37db3ac06
children 3aeb21629c27
files share/local-bsdtools/common.subr
diffstat 1 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/common.subr	Tue Sep 24 18:49:56 2024 +0200
+++ b/share/local-bsdtools/common.subr	Tue Sep 24 18:55:17 2024 +0200
@@ -418,6 +418,9 @@
 #: Args:
 #:   $1: the directory where to start for mounts and sub-mounts
 #:
+#: Other Parameters:
+#:   -r (optional): If given the the output is sorted in reverse order.
+#:
 #: Output (stdout):
 #:   The sorted list (lines) of mounts in :manpage:`fstab(5)` format.
 #:   This list may be empty.
@@ -431,8 +434,23 @@
 #:
 _get_mounts_at_directory() {
     local _directory
+    local _opt_reversed
 
-    local _fstab _mp1 _mp2
+    local _opt _fstab _mp1 _mp2
+
+    _opt_reversed=""
+    while getopts "r" _opt ; do
+        case ${_opt} in
+            r)
+                _opt_reversed="--reverse"
+                ;;
+            \?)
+                fatal 2 "invalid option given"
+                ;;
+        esac
+    done
+    shift $((OPTIND-1))
+    OPTIND=1
 
     _directory=${1-}
     case "${_directory}" in
@@ -461,13 +479,13 @@
         /sbin/mount -p --libxo=json,no-locale \
         | LC_ALL=C "${JQ}" -r $'.mount.fstab[] | [.device, .mntpoint, .fstype, .opts, .dump, .pass] | @tsv ' \
         | LC_ALL=C /usr/bin/awk -F $'\\t+' -v OFS=$'\t' -v mp1="${_mp1}" -v mp2="${_mp2}" $'{ if (($2 == mp1) || (index($2, mp2) == 1)) { print; } }' \
-        | LC_ALL=C /usr/bin/sort --field-separator=$'\t' --key=2
+        | LC_ALL=C /usr/bin/sort --field-separator=$'\t' --key=2 ${_opt_reversed}
     else
         # Check for unexpected spaces
         if ! check_for_proper_fstab; then
             fatal 1 "Unexpected spaces in fstab. Please install \`${JQ}'."
         fi
-        _fstab="$(/sbin/mount -p | LC_ALL=C awk -v OFS=$'\t' -v mp1="${_mp1}" -v mp2="${_mp2}" $'{ if (($2 == mp1) || (index($2, mp2) == 1)) { print $1, $2, $3, $4, $5, $6; } }' | LC_ALL=C /usr/bin/sort --field-separator=$'\t' --key=2)"
+        _fstab="$(/sbin/mount -p | LC_ALL=C awk -v OFS=$'\t' -v mp1="${_mp1}" -v mp2="${_mp2}" $'{ if (($2 == mp1) || (index($2, mp2) == 1)) { print $1, $2, $3, $4, $5, $6; } }' | LC_ALL=C /usr/bin/sort --field-separator=$'\t' --key=2 ${_opt_reversed})"
         printf '%s' "${_fstab}"
     fi
 }