PDA

View Full Version : Changing a var in a shell loop



CatharsisJelly
November 11th, 2009, 05:13 PM
Can someone explain to me why $text is blank after exiting the loop?



#!/bin/bash
BOX='my.box'
THRESHOLD=10
MAILADDRESS='foo.bar@foo.com'
text=''
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $THRESHOLD ]; then
text="${text}Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)\n"
fi
echo ${#text}
done
echo $text

0cton
November 11th, 2009, 05:19 PM
It could be because the text inside the loop is a local variable.

CatharsisJelly
November 11th, 2009, 05:29 PM
Okay so how do I get it to change the var that I assigned outside of the loop, or access this var outside of the loop?

ghostdog74
November 11th, 2009, 05:40 PM
you can see this (http://ghostdog74.livejournal.com/3027.html) similar example

Penguin Guy
November 11th, 2009, 06:38 PM
It could be because the text inside the loop is a local variable.
I'm pretty sure it's nothing like that, I think it's because you're while statement is odd:


df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;



This seems to work for me:
[BASH]


#!/bin/bash
THRESHOLD=10
text=''
OLD_IFS=${IFS}
IFS='
'
for output in `df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $5 " " $1}'`
do
size=`echo ${output} | awk '{ print $1}' | cut -d'%' -f1`
partition=$(echo ${output} | awk '{ print $2 }' )
if [ ${size} -ge ${THRESHOLD} ]
then
text="${text}Running out of space on \"${partition}\" (${size}% full).\n"
fi
done
IFS=${OLD_IFS}
text="${text}\t-- `hostname`: `date`\n"
echo -ne ${text}

(we change the IFS so that the for loop runs line-by-line rather than word-by-word)

CatharsisJelly
November 11th, 2009, 07:01 PM
Thanks for all the help people, I almost had kittens at the explanation of the while loop running a subshell, never knew that and think its a bit mad. Both the awk and shell response work nicely, thanks