Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created June 9, 2014 15:39
Show Gist options
  • Save ttscoff/25ab96138e0a5e4537ea to your computer and use it in GitHub Desktop.
Save ttscoff/25ab96138e0a5e4537ea to your computer and use it in GitHub Desktop.
Generate random filenames using an adjective and a noun from the WordNet dictionaries
# Bash function gen_random_filename
# Description: Generates random file names
# Requires shuf (brew install coreutils)
# Requires a list of adjectives and nouns (1 per line)
gen_random_filename() {
local adjs=~/words/adjectives.txt
local nouns=~/words/nouns.txt
local adj noun title starts_with_1 starts_with_2 counter
# process arguments for "starts_with" variables
counter=1
while [[ -n $1 ]]; do
# only accept single letter (a-z) arguments
if [[ $1 =~ ^[a-zA-Z]$ ]]; then
eval starts_with_$counter="$1"
counter=$[counter + 1]
fi
shift
done
# if starts_with_1 has a value and starts_with_2 doesn't, duplicate
[[ -z $starts_with_2 && -n $starts_with_1 ]] && starts_with_2=$starts_with_1
# if shuf is installed...
if [[ $(which shuf) ]]; then
# grep for starts_with value and pick random result
# if starts_with is empty, grep will return all entries
adj=`grep --color=never -e "^$starts_with_1" $adjs | shuf -n 1`
noun=`grep --color=never -e "^$starts_with_2" $nouns | shuf -n 1`
# concatenate adjective and noun
title=`echo ${adj}_${noun}`
# otherwise, error
else
cat <<-EOS
$FUNCNAME requires the utility shuf to generate random names.
Use homebrew to install "coreutils," which includes shuf.
EOS
return
fi
echo $title
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment