From c8ed9a9da80ed48d5bad2b8217cc5c1774c02a78 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 23 Sep 2021 22:52:15 -0700 Subject: [PATCH] plugins/base: `pickfrom()` --- plugins/available/base.plugin.bash | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/available/base.plugin.bash b/plugins/available/base.plugin.bash index c10771a7..dbbac5a8 100644 --- a/plugins/available/base.plugin.bash +++ b/plugins/available/base.plugin.bash @@ -44,9 +44,11 @@ function pickfrom() { param '1: filename' example '$ pickfrom /usr/share/dict/words' group 'base' - local file=$1 - [[ -z "$file" ]] && reference $FUNCNAME && return - local -i length="$(wc -l "$file")" + local file=${1:-} + if [[ ! -r "$file" ]]; then + reference "${FUNCNAME[0]}" && return + fi + local -i length="$(wc -l < "$file")" n=0 n=$(( RANDOM * length / 32768 + 1 )) head -n $n "$file" | tail -1 }