Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

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

VersionAuthorDateCommentsRate
No loop, no recursionFrédéric Lang07/08/083
Self WritingOlosta07/18/120
Bourne Again ShellDave Plonka04/20/055
portable, rich of features, readableBastian Bittorf08/20/070
with arrays and functionsVittorio Cagnetta06/30/060
Arithmetic on English words for numbersBill Brown07/31/080

Comments

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!

Name:

eMail:

URL:

Security Code:
  
Comment: