LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-06-2004, 10:29 PM   #1
chynna_v
LQ Newbie
 
Registered: Sep 2004
Posts: 13

Rep: Reputation: 0
How do you compare variable in bash programming?


Hi again its me...

i have this code...

Code:
DBS=`echo "select count(*) from out where bfr_id=63 and status=0" | mysql -s -uroot`
COUNTER=0
if ["$DBS" -gt "$COUNTER"]
then
echo "DBS is not equal to zero"
fi
else
echo "DBS is equal to zero"
#exit 0
but i always have this error...

./update_tempo.sh: line 6: [12: command not found
./update_tempo.sh: line 10: syntax error near unexpected token `else'
./update_tempo.sh: line 10: `else'

How do you compare variables in bash programming..tried looking at the examples at google but it says "-gt" command is not found...
 
Old 09-06-2004, 10:40 PM   #2
slyman
Member
 
Registered: Jun 2004
Location: Sydney, Australia
Distribution: FC3, Linux From Scratch, Knoppix
Posts: 99

Rep: Reputation: 15
Code:
if ["$DBS" -gt "$COUNTER"]
Should read

Code:
if [ "$DBS" -gt "$COUNTER" ]

Last edited by slyman; 09-06-2004 at 10:42 PM.
 
Old 09-06-2004, 10:59 PM   #3
chynna_v
LQ Newbie
 
Registered: Sep 2004
Posts: 13

Original Poster
Rep: Reputation: 0
i got this...

./update_tempo.sh: line 12: syntax error: unexpected end of file

even if i uncomment the exit

i also tried doing this..

Code:
DBS=`echo "select count(*) from out where bfr_id=63 and status=0" | mysql -s -uroot`
COUNTER=0
while [ "$DBS"  -gt  "$COUNTER" ] do
echo "DBS is not equal to zero"
done
but i get this erro instead

./update_tempo.sh: line 8: syntax error near unexpected token `done'
./update_tempo.sh: line 8: `done'
 
Old 09-06-2004, 11:22 PM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
You had you fi before your else in your first post. I'm not sure what the while loop was for in the second post. It seems that it would loop forever.

This should work:

Code:
#!/bin/sh

typeset -i DBS

DBS=`echo "select count(*) from out where bfr_id=63 and status=0" | mysql -s -uroot`
COUNTER=0

if [ $DBS -gt $COUNTER ]; then
  echo "DBS is not equal to zero"
else
  echo "DBS is equal to zero"
fi
#exit 0
 
Old 09-07-2004, 01:04 AM   #5
chynna_v
LQ Newbie
 
Registered: Sep 2004
Posts: 13

Original Poster
Rep: Reputation: 0
it did work but it does not loop infinitely...i wanted to echo that the " DBS is not equal to zero" until it does equal to zero..

i tried this code

Code:
#!/bin/sh

typeset -i DBS

DBS=`echo "select count(*) from out where bfr_id=63 and status=0" | mysql -s -uroot`
COUNTER=0
until [ $DBS -gt $COUNTER ]; do
  echo "update out set bfr_id=1 where bfr_id=63 and status=0 limit 500" | mysql -uroot
done
but it doesn't execute the
Code:
echo "update out set bfr_id=1 where bfr_id=63 and status=0 limit 500" | mysql -uroot
this part...

i tried putting
echo $DBS
and it can output the number of counts but it doesn't continue to next step...what's wrong with this?
 
Old 09-07-2004, 05:51 AM   #6
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
until [ $DBS -gt $COUNTER ]; do
echo "update out set bfr_id=1 where bfr_id=63 and status=0 limit 500" | mysql -uroot
done

did you put the 'echo $DBS' inside this until loop, or before it? You've just set COUNTER to 0, and now you're saying "do this echo command until $DBS is larger than 0" - as long as $DBS isn't zero or negative, that command is never going to execute. If $DBS is zero, your SQL won't do anything either! I am confused about what you are trying to do. Try this at the command line to see what I mean:

until true; do echo I never print; done
 
Old 09-08-2004, 03:17 AM   #7
chynna_v
LQ Newbie
 
Registered: Sep 2004
Posts: 13

Original Poster
Rep: Reputation: 0
hi sorry bout the confusion...
i tried the while statement and got my output correct... Thanks for your help!!!
 
  


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
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
Why do I get a broken connection when I try to compare SID with a variable (PHP 4) vharishankar Programming 6 07-09-2005 11:57 AM
Compare installed RPM versions using Bash script jimwelc Linux - Newbie 6 01-28-2005 11:40 AM
compare date uusing shell programming please... izza_azhar Programming 7 01-14-2005 08:24 AM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 05:38 PM


All times are GMT -5. The time now is 01:20 AM.

Main Menu
Advertisement
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