Skip to content

Instantly share code, notes, and snippets.

@thapakazi
Forked from ttscoff/gen_random_filename.bash
Last active August 29, 2015 14:16
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 thapakazi/fd67475a2098342c27a5 to your computer and use it in GitHub Desktop.
Save thapakazi/fd67475a2098342c27a5 to your computer and use it in GitHub Desktop.
# Bash function gen_random_filename
# Description: Generates random two-word names with date appended
# Requires a dictionary file, easily generated with `aspell dump master > dictionary.txt`
# or grab it from https://gist.githubusercontent.com/ttscoff/55493fe89c35ec1588ba/raw/
# Requires shuf and aspell
#
# Example results:
# reissue_degraded-2015-03-12@0254
# Cascades_derivatives-2015-03-12@0255
# oxygens_soaked-2015-03-12@0256
# leaderships_neutralized-2015-03-12@0306
DICT_FILE=dictionary.txt
DATE_APPEND=${1-yes}
check_pkgs(){
if [[ ! $(which shuf) ]] && [[ ! $(which aspell) ]] ; then
cat <<-EOS
$FUNCNAME requires the utilities:
aspell :to generate random names
shuf (or gshuf) :to shuffle words
... please install them first.
pkg_name: coreutils, aspell
EOS
fi
}
populate_dict(){
if [ ! -f $DICT_FILE ]; then
echo 'generating dictionary file to shuffle'
aspell dump master > dictionary.txt
fi
}
gen_random_name() {
local randwords
local title
randwords=`shuf -n 2 $DICT_FILE|sed 's/[^a-zA-Z]//g'`
title=`echo $randwords|tr ' ' '_'`
if [ "$DATE_APPEND" = yes ]; then
echo $title-$(date +'%F@%I%M')
else
echo $title
fi
}
check_pkgs
populate_dict
gen_random_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment