Skip to content

Instantly share code, notes, and snippets.

@dschreck
Created February 29, 2024 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dschreck/fe9b85d23ba263781c2d005e79a55127 to your computer and use it in GitHub Desktop.
Save dschreck/fe9b85d23ba263781c2d005e79a55127 to your computer and use it in GitHub Desktop.
Generate some short names for little services, devices, drives, etc etc.
#!/usr/bin/env bash
#######
# https://github.com/dschreck
#
# Generate a short name for a resource, service, disk, container, etc etc.
# Modify the adjs and nouns arrays to customize the name generation.
#
# Examples:
# $ ./bin/generate_short_name
# mystic-link
#
# $ ./bin/generate_short_name --with-numbers
# red-hub-98
#
# $ ./bin/generate_short_name -s _ -n 5
# slow_unit
# blue_array
# red_vertex
# fast_node
# bold_vault
#
#######
adjs=(
"fast" "red" "slow" "blue" "tiny" "bold" "icy" "quick" "tidy" "mystic" "cosmic"
)
nouns=(
"disk" "array" "unit" "drive" "node" "hub" "anchor" "vertex" "byte" "core" "link" "vault"
)
show_help() {
echo "Usage: $0 [options]" >&2
echo ""
echo "Options:"
echo " --with-numbers Include a numeric identifier at the end of each name."
echo " -s, --separator <char> Specify a separator character or string between name parts."
echo " -n, --number <count> Generate <count> names. Default is 1."
echo " -h, --help Display this help and exit."
echo ""
echo "Examples:"
echo " $0"
echo " $0 --with-numbers"
echo " $0 -s _ -n 5"
echo ""
}
include_numbers=false
separator="-"
count=1
while [[ "$#" -gt 0 ]]; do
case $1 in
--with-numbers) include_numbers=true ;;
-s | --separator)
separator="$2"
shift
;;
-n | --number)
count="$2"
shift
;;
-h | --help)
show_help
exit 0
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
shift
done
# Function to generate a short name
generate_short_name() {
# Random selection of adjective and noun
local adj="${adjs[$RANDOM % ${#adjs[@]}]}"
local noun="${nouns[$RANDOM % ${#nouns[@]}]}"
local name="${adj}${separator}${noun}"
if [[ "$include_numbers" = true ]]; then
# Generate a short numeric identifier, for example, between 0 and 99
local num=$((RANDOM % 100))
name="${name}${separator}${num}"
fi
echo "$name"
}
for ((i = 1; i <= count; i++)); do
generate_short_name
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment