How to change variable name depending on iteration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change variable name depending on iteration
# 1  
Old 04-15-2010
How to change variable name depending on iteration

Hi,

I would like to get a solution to this situation if possible. In a "for" loop how can I assign a value to a variable and then change the name of that variable depending on the loop iteration?

So what I am looking for is something like this in pseudo-code:

for i in 1 2 3 4
do
echo "Please enter value for iteration $i"
read myvar
`echo ${myvar}` = myvar${i}
done

So I end up with a 4 variables, myvar1,2,3 and 4 all having the value input by the user on that iteration.

Hope this makes sense.

Thanks
# 2  
Old 04-15-2010
Code:
eval "$myvar=myvar$i"

# 3  
Old 04-16-2010
Bug

Thanks for your reply. I had an idea that eval would give me what I needed but the man page wasn't much help with the syntax.

Actually FYI, I actually had to reverse your suggestion to get what I wanted. What you suggested did not assign the input value to myvar1, 2, 3 and 4, but instead created a new variable named after the value and gave it a value of myvar 1,2,3 and 4, so for iteration 1 if I input "foo" it created a new variable ${foo} with a value of myvar1.

But many thanks for your help, you pointed me in the right direction! I have been tearing my hair out for a while over this one. Should have joined the forums days ago Smilie

Just for other newbies' (like me) reference the working code is as follows:

for i in 1 2 3 4
do
echo "Please enter value for iteration $i:"
read myvar
eval "myvar${i}=${myvar}"
done

This gives a variable called myvar${i} with whatever was read by the shell as a value, for each iteration.

Thanks very much! Smilie

Last edited by yonderboy; 04-16-2010 at 01:00 AM.. Reason: incorrect syntax
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking the file depending on the input variable

Hi I have a requirement of taking time as input variable outside the script.depending on the time it will check the file output .like ./script.sh <30 min> so script.sh should run every 5 minutes ie.6 times to check the output file.Can any one please help here. (7 Replies)
Discussion started by: netdbaind
7 Replies

2. Shell Programming and Scripting

Change nth depending on matiching a pattern in Y line

Hi, I have below file, each line that starts with /* marks the beginning of the a new job. /* ----------------- cmdsMlyMoveTPMPLANTJ ----------------- UNIX_JOB CMMM002J CMDNAME /home2/proddata/bin/moveTPMPLANT.sh AGENT CMDSHP USER proddata AFTER CMMU001J /* "DDRG monthly... (13 Replies)
Discussion started by: varun22486
13 Replies

3. UNIX for Dummies Questions & Answers

Change variable value

I have big XML which i want to change all VERSIONNUMBER equal to 1,in existing file values of VERSIONNUMBER will be different as below now i want to change all VERSIONNUMBER values qual to 1.Please help me which will convert versionnumber values. <SHORTCUT OBJECTSUBTYPE ="" OBJECTTYPE ... (5 Replies)
Discussion started by: katakamvivek
5 Replies

4. Shell Programming and Scripting

I can't change the value of my variable!

I made this HEADMAKER variable to pull the header from the first file in the loop, but then to stop so it doesn't override the file with later loops. However, I CANNOT get it to reassign the value of my variable away from "FIRST". I have also tried it with 1 and 0, and with and without quotes and... (3 Replies)
Discussion started by: crankymonkey
3 Replies

5. Shell Programming and Scripting

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" file_one.sh I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to file_main.sh. file_main.sh I... (2 Replies)
Discussion started by: div_Neev
2 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

How do I change a variable to something only if it's empty?

I feel like it is just a matter of using the $ operators correctly, but I can't seem to get it... hostname="network" ip="192.168.1.1" netmask="" variables=( $hostname $ip $netmask ) for var in ${variables} do if ; then $var="--" fi done echo... (7 Replies)
Discussion started by: etranman1
7 Replies

8. Shell Programming and Scripting

Change only the name of a variable

if I have a variable named z inside a java file, is there any way to globally change the name of the variable and all its occurences as a variable? (but not any other z that is not part of that variable name) (3 Replies)
Discussion started by: lydiaflamp
3 Replies

9. Shell Programming and Scripting

Insert text into file depending on variable

Hey guys , i have a variable with the contents ... NUMBER=4 and a test file with the contents 1248 1213 1214 1278 1200 3045 3444 2130 I want to execute a script that will produce the following output ( based on NUMBER=4) to be ... create 1248 (1 Reply)
Discussion started by: theshams
1 Replies

10. Shell Programming and Scripting

renaming variable between iteration

Hi , i'm using the script below , in an iteration "for j in..." (j equal 01 then 02 and so on..), i would rename at each pass the variable nb_bal as nb_bal_$j. (so nb_bal_01 then nb_bal_02 and so on.... I think it's just a quoting problem but at this time i don't find..... somebody can... (2 Replies)
Discussion started by: Nicol
2 Replies
Login or Register to Ask a Question