rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* 99 bottles in an rc shell script.
@ 1997-04-30 22:26 Tom Culliton
  1997-05-01  1:57 ` David Luyer
  1997-05-01 16:33 ` Paul Haahr
  0 siblings, 2 replies; 9+ messages in thread
From: Tom Culliton @ 1997-04-30 22:26 UTC (permalink / raw)
  To: TimTroyR, rc

I was feeling a bit whimsical after a friend pointed out the "99
bottles of beer on the wall" page and since there was already a
version in python, rc (the Plan 9 shell) was my next choice. ;-)

Anyone on the rc list want to try for an improved version?  This one
was done strictly with builtins.  Using expr you could get rid of the
count list and other uglyness.  Anyone have other major improvements?
;-) ;-) ;-)

Tom


#!/usr/local/bin/rc
# rc shell version of 99 bottles of beer
# by Tom Culliton (culliton@clark.net)

count = (99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78
         77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56
         55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34
         33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12
         11 10 9 8 7 6 5 4 3 2 1)
bottles = bottles; one = one
for (i in $count) {
        if (~ $i 1) {bottles = bottle; one = it}
        if (! ~ $i 99) {
                echo $i $bottles 'of beer on the wall.'
                echo
        }
        echo $i $bottles 'of beer on the wall,'
        echo $i $bottles 'of beer,'
        echo 'take' $one 'down and pass it around,'
}
echo 'no more bottles of beer on the wall!'


^ permalink raw reply	[flat|nested] 9+ messages in thread
* re: 99 bottles in an rc shell script.
@ 1997-05-01  1:29 rsc
  0 siblings, 0 replies; 9+ messages in thread
From: rsc @ 1997-05-01  1:29 UTC (permalink / raw)
  To: culliton, rc

This introduces a little more code in return for
losing the count array.

#!/bin/rc
# rc shell version of 99 bottles of beer
# by Tom Culliton (culliton@clark.net)
# revised by Russ Cox (rsc@research.att.com)

bottles = bottles; one = one
digits = (9 8 7 6 5 4 3 2 1 0)
for(i in $digits) for(j in $digits) { 
	n = $i$j
	if(~ $i 0) n = $j
	if(! ~ $n 0) {
		if(~ $n 1) { bottles = bottle; one = it }
		if(! ~ $n 99) {
			echo $n $bottles 'of beer on the wall.'
			echo
		}
		echo $n $bottles 'of beer on the wall,'
		echo $n $bottles 'of beer,'
		echo 'take' $one 'down and pass it around,'
	}
}
echo 'no more bottles of beer on the wall!'



^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: 99 bottles in an rc shell script.
@ 1997-05-01  4:03 Tom Culliton
  0 siblings, 0 replies; 9+ messages in thread
From: Tom Culliton @ 1997-05-01  4:03 UTC (permalink / raw)
  To: culliton, luyer; +Cc: TimTroyR, rc

Good idea, but your awk could be simpler...

> for(i in `{awk '{BEGIN{for(i=99;i>0;i--){print i;};exit;}'}) {

for (i in `{awk 'BEGIN{for(i=99;i>0;i--)print i}'}) {

I still like the version done strictly with builtins myself. ;-)


^ permalink raw reply	[flat|nested] 9+ messages in thread
* re: 99 bottles in an rc shell script.
@ 1997-05-01  5:02 Tom Culliton
  0 siblings, 0 replies; 9+ messages in thread
From: Tom Culliton @ 1997-05-01  5:02 UTC (permalink / raw)
  To: culliton, rc, rsc

> This introduces a little more code in return for
> losing the count array.
>
> #!/bin/rc
> # rc shell version of 99 bottles of beer
> # by Tom Culliton (culliton@clark.net)
> # revised by Russ Cox (rsc@research.att.com)
>
> bottles = bottles; one = one
> digits = (9 8 7 6 5 4 3 2 1 0)
> for(i in $digits) for(j in $digits) { 
> 	n = $i$j
> 	if(~ $i 0) n = $j
> 	if(! ~ $n 0) {
> 		if(~ $n 1) { bottles = bottle; one = it }
> 		if(! ~ $n 99) {
> 			echo $n $bottles 'of beer on the wall.'
> 			echo
> 		}
> 		echo $n $bottles 'of beer on the wall,'
> 		echo $n $bottles 'of beer,'
> 		echo 'take' $one 'down and pass it around,'
> 	}
> }
> echo 'no more bottles of beer on the wall!'

This reminds me of an even more "rc like" way to do this with just
builtins... ;-) ;-) ;-)

ten = (beer beer beer beer beer beer beer beer beer beer)
* = ($ten $ten $ten $ten $ten $ten $ten $ten $ten $ten)
shift ; bottles = $#*^' bottles' ; one = one
while (! ~ $* ()) {
	echo $bottles 'of beer on the wall,' ; echo $bottles 'of beer,'
	echo 'take' $one 'down and pass it around,'
	shift ; switch ($#*) {
		case 1 ; bottles = '1 bottle' ; one = it
		case 0 ; bottles = 'no more bottles'
		case * ; bottles = $#*^' bottles'
	}
	echo $bottles 'of beer on the wall.' ; echo
}


^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: 99 bottles in an rc shell script.
@ 1997-05-01 17:14 Tom Culliton
  0 siblings, 0 replies; 9+ messages in thread
From: Tom Culliton @ 1997-05-01 17:14 UTC (permalink / raw)
  To: culliton, haahr; +Cc: TimTroyR, rc

> Why enumerate all the numbers?  Why use some external program?  Peano
> arithmetic is more than sufficient.

True, but the way you did it also takes an extra loop.  However I was 
thinking about using it to generate the output backwards, inserting each
line at the head of the list, then echoing the whole list at once. 8-o   
Fortunately it was late and I went to bed before try that. ;-)

Tom


^ permalink raw reply	[flat|nested] 9+ messages in thread
* re: 99 bottles in an rc shell script.
@ 1997-05-01 19:28 Byron Rakitzis
  0 siblings, 0 replies; 9+ messages in thread
From: Byron Rakitzis @ 1997-05-01 19:28 UTC (permalink / raw)
  To: culliton, rc, rsc

How about this way to build a list up:

#!/bin/rc

fn stack_bottles {
	count=$1
	if (~ $count *[~0-9]* || ~ $count ()) {
		echo $0: count must be a nonnegative decimal integer >[1=2]
		exit 1
	}
	bottles=()
	while (!~ $#bottles $count)
		bottles=(beer $bottles)
	echo $bottles
}

# the rest is cribbed from tom

*=`{stack_bottles $*}
bottles=$#*
one=one

while (!~ $#* 0) {
        echo $bottles 'of beer on the wall,' ; echo $bottles 'of beer,'
        echo 'take' $one 'down and pass it around,'
        shift ; switch ($#*) {
                case 1 ; bottles = '1 bottle' ; one = it
                case 0 ; bottles = 'no more bottles'
                case * ; bottles = $#*^' bottles'
        }
        echo $bottles 'of beer on the wall.' ; echo
}



^ permalink raw reply	[flat|nested] 9+ messages in thread
* re: 99 bottles in an rc shell script.
@ 1997-05-01 21:13 Mark K. Gardner
  0 siblings, 0 replies; 9+ messages in thread
From: Mark K. Gardner @ 1997-05-01 21:13 UTC (permalink / raw)
  To: rc

I had to join in the fun (even though I don't drink beer).

This one not only drinks beer, it also eats CPU. 
(You get a test mode free of charge by typing "beer <num>".
It may or may not use builtins exclusively...
depending on your philosophy.)

-- 
Mark K. Gardner (mkgardne@cs.uiuc.edu)
University of Illinois at Urbana-Champaign
Real-Time Systems Laboratory
-- 

=== File: beer ===
#!/bin/rc
# rc shell version of 99 bottles of beer
# by Tom Culliton (culliton@clark.net)
# revised by Mark Gardner (mkgardne@cs.uiuc.edu)

# Supply the path to this script...
again = `{pwd}^/beer

if (~ $#* 0) {
	count = 99
} else {
	count = $1
}

bottles = bottles; one = one
if (! ~ $count 0) {
	if (~ $count 1) {bottles = bottle; one = it}
	if (! ~ $count 99) {
		echo $count $bottles 'of beer on the wall.'
		echo
	}
	echo $count $bottles 'of beer on the wall,'
	echo $count $bottles 'of beer,'
	echo 'take' $one 'down and pass it around,'
	$again `{expr $count - 1}
} else {
	echo 'no more bottles of beer on the wall!'
}


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~1997-05-02  1:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-30 22:26 99 bottles in an rc shell script Tom Culliton
1997-05-01  1:57 ` David Luyer
1997-05-01 16:33 ` Paul Haahr
1997-05-01  1:29 rsc
1997-05-01  4:03 Tom Culliton
1997-05-01  5:02 Tom Culliton
1997-05-01 17:14 Tom Culliton
1997-05-01 19:28 Byron Rakitzis
1997-05-01 21:13 Mark K. Gardner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).