Created
June 9, 2014 15:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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