LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-30-2009, 09:12 AM   #1
gidrow
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Rep: Reputation: 0
How to do simple math in shell script


I am writing a simple shell script to look at swap, add up all the device swap usage and then if over a certain percentage send out an email. The logic I am missing is how to add up the numbers. I am able to correctly add the number of swapdevices but I dont know how to add their values and am missing my variable TOTALSWAP. So I need to add all the swap numbers together which I successfully get from my sed and awk line and then devide TOTALSWAP / SWAPDEV if I could get some help with the logic to get TOTALSWAP, I will then use $THRESH to compare totalswap/swapdev.

#/usr/bin/ksh
HOST=`uname -n`
THRESH=90
TOTALSWAP=0
SWAPDEV=0
swapinfo -t| sed -n '/dev/p'| awk '{print $5}'|sed 's/%//g' > /tmp/swapinfo.txt
exec </tmp/swapinfo.txt
while read line;
do
SWAPDEV=`expr $SWAPDEV + 1`
done
echo "The total number of swap devices is $SWAPDEV"
 
Old 07-30-2009, 09:41 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,489

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
I don't have the swapinfo command, but I guess the output is in a format similar to that one of df. Anyway, you can sum up all the % of usage in awk, e.g.
Code:
TOTALSWAP=$(swapinfo -t | awk '/dev/{gsub(/%/,"",$5); sum+=$5} END{print sum}')
and to retrieve the number of devices you can just do
Code:
SWAPDEV=$(swapinfo -t | grep -c dev)

Last edited by colucix; 07-30-2009 at 03:58 PM. Reason: Typo fixed
 
Old 07-30-2009, 11:15 AM   #3
gidrow
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
thanks for the quick reply I had a syntax error

I put in your line
TOTALSWAP=$(swapinfo -t | awk '/dev/{gsub(/%/,"",$5); sum+=$5} END{print sum})'

and got the following error.

./swapscript2.ksh[7]: syntax error at line 8 : `(' unmatched

The out put of my swapinfo command
swapinfo -t| sed -n '/dev/p'| awk '{print $5}'|sed 's/%//g' > /tmp/swapinfo.txt

yields the following data.
$ more /tmp/swapinfo.txt
36
1
1
1
1
1
1
0

I have now got the logic to add the numbers together and am now struggling with the division piece

This code
THRESH=4
TSWAP=0
SWAPDEV=0
swapinfo -t| sed -n '/dev/p'| awk '{print $5}'|sed 's/%//g' > /tmp/swapinfo.txt
exec </tmp/swapinfo.txt
while read swapinfo;
do
SWAPDEV=`expr $SWAPDEV + 1`
echo "swap = $swapinfo"
TSWAP=`expr $TSWAP + $swapinfo`
echo "Total swap is $TSWAP"
echo $THRESH
if [[ $TSWAP > $THRESH ]]
then echo "alert"
else echo "Damn Sam"
fi
done
echo "The total number of swap devices is $SWAPDEV"



yields the following output

$ ./swapscript2.ksh
swap = 36
Total swap is 36
4
Damn Sam
swap = 1
Total swap is 37
4
Damn Sam
swap = 1
Total swap is 38
4
Damn Sam
swap = 1
Total swap is 39
4
Damn Sam
swap = 1
Total swap is 40
4
alert
swap = 1
Total swap is 41
4
alert
swap = 1
Total swap is 42
4
alert
swap = 0
Total swap is 42
4
alert
The total number of swap devices is 8

I should see alert not damn sam right out of the gate. My if statement looks to be comparing $SWAPDEV instead of $TSWAP.

Your help is appreciated

P.S. swapinfo -t raw data is as follows

$ swapinfo -t
Kb Kb Kb PCT START/ Kb
TYPE AVAIL USED FREE USED LIMIT RESERVE PRI NAME
dev 524288 191260 333028 36% 0 - 1 /dev/vg00/swap01
dev 1048576 13596 1034980 1% 0 - 5 /dev/vg00/swap02
dev 1048576 13692 1034884 1% 0 - 5 /dev/vg00/swap03
dev 1048576 14080 1034496 1% 0 - 5 /dev/vg00/swap04
dev 1048576 13256 1035320 1% 0 - 5 /dev/vg00/swap05
dev 1048576 13260 1035316 1% 0 - 5 /dev/vg00/swap06
dev 1048576 13692 1034884 1% 0 - 5 /dev/vg00/swap07
dev 17670144 13668 17656476 0% 0 - 5 /dev/vgi191120/swap01
reserve - 14371632 -14371632
memory 16907848 2082184 14825664 12%
total 41393736 16740320 24653416 40% - 0 -

This is a HPUX box
 
Old 07-30-2009, 01:27 PM   #4
gidrow
LQ Newbie
 
Registered: Jul 2009
Posts: 6

Original Poster
Rep: Reputation: 0
I think I got it.

I'm sure there are better ways to accomplish this but I'm not a script writter. At any rate I solved the division issue by changing > with -gt and this gets me closer to my anticipated results. Still got a few minor comparision issues to work out.

swapinfo -t| sed -n '/dev/p'| awk '{print $5}'|sed 's/%//g' > /tmp/swapinfo.txt
exec </tmp/swapinfo.txt
while read swapinfo;
do
SWAPDEV=`expr $SWAPDEV + 1`
echo "swap = $swapinfo"
TSWAP=`expr $TSWAP + $swapinfo`
echo "Total swap is $TSWAP"
echo $THRESH
if [[ $TSWAP -gt $THRESH ]]
then echo "alert $TSWAP is greater than $THRESH"
else echo "Damn Sam looking good swap total is $TSWAP and threshol
d is $THRESH"
fi
done
echo "The total number of swap devices is $SWAPDEV"

OUTPUT IS AS FOLLOWS

$ ./swapscript2.ksh
swap = 36
Total swap is 36
4
alert 36 is greater than 4
swap = 1
Total swap is 37
4
alert 37 is greater than 4
swap = 1
Total swap is 38
4
alert 38 is greater than 4
swap = 1
Total swap is 39
4
alert 39 is greater than 4
swap = 1
Total swap is 40
4
alert 40 is greater than 4
swap = 1
Total swap is 41
4
alert 41 is greater than 4
swap = 1
Total swap is 42
4
alert 42 is greater than 4
swap = 0
Total swap is 42
4
alert 42 is greater than 4
The total number of swap devices is 8
 
Old 07-30-2009, 03:42 PM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
you can have this if you want.

Code:
#!/usr/local/bin/perl -ws

sub usage()
{
    print <<"EOF";

# tots up all numbers in a tabular file
# assumes first column is the number to
# be totalled unless specified with -col=n
# -quiet will just print the number
#
# e.g. try: 'du -s * | total'
#
# or
#
# ls -l | total -col=5
EOF
    die "\n";
}

$h ||= undef;
usage if $h;

$col ||=1;
$col--;

while (<>) {

    chomp;

    next unless /./; # skip blanks
    $number = (split)[$col] || 0;
    $total += $number;
    print unless $quiet;
    print " ($number)\n" unless $quiet;

}

print "\n" , "=" x length($total), "\n" unless $quiet;
print "$total\n";
 
Old 07-30-2009, 03:46 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,489

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by gidrow View Post
I put in your line
TOTALSWAP=$(swapinfo -t | awk '/dev/{gsub(/%/,"",$5); sum+=$5} END{print sum})'

and got the following error.

./swapscript2.ksh[7]: syntax error at line 8 : `(' unmatched
Oh yes, I made a typo... I switched the parenthesis at the end of the command line with the single quote. I've corrected my previous post. Glad to see you solved the issue. Cheers!

Last edited by colucix; 07-30-2009 at 03:58 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
csh Shell Script: Getting wc command output to variable to do math on it, how? vxc69 Programming 5 05-04-2009 04:31 PM
need shell-script help (very simple) oskar Linux - General 6 03-12-2008 07:08 AM
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM
simple shell script sycamorex Linux - Newbie 2 03-16-2006 06:00 PM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM


All times are GMT -5. The time now is 11:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration