Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable used to set a variable

Status
Not open for further replies.

denisl

Technical User
Jun 18, 2001
36
US
Hi,
I'm trying to use a variable to initialize a new variable in a for loop. Is this possible? If not any other way in ksh to do this?

for x in main1 main2
do
${x}string1=$(grep string1 file1 | wc -l)
${x}string2=$(grep string2 file1 | wc -l)
done

The variables I was hoping to define are as follows:
$main1string1
$main1string2
$main2string1
$main2string2



Thanks, Denis
 
In your ksh man page have a look to the eval shell builtin.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,
Thanks for you reply.

I don't see how the eval built in will help me.

How would I use eval?

Thanks, Denis
 
eval makes the shell evaluate the command line twice, including parameter substitution, process substition, etc., e.g.:
Code:
#!/bin/ksh
for x in main1 main2
do
        eval ${x}string1='$(grep string1 file1 | wc -l)'
        eval ${x}string2='$(grep string2 file1 | wc -l)'
done

The first time it evaluates the command, the result is:
Code:
$main1string1=$(grep string1 file1 | wc -l)

Then the second time it evaluates as normal.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top