[Chester LUG] shell script (sh) : variable contains string?

Ben Arnold iamseawolf at gmail.com
Thu Sep 3 11:41:06 UTC 2009


Hey all --

As I've run out of brick wall to bang my head against, I wonder if any of you 
guys could help with a little problem I'm having in a shell (sh) script.

Basically, it's a start-up script that uses `Xvesa -listmodes` to list the 
display resolutions available. I want to use either 1024x768x[8|16|24] if it's 
there or the best available if it isn't. Getting the best is simply a case of 
using `head -n 1` to get the first line.

I set the variable as a list of resolutions fine, I just can't find a clean way 
of seeing if (1024x768x[8|16|24]) exists in the pre-defined variable. I've 
tried eval too but it doesn't like it. Maybe the BusyBox version is #crap?

Thanks for anything!
Ben

NB -- It's an sh script, not bash. If it were bash, I could do
[variable =~ text]
and all is well, but I don't want to switch it and break the other code!

----------

#!/bin/sh

# This line produces a list of resolutions like:
# 640x480x16
# 640x480x24
# 800x600x16
# etc.Xvesa -listmodes also gives out some extra stuff which we don't want,
# neither is it sorted. So we need some fairy dust magic to tidy up. It goes:
#    List all modes and messages | grep: get only those with a useful hex code 
#    awk: chop off the extra stuff | sort: highest to lowest.

export SCREEN_MODES=` Xvesa -listmodes 2>&1 | grep ^0x | awk '{ printf "%s 
%s\n", $2 }' | sort -nr  `

if [ `echo "${SCREEN_MODES}" | grep '1024x768x' ` ]; then
	echo "Found a 1024x768 resolution, setting as default."

	# Get the best one (i.e. 1024x768x[8|16|24])
	export BEST_1024=`echo "${SCREEN_MODES}" | grep '1024x768x' | head -n 1 `

	# May be ugly but the rest of the script needs NEW_SCREEN:
	export NEW_SCREEN=${BEST_1024}
	echo "Set display configuration to ${NEW_SCREEN}"

else

	export NEW_SCREEN=`echo "${AVAILABLE_SCREEN_MODES}" | head -n 1`
	echo "Standard 1024x768 resolution not found. Using highest available: 
${NEW_SCREEN}"

fi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://mailman.lug.org.uk/pipermail/chester/attachments/20090903/cacf0b33/attachment.sig>


More information about the Chester mailing list