[comp.unix.shell] globbing

thoth@reef.cis.ufl.edu (Gilligan) (03/29/91)

  Using Bourne shell #!/bin/sh
on Sun3s and Sun4s, sunos 4.1.1

  I'm having a bit of trouble with globbing.  I have a script that
tries to load a background in X with nothing but a name.  It has some
special cases for backgrounds that are programs or images that deserve
special arguments, but since we have several megabytes of images and
they are scattered and classified into several directories I put some
intelligence into this thing so that it would search all known
directories to find the image if it wasn't one of the special cases.

  At the top I define some directories that contain images.

#
# set up some handy directories in this easy to edit location
#
THOTHDIR=/cis/manatee1/thoth
XBINDIR=/local/X11
IMDIR=/local/images
SRDIR=${IMDIR}/sunraster
XBDIR=${IMDIR}/xbitmaps
GIFDIR=${IMDIR}/gif


  And at the bottom of the script 

#
# if all else fails, check all directories and extensions.
#
*)
	# unknown picture.  Check to see if the file exists as anything
	# in the raster dirs or gif dirs.
	if [ -f ${PICTURE} ]; then
		${XLIMG} $* ${PICTURE}
		exit 0;
	fi
	for dir in ${SRDIR}/*/ ${GIFDIR}/*/ ${THOTHDIR}/pixmaps/ ${THOTHDIR}/rasters/; do
	    for suffix in "" ".sunras" ".sr" ".SR" ".gif" ".xpm" ".xpm2" ; do
		if [ -f ${dir}/${PICTURE}${suffix} ]; then
			if [ $verbose != 0 ]; then
			echo found ${PICTURE} as ${dir}/${PICTURE}${suffix}; fi
			${XLIMG} $* ${dir}/${PICTURE}${suffix}
			exit 0;
		fi
	    done
	done
	for dir in ${XBDIR} ${XBDIR}/HP ${THOTHDIR}/icons ; do
	    for suffix in "" ".bg" ".xbm" ; do
		if [ -f ${dir}/${PICTURE}${suffix} ]; then
			if [ $verbose != 0 ]; then
			echo found ${PICTURE} as ${dir}/${PICTURE}${suffix}; fi
			${XSETBM} ${dir}/${PICTURE}${suffix} $*
			exit 0;
		fi
	    done
	done
	echo xbackground: ${PICTURE} unknown and unfound
	exit 1
	;;
esac

exit 0


  We had a problem where one user's background would not come up.  It
turns out that there were two subdirectories of gif/ that contained
the image.  Output from -vx


+ [ -f nagel ]
+ [ -f /local/images/sunraster/*//nagel ]
+ [ -f /local/images/sunraster/*//nagel.sunras ]
+ [ -f /local/images/sunraster/*//nagel.sr ]
+ [ -f /local/images/sunraster/*//nagel.SR ]
+ [ -f /local/images/sunraster/*//nagel.gif ]
+ [ -f /local/images/sunraster/*//nagel.xpm ]
+ [ -f /local/images/sunraster/*//nagel.xpm2 ]
+ [ -f /local/images/gif/*//nagel ]
+ [ -f /local/images/gif/*//nagel.sunras ]
+ [ -f /local/images/gif/*//nagel.sr ]
+ [ -f /local/images/gif/*//nagel.SR ]
+ [ -f /local/images/gif/holdingarea//nagel.gif /local/images/gif/nagel//nagel.gif ]
test: too many arguments

  AARGH.  I need some way to glob those asterisks on the "for" line
insead of delaying it until the if ([]).  Don't tell me `echo
${SRDIR}/*/`.  I tried that and it was totally ineffective.

  Please respond by email, even if the answer is of general interest,
since I don't normally read this group.  I can shar you the entire
script if you're interested.
--
"+ That the Roman Church has never erred, nor ever, by the witness
	of Scripture, shall err to all eternity" - yeah, right.
I deal with Reality as you _don't_ understand it.