Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm writing a script in AIX 5.3 that will loop through the output of a df and check each volume against another config file. If the volume appears in the config file, it will set a flag which is needed later in the script. If my config file only has a single column and I use a for loop, this works perfectly. My problem, however, is that if I use a while read loop to populate more than one variable per line, any variables I set between the while and the done are discarded.

For example, assuming the contents of /netapp/conf/ExcludeFile.conf are a bunch of lines containing two fields each:

volName="myVolume"
utilization=70
thresholdFlag=0

grep volName /netapp/conf/ExcludeFile.conf | while read vol threshold; do
    if [ $utilization -ge $threshold ] ; then
        thresholdFlag=1
    fi
done

echo "$thresholdFlag"

In this example, thresholdFlag will always be 0, even if the volume appears in the file and its utilization is greater than the threshold. I could have added an echo "setting thresholdFlag to 1" in there, see the echo, and it'll still echo a 0 at the end.

Is there a clean way to do this? I think my while loop is being done in a subshell, and changes I make to variables in there are actually being made to local variables that are discarded after the done.

share|improve this question
    
    
Please re-edit the Subject, since your question has very litte to do with line reading, it's rather about "variables I set between the while and the done are discarded." –  poige Oct 11 '12 at 20:45
    
@poige, I think the current title is a better choice for the purposes of SEO and searches. Almost everytime I see this asked the title is like above. –  Zoredache Oct 11 '12 at 20:47
    
Also, this problem comes up when you have to switch from a for loop to a while read loop because of multiple variables. –  Basil Oct 11 '12 at 20:49
    
Related question on U&L -- unix.stackexchange.com/questions/5869/… –  voretaq7 Oct 11 '12 at 20:49

2 Answers 2

Zoredache pointed this out to me in chat, and poige mentioned it in his answer: this problem can be solved with a subshell.

When I had to change from a for loop that read a single variable from my grep at a time to a while read var1 var2 loop that allowed me to read in multiple variables, I was able to hang on to temporary variables I manipulated within the while loop by using parentheses to define an explicit subshell. Here's an example:

sum=0

grep volume configfile | head -n1 | (while read var1 var2; do
  let sum=var1+var2
done

echo "The sum is $sum.")

Without the parentheses, you will always echo a sum of 0. With them, you will echo the sum of the first two values in the first matching line of your grep.

Additionally, as Poige points out in another answer, you can use a subshell to populate an in-scope variable like this:

var=$( cat file | while read a b; do
    sum=a+b
    echo "$sum"
done)

echo "$var"

In this case, the value of var that you echo at the end would be the last sum you calculated in your loop, even though sum got destroyed at the end of the subshell.

share|improve this answer

Two ways at least — return Err code and check it, or wrap your sub-shell code:

Res=$(cat /etc/hosts | while read h _rest; do
    [ "$h" = 'fe00::0' ] && echo 'Found'
done
)
echo $Res
share|improve this answer
    
You're correct, but there's not enough detail in here for a shell noob to really get at what you mean. –  Basil Oct 12 '12 at 14:00
    
@Basil, haven't you understood mine answer? –  poige Oct 12 '12 at 16:12
    
Not at first glance- now that I've looked up what happens if you use square brackets with a condition and then a logical operand like && with an action after it, it's a little clearer, but certainly not something that someone at my level would understand at first glance. That said, I do understand it now, and if you want me to, I can add a longer explanation before the code. –  Basil Oct 12 '12 at 17:19
1  
@Basil, please don't make cosmetic changes like changing [ … ] && to if then fi — it doesn't make sense and what it really is just style. I prefer brevity, others may prefer long ways to say the same. There's also no need in chewing up everything enough to feed a toothless — others won't eat it. –  poige Oct 12 '12 at 17:52
    
fine, then your answer is too brief to be helpful, and I'll accept another one that lays it out easily for shell noobs. –  Basil Oct 12 '12 at 19:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.