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
unset counter
unset linenumber
}
function getpoem {
#get number of lines in file
counter=`wc -l < $poems`
#get random line number
linenumber=$[ ( $RANDOM % $counter ) + 1 ]
#get the line
poem=`sed -n "${linenumber}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 to your bash profile:
. ~/.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
I've added an example poem file.
"The summer grasses: / of mighty warlords' visions / all that they have left." / [Basho]
"On my travels, stricken-- / my dreams over the dry land / go on roving." / [Basho]
"Ancient pond / Frog leaps / Splash!" / [Basho]
"Over the wintry / forest, winds howl in rage / with no leaves to blow." / [Soseki]
"A sudden shower falls - / and naked I am riding / on a naked horse!" / [Issa]
"Green frog / is your body also / freshly painted?" / [Akutagawa]
"Spring water in the green creek is clear / Moonlight on Cold Mountain is white / Silent knowledge - the spirit is enlightened of itself / Contemplate the void: this world exceeds stillness." [Han Shan]
"In my first thirty years of life / I roamed hundreds and thousands of miles. / Walked by rivers through deep green grass / Entered cities of boiling red dust. / Tried drugs, but couldn't make Immortal; / Read books and wrote poems on history. / Today I'm back at Cold Mountain: / I'll sleep by the creek and purify my ears." [Han Shan]
"Once at Cold Mountain, troubles cease - / No more tangled, hung up mind. / I idly scribble poems on the rock cliff, / Taking whatever comes, like a drifting boat." [Han Shan]
"On top of Cold Mountain the lone round moon / Lights the whole clear cloudless sky. / Honor this priceless natural treasure / Concealed in five shadows, sunk deep in the flesh." [Han Shan]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment