Thursday May 12, 2005

More Beer !

Somewhat inspiried by the python beer.py script, and more so by Brendan Gregg's dtrace'd Guessing Game, heres some beer thanks to dtrace.

#!/usr/sbin/dtrace -s

BEGIN
{
        n = 10;
        ifor = 1;
}

tick-1sec
/!n && !ifor/
{
        printf("no more bottles of beer on the wall");
        exit(0);
}

tick-1sec
/n == 1 && !ifor/
{
        printf("one more bottle of beer on the wall");
        ifor = 1;
}

tick-1sec
/n > 1 && !ifor/
{
        printf("%d bottles of beer on the wall", n);
        ifor = 1;
}

tick-1sec
/ifor/
{
        n--;
        ifor = 0;
}

Dtrace can't do loops (for good reason), but this script shows an example of getting something similar out of it..

# dtrace -s beer.d   
dtrace: script 'beer.d' matched 5 probes
CPU     ID                    FUNCTION:NAME
  0  39815                       :tick-1sec 9 bottles of beer on the wall
  0  39815                       :tick-1sec 8 bottles of beer on the wall
  0  39815                       :tick-1sec 7 bottles of beer on the wall
  0  39815                       :tick-1sec 6 bottles of beer on the wall
  0  39815                       :tick-1sec 5 bottles of beer on the wall
  0  39815                       :tick-1sec 4 bottles of beer on the wall
  0  39815                       :tick-1sec 3 bottles of beer on the wall
  0  39815                       :tick-1sec 2 bottles of beer on the wall
  0  39815                       :tick-1sec one more bottle of beer on the wall
  0  39815                       :tick-1sec no more bottles of beer on the wall

#