Skip to content

Instantly share code, notes, and snippets.

@2bard
Created July 18, 2012 20:09
Show Gist options
  • Save 2bard/3138553 to your computer and use it in GitHub Desktop.
Save 2bard/3138553 to your computer and use it in GitHub Desktop.
Bash poem echo
function unsetvars {
unset word
unset poem
unset poems
unset line
}
function getpoem {
#get number of lines in file
counter=`wc -l < $poems`
#get random line number
NUMBER=$[ ( $RANDOM % $counter ) + 1 ]
#get the line
poem=`sed -n "${NUMBER}p" < $poems`
}
function echopoem {
echo
#go through each word
for word in $poem; do
#if the word isn't a newline character, add it to the output
if [ "${word}" == "/" ];
then
echo $line;
unset line;
else
line=$line" ${word}";
fi
done
echo $line;
echo
}
function printhaiku {
unsetvars
poems=$1
getpoem
echopoem
unsetvars
}
A simple bash function to display a poem.
Pull the function from the .cfg into your .bash_profile by adding this line:
. ~/.haiku.cfg
Poems are to be stored in a textfile with one line per poem. A forward slash indicated a new line e.g.
This is line 1 of the first poem / This is line 2 of the first poem
This is line 1 of the second poem / This is line 2 of the second poem
The bash function grabs a random poem and echoes it out line-by-line.
The function is called 'printhaiku', it takes one parameter: the poem file.
In my .bash_profile I have put the following line to display a poem every time I open a new shell:
printhaiku './haiku.txt'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment