nix/home/program/bash/module/Random.sh

22 lines
421 B
Bash
Raw Normal View History

2024-12-18 10:54:24 +03:00
# 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'
local dirs=($(ls))
local total=${#dirs[@]}
((total--))
local index=$(shuf -i 0-${total} -n 1)
printf "%s" ${dirs[$index]}
}