From d4155934e8fd090b28f7283c8476670f4b83ae51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20E=2E=20H=C3=A4u=C3=9Fler?= Date: Tue, 13 Aug 2019 17:30:02 +0200 Subject: [PATCH] Improved listAndReturn() If there is nothing to $2, it prints an error message. If there is only one thing to $2, it selects it. If there are more things to $2, it functions like it did. --- lb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lb b/lb index c55a04f..fb4d471 100755 --- a/lb +++ b/lb @@ -27,11 +27,17 @@ newpost() { read -erp "Give a title for your post: [ -f "$webdir/blog/$url.html" ] && echo "There is already an existing published entry of that same name/URL." && exit $EDITOR "$webdir/blog/.drafts/$url.html" ;} -listandReturn() { printf "Listing contents of %s.\\n" "$1" - ls -rc "$1" | awk -F '/' '{print $NF}' | nl - read -erp "Pick an entry by number to $2, or press ctrl-c to cancel. " number - chosen="$(ls -rc "$1" | nl | grep -w "$number" | awk '{print $2}')" - basefile="$(basename "$chosen")" && base="${basefile%.*}" ;} +listandReturn() { + filecount=$(ls "$1" | wc -l) + [ $filecount -eq 0 ] && echo "There's nothing to $2." && exit 1 + number=1 + [ $filecount -eq 1 ] || { + ls -rc "$1" | awk -F '/' '{print $NF}' | nl + read -erp "Pick an entry by number to $2, or press Ctrl-C to cancel. " number + } + chosen="$(ls -rc "$1" | nl | grep -w "$number" | awk '{print $2}')" + basefile="$(basename "$chosen")" && base="${basefile%.*}" +} publish() { \ delete