Language bash
(recursive function)
Date: | 12/30/07 |
Author: | Koen Noens |
URL: | n/a |
Comments: | 0 |
Info: | http://www.gnu.org/software/bash/ |
Score: | (2.67 in 6 votes) |
#!/bin/bash ## Koen Noens, december 2007 ## Lyrics for "99 bottles of beer" # # GNU bash shell # functions (can be recursive) # built-in string manipulation # built-in arithmetic operations # function drinkBeer() { howmany=$1 txt1=" of beer on the wall" txt2="take one down, pass it around" txt3="go to the shop and buy some more" # sing bottles $howmany ; echo "$txt1" bottles $howmany ; echo "${txt1:0:9}" # continue drinking or go to the shop ? if [[ howmany -gt 0 ]]; then howmany=$(($1 - 1)) nowwhat=$txt2 else howmany=99 nowwhat=$txt3 STOP="x" #comment this to continue endlessly fi # keep drinking echo "$nowwhat" bottles $howmany ; echo "$txt1" ; echo -e [[ "$STOP" == "x" ]] || drinkBeer $howmany # last line has test ([[ ]]) to avoid endless loop # whitout it, song just restarts with 99 bottles, forever. } # manage syntax and spelling correction function bottles() { case $1 in 1) echo -n "one bottle" ;; 0) echo -n "no more bottles" ;; *) echo -n "$1 bottles" ;; esac } # start song drinkBeer 99
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
No loop, no recursion | Frédéric Lang | 07/08/08 | 3 | |
Self Writing | Olosta | 07/18/12 | 0 | |
Bourne Again Shell | Dave Plonka | 04/20/05 | 5 | |
portable, rich of features, readable | Bastian Bittorf | 08/20/07 | 0 | |
with arrays and functions | Vittorio Cagnetta | 06/30/06 | 0 | |
Arithmetic on English words for numbers | Bill Brown | 07/31/08 | 0 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments