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

(No loop, no recursion)

Date:07/08/08
Author:Frédéric Lang
URL:http://fr.lang.free.fr
Comments:3
Info:http://fr.lang.free.fr/cours
Score: (3.06 in 149 votes)
#!/bin/bash
# 
# Bourne Again shell version of 99 Bottles - No loop - No recursion
# By Frédéric Lang (http://fr.lang.free.fr)

# Memorize count initial
max=${max:-99}

# Store count actual
typeset -i count=${next:-$max}

# Calculate next count
typeset -i next=count-1

# Evaluate count
if test $count -gt 0
then
	middle="take one down, pass it around"
	case $count in
		1)
			first="one bottle"
			last="no more bottles"
			;;
		2)
			first="2 bottles"
			last="one bottle"
			;;
		*)
			first="$count bottles"
			last="$next bottles"
	esac
	order="source $0"
else
	# Last occur
	first="no more bottles"
	middle="go to the shop and buy some more"
	last="$max bottles"
	order="exit 0"
fi

# Print sing
echo "$first of beer on the wall,"
echo "$first of beer,"
echo "$middle,"
echo "$last of beer on the wall."
echo

# Next occur
$order

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
portable, rich of features, readableBastian Bittorf08/20/070
Bourne Again ShellDave Plonka04/20/055
Arithmetic on English words for numbersBill Brown07/31/080
with arrays and functionsVittorio Cagnetta06/30/060
recursive functionKoen Noens12/30/070
Self WritingOlosta07/18/120

Comments

>>  Vittorio said on 09/13/08 14:11:26

Vittorio Frédéric, why you say "no recursion" ? In the last row, variable "order" IS actually a recursion command ( "source $0" )...
My question is: could be the result the same even if you don't source the script, but simply re-launch it ( that is, order="eval $0" )? In my humble opinion yes, the result is the same, because sourcing a text executable that has in first row: "#![INTERPRETER-PATH]", is like launching it.
Anyway, your script is nice. :)

>>  yeti said on 10/27/08 02:07:02

yeti Dear Vittorio, "no recursion" is correct because "source $0" does not mimic a call. It acts like placing the same soure lines linearily at this point at the same level as the preceeding lines.

>>  Paulo Matos said on 12/18/08 03:22:07

Paulo Matos Sorry this script is recursive. Recursion is done with $order.

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: