I have some unit tests that need an X11 display so I plan to start Xvfb before running them, but to start Xvfb I will need a free display number to connect it to. My best guess
Why not exploit the fact that every X11 server puts a "Lock" file into /tmp?
This is called /tmp/.Xn-lock where "n" is the Display id. (Also note the leading . in the filename).
This is the mechanism that Xserver itself uses to check for duplication, and it appears to be consistent on all *nix platforms I have tried (HP-UX, Linux, ...)
So you could adapt your script thus (forgive me for syntax errors, I'm nore accustomed to C shell than Bourne/Korn shell scripting)
DISPLAY_NUM=0
do
if ( -e /tmp/.X$DISPLAY_NUM-lock ) then
let DISPLAY_NUM=$DISPLAY_NUM+1
else
Xvfb :$DISPLAY_NUM -screen 0 1280x1024x24 -ac (or whatever args take your fancy)
fi
done