LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-25-2009, 09:11 AM   #1
amriteshsmsc
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Rep: Reputation: 0
Arrow How to Substract two values in shell script


I m trying to do this

I have a file a.txt
content is as following

$ cat SMSCDR_POSTPAID_09062518* | wc -l
43498
$ sleep 1
$ cat SMSCDR_POSTPAID_09062518* | wc -l
43547

I am getting this by running a script on remote terminal and directing output to a.txt

I need the values after executing command "$ cat SMSCDR_POSTPAID_09062518* | wc -l" so i m using
a=`awk '/cat/{getline; print}' a.txt| tail -n 1`
echo $a
b=`awk '/cat/{getline; print}' a.txt| head -n 1`
echo $b
to get these values in variable a $ b.

then I need difference b/w in these two variables
c=`expr $a - $b`
echo $c
echo "Message Processed in 1 Second: $c"
then if c > 0 then i need to print
server is ok else down
if [ ${c} -gt 0 ]
then
echo "RS-$ip MSG Flow:OK"
else
echo "RS-$ip MSG FlowOWN"
fi


out put of these script is as following


[root@linux-amritesh amritesh]# sh diff

43498

43547
a-b
Message Processed in 1 Second: -
diff: line 17: [: -: integer expression expected
server down
 
Old 06-25-2009, 09:16 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Code:
sh-3.1# c=1000
sh-3.1# d=4500
sh-3.1# let c=$c-d
sh-3.1# echo $c
-3500
sh-3.1#

Here's a hint, above. To operate arithmetically on variables, the shell needs to know that they are numerical operands. The `let` function tells the shell that they are numerical/digits, letting you subtract.

To compare, try:

[ $d -le $c ] && <do something if less or equal> || <do something else if NOT less than or equal>

-le means less than/equal... -ge means greater than/equal... See `man bash` for more complete help!

Sasha

Last edited by GrapefruiTgirl; 06-25-2009 at 09:20 AM. Reason: additional info
 
Old 06-25-2009, 09:26 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
It might help if you posted the actual script, or important portion thereof. Which line is "line 17", for example? the snippets you've posted all seem to be ok to me.


If you're using bash, you can use the built-in bash arithmetic functions to do the job instead (as long as the numbers are always integers).

c=$((a-b))
 
Old 06-26-2009, 04:20 AM   #4
amriteshsmsc
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by David the H. View Post
It might help if you posted the actual script, or important portion thereof. Which line is "line 17", for example? the snippets you've posted all seem to be ok to me.


If you're using bash, you can use the built-in bash arithmetic functions to do the job instead (as long as the numbers are always integers).

c=$((a-b))
Thanks for your Suggestion
Here I am giving my actual script and output

Script:

#!/bin/bash

a=`awk '/cat/{getline; print}' /root/amritesh/report/rs_10.100.76.19 | tail -n 1`
b=`awk '/cat/{getline; print}' /root/amritesh/report/rs_10.100.76.19 | head -n 1`
echo $a
cat a
echo $b
cat b
let c=$a-$b
echo $c
cat c
c=`expr $a - $b`
cat c
echo "Message Processed in 1 Second: $c"
if [ ${c} -gt 0 ]
then
echo "server ok"
else
echo "server down"
fi


File rs_10.100.76.19

$ cat SMSCDR_POSTPAID_09062613* | wc -l
137634
$ sleep 1
$ cat SMSCDR_POSTPAID_09062613* | wc -l
137792


Output of Script:

[root@linux-amritesh amritesh]# sh diff
137792
cat: a: No such file or directory
137634
cat: b: No such file or directory
diff: line 9: let: c=: syntax error: operand expected (error token is "=")

cat: c: No such file or directory
expr: non-numeric argument
cat: c: No such file or directory
Message Processed in 1 Second:
diff: line 15: [: -gt: unary operator expected
server down
 
Old 06-26-2009, 05:00 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Well, other than the fact that "cat a", "cat b", and "cat c" are meaningless (cat prints the contents of files, and you don't have any files named a, b, or c here), the rest of the script works just fine for me. At least using the input file that you gave.

Something seems to be broken with your arithmetic processing, or the way the variables are being read or something. I've tried it with

let c=$a-$b
c=$(expr $a - $b)
c=$((a-b))

and all three expressions work just fine. Strange that it doesn't work for you.
What version of bash are you running here?

Try running the script with "$!/bin/bash -x" as the first line. This will enable verbose output so you can see exactly what's happening as the script runs.

(FYI, it's recommended that you use $(expr) rather than `expr` for process substitution)

(FYI2, you should post your scripts and long code blocks inside [code][/code] tags, in order to preserve formatting.

Last edited by David the H.; 06-26-2009 at 05:12 AM.
 
Old 06-26-2009, 06:27 AM   #6
amriteshsmsc
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
Well, other than the fact that "cat a", "cat b", and "cat c" are meaningless (cat prints the contents of files, and you don't have any files named a, b, or c here), the rest of the script works just fine for me. At least using the input file that you gave.

Something seems to be broken with your arithmetic processing, or the way the variables are being read or something. I've tried it with

let c=$a-$b
c=$(expr $a - $b)
c=$((a-b))

and all three expressions work just fine. Strange that it doesn't work for you.
What version of bash are you running here?

Try running the script with "$!/bin/bash -x" as the first line. This will enable verbose output so you can see exactly what's happening as the script runs.

(FYI, it's recommended that you use $(expr) rather than `expr` for process substitution)

(FYI2, you should post your scripts and long code blocks inside [code][/code] tags, in order to preserve formatting.
Thanks actually i was just checking that a and b are treated as variables only so I used cat also. here one strange thing is happening that when i m doing echo $a it means that echo $'147821\r' instead of echo $147821 I have tried with all methods available on net

here is the out put using $(expr)

Code:
[root@linux-amritesh amritesh]# sh -x diff
++ awk '/cat/{getline; print}' /root/amritesh/report/rs_10.100.76.19
++ tail -n 1
' a='  147821
++ awk '/cat/{getline; print}' /root/amritesh/report/rs_10.100.76.19
++ head -n 1
' b='  147577
+ echo $'147821\r'
147821
+ echo $'147577\r'
147577
+ let c= $'147821\r-' $'147577\r'
diff: line 7: let: c=: syntax error: operand expected (error token is "=")
+ echo

++ expr $'147821\r' - $'147577\r'
expr: non-numeric argument
+ c=
+ echo 'Message Processed in 1 Second: '
Message Processed in 1 Second:
+ '[' -gt 0 ']'
diff: line 11: [: -gt: unary operator expected
+ echo 'server down'
server down

Last edited by amriteshsmsc; 06-26-2009 at 06:39 AM.
 
Old 06-26-2009, 06:45 AM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Try running dos2unix on your input file - it looks to me like it's formatted in Windows, not UNIX, format.
 
Old 06-26-2009, 08:06 AM   #8
amriteshsmsc
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Talking Thanks

Quote:
Originally Posted by pwc101 View Post
Try running dos2unix on your input file - it looks to me like it's formatted in Windows, not UNIX, format.
Buddy now it is working fine...
can u plz tell me why it is in dos format rather that in unix because we are generating this in unix system only
Thanks a lot u again...

Last edited by amriteshsmsc; 06-27-2009 at 01:25 AM.
 
Old 06-26-2009, 09:05 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
So it was the old dos/unix line-ending difference problem. I was wondering why you were getting a "uniary operator expected" error, if the variable was being output as a number. It turns out the hidden dos line ending was still attached to it.

Was this file generated on a Windows system then? Are you running this on cygwin or something? That's the kind of thing that might be useful to know when diagnosing problems like this.
 
Old 06-26-2009, 09:21 AM   #10
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by David the H. View Post
So it was the old dos/unix line-ending difference problem.
Good catch on the bash -x suggestion
 
  


Reply

Tags
compare, two, variables


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script that assign's values to fields NsearchOf Programming 16 06-01-2009 10:00 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
help with shell script - fill an array with values from a file delmoras Linux - General 1 07-17-2006 11:19 AM
passing values in shell pantera Programming 1 05-20-2004 09:01 AM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 09:52 AM


All times are GMT -5. The time now is 11:43 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