plugins/base: `pickfrom()`

pull/1930/head
John D Pell 2021-09-23 22:52:15 -07:00
parent c975f31899
commit c8ed9a9da8
1 changed files with 5 additions and 3 deletions

View File

@ -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
}