puzzle

martin.zam marz at cbnewsm.att.com
Wed Nov 28 05:41:04 AEST 1990


In article <1990Nov27.003659.3521 at informix.com>, housed at infmx.informix.com (Darryl House) writes:
> The following is a Bourne shell script that illustrates
> what I am trying to do: set a shell variable that
> contains the contents of another one that is referred
> to by concatenating two others. Sufficiently confusing?
> Yeah, I thought so, too.
> 
> --------------------------------------------------------
> #! /bin/sh
> 
> # what I want is to be able to set
> # the ITERATION variable to be the expanded
> # version of $PREFIX$SUFFIX, i.e. the first iteration
> # would be the contents of $firsttime, the second
> # would be the contents of $secondtime. The following
> # code gives me errors, and everything I try either gets
> # the same substitution failure or just echoes the name
> # of the variable, i.e. ($firsttime and $secondtime).
> 
> echo
> 
> firsttime="first_time"
> secondtime="second_time"
> 
> PREFIX_WORDS="first second"
> SUFFIX="time"
> 
> for PREFIX in $PREFIX_WORDS
> do
> 
> # the following line doesn't work, but
> # sort of illustrates what I want to do.
> # I want this to be ITERATION=$firsttime the first time
> # through and ITERATION=$secondtime the second time.
> 
> 	ITERATION=${$PREFIX$SUFFIX}
> 
>     echo 'Iteration is $ITERATION'
> done
> 
> echo
> 
> exit 0
> --------------------------------------------------------
> 
> Required output:
> 
> prompt% program_name
> 
> Iteration is first_time
> Iteration is second_time
> 
> prompt%
> 
> Any hints you can offer will be greatly appreciated.

Try this...

firsttime="first_time"
secondtime="second_time"

PREFIX_WORDS="first second"
SUFFIX="time"

for PREFIX in ${PREFIX_WORDS}
do
	ITERATION="${PREFIX} ${SUFFIX}"	# These variables need to be
					# expanded individually, and the
					# quotes preserve the space
					# between them.

	echo "Iteration is $ITERATION"	# Your single quotes didn't allow
					# variable expansion to take place.
done

						Hope this helps,
						Martin Zam
						(201)564-2554



More information about the Comp.unix.internals mailing list