Created
July 18, 2012 20:09
-
-
Save 2bard/3138553 to your computer and use it in GitHub Desktop.
Bash poem echo
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
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 | |
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 | |
} |
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
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