Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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
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).
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
[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
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.