Variable not preserving value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable not preserving value
# 1  
Old 10-02-2013
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.



Code:
 
#!/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:
Code:
 
 
++ 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


Last edited by Don Cragun; 06-05-2014 at 12:29 AM.. Reason: Removed email from ADMIN= line; obscured hostname
# 2  
Old 10-02-2013
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)
# 3  
Old 10-02-2013
I noticed blank spaces around assignment operator:
Code:
flag = 0

Code:
  flag = flag+1

# 4  
Old 10-02-2013
Besides
Code:
flag=0
flag=$(($flag+1))

it should be
Code:
output=`command | command`

or
Code:
output=$(command | command)

instead of
Code:
command | command | while read output

# 5  
Old 10-02-2013
@MadeInGermany: Why?
Code:
command | command | while read output

should work just fine
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-02-2013
You are right, my fault! Somehow I thought | read output; without the while.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Edit lines in file preserving part of it

Hello: I have the following HTML table: <table> <thead> <tr> <th>Code</th> <th>Percentage</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>30%</td> </tr> <tr> <td>2</td> ... (3 Replies)
Discussion started by: Cacializ
3 Replies

2. Shell Programming and Scripting

Preserving variable in script run with sudo

In a nutshell, I want $USER to reflect my user ID in a script run with sudo. I'm working with OSX in case that makes a difference. If I 'sudo echo $USER', I get my user ID. But if I 'sudo myscript.sh' and myscript.sh has a line to echo $USER, I get 'root' I'm hoping there's a switch I can add... (1 Reply)
Discussion started by: jnojr
1 Replies

3. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: Elizabeth H
3 Replies

4. UNIX for Dummies Questions & Answers

Preserving the timestamp while running gzip?

Hi guys, I'm trying to unzip a file and rename it to another while preserving the original timestamp: $ cat file.dat.gz | gzip -d > newfile.dat My goal is to assign the file.dat.gz timestamp to newfile.dat. I cannot use gunzip, due to various checks done with wget. Basically, I have to... (1 Reply)
Discussion started by: TECK
1 Replies

5. HP-UX

SCP not preserving properties, no rsync ?

I'm trying to get a number of old disks on HP-UX 10.2 copied over to a new Debian machine which has a NAS on it. The HP does not have rsync, but does have scp. Scp unfortunately does not always preserve permissions, and does not save links which were on the disk. Apparently rsync has a flag... (2 Replies)
Discussion started by: PasadenaDave
2 Replies

6. Debian

scp not preserving properties and links

I'm trying to get a number of old disks on HP-UX 10.2 copied over to a new Debian machine which has a NAS on it. The HP does not have rsync, but does have scp. Scp unfortunately does not always preserve permissions, and does not save links which were on the disk. Apparently rsync has a flag... (3 Replies)
Discussion started by: PasadenaDave
3 Replies

7. Shell Programming and Scripting

Preserving whitespace in a for loop

I obviously haven't learned my lesson with shell and whitespace. find /path/to/some/where/ -name "*.pdf" | awk '{print $5}'| uniq -d results: some Corporation other Corporate junk firmx Works fine from cmdline but the whitespace turns into another FS in a for loop. for... (7 Replies)
Discussion started by: s_becker
7 Replies

8. SCO

Add HDs while preserving the data

I am a 10 years windows person with basic unix training (very basic). I have a sco server where it originally had 3 physical drives. Drive 0 had to be replaced so I did that but because I am really new to Unix I was afraid to detroy the data on DRIVE 1 and 2, so I took them out when I loaded the... (3 Replies)
Discussion started by: mhenry
3 Replies

9. UNIX for Dummies Questions & Answers

Preserving space during command substitution

Hi all, I am caught up in a problem .... tried all possible ways i knew .. but ended up in big zero ... ;-( I have a file as below .. $ cat siv.txt 1234567890BF00522000095ibsadministrator@ae.ge.com ibsadministrator@ae.ge.com Note the spaces in between .... I am... (1 Reply)
Discussion started by: Sabari Nath S
1 Replies

10. Filesystems, Disks and Memory

Preserving Ownership w/tar

I'm trying to make a backup of a directory tree on Solaris 8. I'm doing this with my own ID, not root. The problem I am running into is when I extract the archive, all files are owned by me and the group is my default group. The man page lists this as the default behavior when executed by a... (1 Reply)
Discussion started by: bergerj3
1 Replies
Login or Register to Ask a Question