From 24adf2b53dc3015a07aae18b178d9e9fbae03cb4 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Tue, 19 Jun 2018 08:49:49 +1000 Subject: [PATCH] Added UUID V4 --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index f168339..8cb8625 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ Any donations are appreciated, they give me the time to make this the best resou * [Use `read` as an alternative to the `sleep` command.](#use-read-as-an-alternative-to-the-sleep-command) * [Check if a program is in the user's PATH.](#check-if-a-program-is-in-the-users-path) * [Get the current date using `strftime`.](#get-the-current-date-using-strftime) + * [Generate a UUID V4.](#generate-a-uuid-v4) * [Progress bars.](#progress-bars) * [Get the list of functions from your script.](#get-the-list-of-functions-from-your-script) * [Bypass shell aliases.](#bypass-shell-aliases) @@ -1612,6 +1613,43 @@ $ printf '%s\n' "$date" Fri 15 Jun - 10:00 AM ``` +## Generate a UUID V4. + +**Example Function:** + +```sh +uuid() { + # Usage: uuid + C="89ab" + + for ((N=0;N<16;++N)); do + B="$((RANDOM%256))" + + case "$N" in + 6) printf '4%x' "$((B%16))" ;; + 8) printf '%c%x' "${C:$RANDOM%${#C}:1}" "$((B%16))" ;; + + 3|5|7|9) + printf '%02x-' "$B" + ;; + + *) + printf '%02x' "$B" + ;; + esac + done + + printf '\n' +} +``` + +**Example Usage:** + +```shell +$ uuid +d5b6c731-1310-4c24-9fe3-55d556d44374 +``` + ## Progress bars. This is a simple way of drawing progress bars without needing a for loop