Results 1 to 2 of 2
Could someone please tell me why my if statement is not working. I think I am comparing my integers correctly. No matter how high the $num1 count gets, it still ...
- 12-03-2008 #1Just Joined!
- Join Date
- Nov 2008
- Posts
- 3
Trouble with comparisons
Could someone please tell me why my if statement is not working. I think I am comparing my integers correctly. No matter how high the $num1 count gets, it still responds with "They are equal"
Code:#!/bin/bash #I understand I get an error here until it is run again, #but I do not believe it is affecting the comparison. num1=0 cat wallet | grep -c spend > $num1 cat $num1 num2=0 cat wallet | grep -c spendmore > $num2 cat $soff if [ $num1 -eq $num2 ]; then echo "They are equal" else echo "Not equal" fi echo "What are you buying today today?" read ans echo "------------------------------------------" >> wallet echo "spend " $(date)>> wallet echo $ans >> wallet
- 12-04-2008 #2Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
The problem lies not in the comparison statement. It's the variable assignment statements that cause the problem:
cat wallet | grep -c spend > $num1
is wrong, $num1 is not a file, you can not assign a variable value this way. $num1 and $num2 remain their original values (0) regardless the occurrences of 'spend' and 'spendmore' in the file wallet.
One of t the correct ways is
num1=`cat wallet | grep -c spend`
Do the same treatment for num2 and see if it runs correctly.


Reply With Quote