Skip to content

Instantly share code, notes, and snippets.

@amorphid
Created September 15, 2016 00:45
Show Gist options
  • Save amorphid/4aee25a44303e7e15c1e0e2f0665440c to your computer and use it in GitHub Desktop.
Save amorphid/4aee25a44303e7e15c1e0e2f0665440c to your computer and use it in GitHub Desktop.
Bash UUID
#!/bin/bash
# Couldn't find a pure Bash utility for UUIDs, so I wrote this for fun (it ain't fast)
base_36_char () {
(
HIGH=${1:-15}
LOW=${2:-0}
RAND_NUM=$RANDOM
for i in $(seq $HIGH $LOW); do
if (( $RAND_NUM > ((32767/16)*$i) )); then
echo "obase=16;${i}" | bc
break
fi
done
)
}
uuid_chars () {
(
TIMES=$1
HIGH=${2:-15}
LOW=${3:-0}
CHARS=""
for i in $(seq 1 $TIMES); do
CHARS+=$(base_16_char $HIGH $LOW)
done
echo $CHARS
)
}
echo "$(uuid_chars 8)-$(uuid_chars 4)-4$(uuid_chars 3)-$(uuid_chars 1 11 8)$(uuid_chars 3)-$(uuid_chars 12)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment