[Pacemaker] [PATCH 2 of 2] low: remove various bashisms

Simon Horman horms at verge.net.au
Sun Jul 11 21:01:08 EDT 2010


Hi Lars,

On Fri, Jul 09, 2010 at 05:06:10PM +0200, Lars Ellenberg wrote:
> On Fri, Jul 09, 2010 at 04:05:13PM +0200, Dejan Muhamedagic wrote:
> > Hi,
> > 
> > On Thu, Jul 08, 2010 at 03:16:03PM +0900, Simon Horman wrote:
> > > # HG changeset patch
> > > # User Simon Horman <horms at verge.net.au>
> > > # Date 1278569313 -32400
> > > # Node ID 48a51108d0d181ecb21c3289d3bc86b46f77f622
> > > # Parent  110d056193472fa64ffabd3069d5ed20d32b01c2
> > > low: remove various bashisms
> > > 
> > > As reported by Debian's devscripts's checkbashisms.
> > > 
> > > Signed-off-by: Simon Horman <horms at verge.net.au>
> > > 
> > > diff -r 110d05619347 -r 48a51108d0d1 ConfigureMe
> > > --- a/ConfigureMe	Thu Jul 08 15:06:00 2010 +0900
> > > +++ b/ConfigureMe	Thu Jul 08 15:08:33 2010 +0900
> > > @@ -294,7 +294,7 @@
> > >  	;;
> > >    distcheck)			
> > >    	do_configure $FLAGS $@ && \
> > > -	source ./heartbeat/lib/ha_config && \
> > > +	. ./heartbeat/lib/ha_config && \
> > >  	Run $MAKE_CMD DESTDIR="$PWD/heartbeat-$VERSION/=inst" distcheck
> > >  	;;
> > >    pkg|package|rpm|deb|dpkg)	
> > > diff -r 110d05619347 -r 48a51108d0d1 extra/resources/SysInfo
> > > --- a/extra/resources/SysInfo	Thu Jul 08 15:06:00 2010 +0900
> > > +++ b/extra/resources/SysInfo	Thu Jul 08 15:08:33 2010 +0900
> > > @@ -107,7 +107,7 @@
> > >  UpdateStat() {
> > >      name=$1; shift
> > >      value="$*"
> > > -    echo -e "$name:\t$value"
> > > +    printf "%s:\t%s\n" "$name" "$value"
> > >      ${HA_SBIN_DIR}/attrd_updater ${OCF_RESKEY_delay} -S status -n $name -v "$value"
> > >  }
> > >  
> > > @@ -123,19 +123,15 @@
> > >  	    mem=`SysInfo_mem_units $mem`
> > >  	    mem_used=`SysInfo_mem_units $mem_used`
> > >  	    mem_total=`expr $mem_used + $mem`
> > > -	    cpu_type=`system_profiler SPHardwareDataType | grep "CPU Type:"`
> > > -	    cpu_type=${cpu_type/*: /}
> > > -	    cpu_speed=`system_profiler SPHardwareDataType | grep "CPU Speed:" | awk '{print $3}'`
> > > -	    cpu_cores=`system_profiler SPHardwareDataType | grep "Number Of"`
> > > -	    cpu_cores=${cpu_cores/*: /}
> > > +	    cpu_type=`system_profiler SPHardwareDataType | awk 'BEGIN {FS=": "} /^CPU Type/ {print $2; exit}'`
> > 
> > Why not:
> > 
> > +	    cpu_type=`system_profiler SPHardwareDataType | awk -F": " '/^CPU Type/ {print $2; exit}'`
> 
> # because its friday, I'll give you some irrelevant input,
> # doing it all in one pipe.  feel free to ignore ;-)
> 
> eval $(
> printf "Garbage ignored\nCPU Type: speedy\nCPU Speed: gonzales\nNumber Of Cores: you won't guess\nOther information: ignored\n"  |
> sed -n -e "
> 	s/^CPU Type: /cpu_type=/
> 	s/^CPU Speed: /cpu_speed=/
> 	s/^Number Of.*: /cpu_cores=/
> 	t shell_escape_and_print
> 	b
> 	: shell_escape_and_print
> 	s/'/'\\\\''/g
> 	s/=/='/
> 	s/\$/'/
> 	p
> 	"
> )
> echo "$cpu_type"
> echo "$cpu_speed"
> echo "$cpu_cores"

Can it be simplified slightly like this?

eval $(
printf "Garbage ignored\nCPU Type: speedy\nCPU Speed: gonzales\nNumber Of Cores: you won't guess\nOther information: ignored\n"  |
sed -n -e "
	s/^CPU Type: /cpu_type=/
	s/^CPU Speed: /cpu_speed=/
	s/^Number Of.*: /cpu_cores=/
	T
	s/'/'\\\\''/g
	s/=/='/
	s/\$/'/
	p
	"
)
echo "$cpu_type"
echo "$cpu_speed"
echo "$cpu_cores"

> 
> speedy
> gonzales
> you won't guess
> 
> 
> same thing works for model name, bogomips, processor from /proc/cpuinfo:
> eval $(
> sed -n -e "
> 	s/^model name[^:]*: /cpu_type=/;
> 	s/^bogomips[^:]*: /cpu_speed=/;
> 	s/^processor[^:]*: */cpu_cores=/;
> 	t shell_escape_and_print;
> 	b
> 	: shell_escape_and_print;
> 	s/'/'\\\\''/g
> 	s/=/='/
> 	s/\$/'/
> 	p
> 	" < /proc/cpuinfo
> )
> # processor : X  in cpuinfo starts with X=0
> cpu_cores=$(( $cpu_cores + 1 ))
> 
> echo "$cpu_type"
> echo "$cpu_speed"
> echo "$cpu_cores"

I'm much more of a sed fan than an awk fan, and my initial instinct
was to do something like what you have above. I chose to use awk
as it was already in use in the script, and the changes seemed
less invasive.

I think that the best thing would be to get my fixes in
(modulo any fixes that come out of the discussion with Dejan)
and then if you're keen, put your changes on top.

OTOH, while this code is almost certainly faster,
I'm not conviced this script is performance-critical.

Your call :-)





More information about the Pacemaker mailing list