Random: Add random string generator.

This commit is contained in:
Dmitry Voronin 2024-11-13 11:06:28 +03:00
parent fb0050a0a6
commit 77bfe23b2b
Signed by: voronind
SSH key fingerprint: SHA256:3kBb4iV2ahufEBNq+vFbUe4QYfHt98DHQjN7QaptY9k

View file

@ -1,5 +1,16 @@
{ ... }: {
text = ''
# Generate random string.
# Usage: random <LENGTH>
function random() {
local length="''${1}"
if [[ "''${length}" = "" ]]; then
help random
return 2
fi
head /dev/urandom | tr -dc A-Za-z0-9 | head -c''${length}
}
# Picks a random file or directory.
function random_file() {
local IFS=$'\n'
@ -8,7 +19,7 @@
((total--))
local index=$(shuf -i 0-''${total} -n 1)
echo ''${dirs[$index]}
printf "%s" ''${dirs[$index]}
}
'';
}