Variable not preserving value

Hi I am running this shell script .But some how the flag value is getting reset to 0 .could you please help .I'm pasting the output of the script also for your reference.

 
#!/usr/bin/sh
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free avilable) percentage
# of space is >= 90%
# Linux shell script to watch disk space (should work on other UNIX oses )
# -------------------------------------------------------------------------
# ----------------------------------------------------------------------
set -x
# set admin email so that you can get email
ADMIN=""
host=`hostname`
date=`date`
# set alert level 90% is default
ALERT=70
flag = 0
df -h | grep  itf | grep -v fititf |awk 'NF>1' |awk '{ print  $4 " " $5}'| while read output;
do
  echo $output;
  usep=`(echo $output | awk '{ print $1}' | cut -d'%' -f1  )`
  partition=`(echo $output | awk '{ print $2 }' )`
  if [ $usep -ge $ALERT ]; then
  flag = flag+1 
  #  echo "Running out of space \"$partition ($usep%)\" on "$host" as on "$date |
  #mailx -s "Alert: Almost out of disk space $usep" $ADMIN
 
  fi
done
df -k -t ext3 | grep -v  -e '^Fi' -e var |sed 's/\/dev\/cciss\/c0d0p1/                /'|awk 'NF>1'|awk '{ print  $4 " " $5}' |while read output;
do
  echo $output;
  usep=`(echo $output | awk '{ print $1}' | cut -d'%' -f1  )`
  partition=`(echo $output | awk '{ print $2 }' )`
  if [ $usep -ge $ALERT ]; then
  
flag = flag+1
  
  fi
done
if [ $flag -ge 1 ]; then
echo "Running out of space \"$partition ($usep%)\" on "$host" as on "$date
#echo "Running out of space \"$partition ($usep%)\" on "$host" as on "$date |
#mailx -s "Alert: Almost out of disk space $usep" $ADMIN

set +x

output is as follows:

 
 
++ hostname
+ host=server.company.com
++ date
+ date='Wed Oct  2 07:53:36 EDT 2013'
+ ALERT=70
+ flag=0
+ df -h
+ grep itf
+ grep -v fititf
+ awk 'NF>1'
+ awk '{ print  $4 " " $5}'
+ read output
+ echo 77% /itf/fideitf
77% /itf/fideitf
++ echo 77% /itf/fideitf
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=77
++ echo 77% /itf/fideitf
++ awk '{ print $2 }'
+ partition=/itf/fideitf
+ '[' 77 -ge 70 ']'
+ flag=1
+ echo 1
1
+ read output
+ echo 64% /itf/ude
64% /itf/ude
++ echo 64% /itf/ude
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=64
++ echo 64% /itf/ude
++ awk '{ print $2 }'
+ partition=/itf/ude
+ '[' 64 -ge 70 ']'
+ read output
+ echo 64% /itf/quant
64% /itf/quant
++ echo 64% /itf/quant
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=64
++ echo 64% /itf/quant
++ awk '{ print $2 }'
+ partition=/itf/quant
+ '[' 64 -ge 70 ']'
+ read output
+ echo 0
0
+ df -k -t ext3
+ grep -v -e '^Fi' -e var
+ sed 's/\/dev\/cciss\/c0d0p1/                /'
+ awk 'NF>1'
+ awk '{ print  $4 " " $5}'
+ read output
+ echo 48% /
48% /
++ echo 48% /
++ cut -d% -f1
++ awk '{ print $1}'
+ usep=48
++ echo 48% /
++ awk '{ print $2 }'
+ partition=/
+ '[' 48 -ge 70 ']'
+ read output
+ echo 30% /boot
30% /boot
++ echo 30% /boot
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=30
++ awk '{ print $2 }'
++ echo 30% /boot
+ partition=/boot
+ '[' 30 -ge 70 ']'
+ read output
+ echo 17% /opt
17% /opt
++ echo 17% /opt
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=17
++ echo 17% /opt
++ awk '{ print $2 }'
+ partition=/opt
+ '[' 17 -ge 70 ']'
+ read output
+ echo 9% /tmp
9% /tmp
++ echo 9% /tmp
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=9
++ echo 9% /tmp
++ awk '{ print $2 }'
+ partition=/tmp
+ '[' 9 -ge 70 ']'
+ read output
+ '[' 0 -ge 1 ']'
+ set +x

Just to rule out one possible...
Change the variable name from 'flag' to something else like 'space_flag'
I suggest this because sometimes obvious words like flag may have other meanings and usages (that may reset them)

I noticed blank spaces around assignment operator:

flag = 0
  flag = flag+1 

Besides

flag=0
flag=$(($flag+1))

it should be

output=`command | command`

or

output=$(command | command)

instead of

command | command | while read output

@MadeInGermany: Why?

command | command | while read output

should work just fine

1 Like

You are right, my fault! Somehow I thought | read output; without the while .