Preserving values with for loop for later use | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Preserving values with for loop for later use

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-05-2012
Elizabeth H Elizabeth H is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 20 November 2013, 1:02 PM EST
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Preserving values with for loop for later use

Below is the issue I am having.

I have a few variables which have certain values in them like
var1=23
var2=46
var3=78 etc...


I want to save these values with the help of a for loop in a single variable so that I can use it later,beacuse a few lines down the script, some of these variables get set to a different value and I would like to reset them to the values that I had saved.Can you point out a way without using arrays?
Sponsored Links
    #2  
Old 12-05-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 September 2014, 4:01 PM EDT
Location: Saskatchewan
Posts: 19,392
Thanks: 790
Thanked 3,271 Times in 3,066 Posts
I would put the problem code in a subshell instead, to prevent its changes from affecting the rest of the shell.


Code:
#!/bin/sh

VAR1=asdf
VAR2=qwertyuipo

(
        VAR1=slartibartfast
        VAR2=lkjlkdsfh

        echo "some stuff"
)

echo "Variables changed inside ( ) do not change outside"
echo "VAR1 is still $VAR1"

Sponsored Links
    #3  
Old 12-05-2012
Elizabeth H Elizabeth H is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 20 November 2013, 1:02 PM EST
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Can I do something like this?


temp=`for t in var1 var2 var3; do <save the value> ; done`
<loop which sets the values to a different value>
eval echo $temp


Is there a way I can save the initial values of the variables in a single variable as above(temp)
    #4  
Old 12-05-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 September 2014, 4:01 PM EDT
Location: Saskatchewan
Posts: 19,392
Thanks: 790
Thanked 3,271 Times in 3,066 Posts
That would be ugly, glitch-prone, and a gaping security hole(never use eval), which is why I suggested the alternative.

This could work, if your variables don't have more than one line in them.


Code:
printf "%s\n" "$var1" "$var2" "$var3" > save

for X in var1 var2 var3
do
        read $X
done < save

rm -f save

But again, the best way would be to avoid the problem completely by using a subshell -- that's one reason they exist. Or just don't overwrite those variables in the first place.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array Variable being Assigned Values in Loop, But Gone when Loop Completes??? mrm5102 Shell Programming and Scripting 5 10-19-2012 10:00 AM
Preserving whitespace in a for loop s_becker Shell Programming and Scripting 7 03-26-2009 03:09 PM
adding values with a loop hcclnoodles Shell Programming and Scripting 1 05-27-2008 06:42 AM
getting values from variable in a loop kriuz Shell Programming and Scripting 3 01-22-2008 04:50 PM
while read loop preserving leading whitespace zazzybob Shell Programming and Scripting 3 06-07-2004 05:36 PM



All times are GMT -4. The time now is 10:12 PM.