dotfiles/scripts/bin/uuid

37 lines
1.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# Script Name: uuid
# Description: Generate v4 UUID (useful for security testing, tokens, identifiers)
# Source: https://evanhahn.com/scripts-i-wrote-that-i-use-all-the-time/
# Credit: Evan Hahn - https://codeberg.org/EvanHahn/dotfiles
# Usage: uuid # generate one UUID
# uuid 5 # generate 5 UUIDs
# uuid | pbcopy # copy to clipboard
count=${1:-1}
# Try multiple methods (in order of preference)
generate_uuid() {
# Method 1: Python (most portable)
if command -v python3 &>/dev/null; then
python3 -c 'import uuid; print(uuid.uuid4())'
# Method 2: Ruby
elif command -v ruby &>/dev/null; then
ruby -e "require 'securerandom'; puts SecureRandom.uuid"
# Method 3: uuidgen (available on many systems)
elif command -v uuidgen &>/dev/null; then
uuidgen | tr '[:upper:]' '[:lower:]'
# Method 4: /proc/sys/kernel/random/uuid (Linux)
elif [[ -r /proc/sys/kernel/random/uuid ]]; then
cat /proc/sys/kernel/random/uuid
else
echo "Error: No UUID generation method available" >&2
echo "Install python3, ruby, or uuid-runtime package" >&2
return 1
fi
}
for ((i=1; i<=count; i++)); do
generate_uuid
done