From 77bfe23b2b2fb0224e6f77066d6077ee51d5e5b8 Mon Sep 17 00:00:00 2001 From: Dmitry Voronin Date: Wed, 13 Nov 2024 11:06:28 +0300 Subject: [PATCH] Random: Add random string generator. --- home/program/bash/module/Random.nix | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/home/program/bash/module/Random.nix b/home/program/bash/module/Random.nix index 8346d43..b60b923 100644 --- a/home/program/bash/module/Random.nix +++ b/home/program/bash/module/Random.nix @@ -1,5 +1,16 @@ { ... }: { text = '' + # Generate random string. + # Usage: random + 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]} } ''; }