LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-17-2005, 09:17 PM   #1
chatterbug89
Member
 
Registered: Jun 2004
Distribution: TopologiLinux 4.0.0, Slackware 10.0, LFS 6.0
Posts: 53

Rep: Reputation: 15
BASH script showing random behavior


OK, I thought after 3 days of learning BASH shell scripting if it would be posible to create a IRC chat bot with BASH shell scripting, and low and behold, I did. In fact, it is workin fairly well and is already 224 lines of code (including the 3 days I spent learning BASH shell scripting I probaly ahve spent about a week on this project). Anyways, my point is, i'm releatively new to BASH shell scripting.

So, every once in a while I am noticing that the script sort of goes bazerk and starts passing if statements when they arn't true...for example this just happened not too long ago.

aku tig, you can googlemaps aggie land XD
Tigre she went to a&m
Tigre XD
aku you get a&m
aku well CS
C-b0t You can not calculate nothing!
C-b0t Why hello there!
aku !calc 1+1
C-b0t You entered an illegal operation. Correct Format: !calc equation END
aku o.O
cbeley uh oh...
cbeley :S
cbeley it went bazerk again
aku XCD
C-b0t Why hello there!

The bot just suddenly started actign as if the !calc flag was said and that someone said Hi C-b0t for no apparent reason. It just happened. I tried recreatign the whole situation including redoing all the commands I recently issued to the bot ...and nothing happened. Also, I should note that this eratic behavior happened around a hour after it was running. My question is, is there a soltuion to this. From the looks of it, it seems nothing in my code coudl cause this sort of random behavior and it seems to just suddenly happen at no particlar time and for no apparent reason, repeately allow if statements to pass true.

Currenlty the script is running on Slackware 10.0 and I am using BASH 3.00.15(2). If anyone thinks that pasting the source would help solve this weird problem, please give a shout.

EDIT: Just thought I would mention that just because in that example the parts of the code that were outputting that are extremealy similar to a bunch of other parts of the code...it is compeltely random what goes wrong..it's not always that calculator and hello...however, it hasen't happen too much. But, just to prevent some peopel from contradicting, here is the code segments of the response to Hi C-b0t and the calculator part.

Hi C-b0t code
Code:
# Begin responses to messages
if grep "Hi $nick" nohup.out; then
	echo 'Why hello there!' > $pipe &
elif grep "hi $nick" nohup.out; then
	echo 'Why hello there!' > $pipe &
fi
Calculator
Code:
# Begin Calculator
if grep '!calc' nohup.out; then
		calc=`awk '/!calc/ { print $3 }' nohup.out`
		calc2=`echo $calc`
		echo "The value of calc is $calc"
		echo "The value of calc2 is $calc2"
		null=
		if [ $calc2 -eq $null ]; then
		echo 'You can not calculate nothing!' > $pipe &
		else
		calc $calc2
			if [ $? -ne 0 ];then
			if [ $calctest -eq 0 ]; then
			echo 'You entered an illegal operation.  Correct Format: !calc equation END' > $pipe &
			calc 1+1
			calctest=1
			fi
			else
				calctest=0
				ans=`calc $calc2`
				if [ `echo ${#ans}` -gt 22 ];then
				echo 'Are you tryign to make me spam!?' > $pipe &
				else
				echo "$calc2 = $ans" > $pipe &
				fi
				fi
		fi  
 fi
And their you go ;-)

EDIT2: Oh, just to answere another posible problem...the calc program has 0 for true and 1 for false...sort of awkward :S Also, the calc 1+1 is put there due to a problem with calc repeating messages ..i think..don't excaclty remember what the problem was, but that was for a bug fix, i know that.

Last edited by chatterbug89; 07-17-2005 at 09:35 PM.
 
Old 07-17-2005, 09:33 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
The !calc seems to be in there...

Also, how is the bash script being connected to the irc network?
 
Old 07-17-2005, 09:37 PM   #3
chatterbug89
Member
 
Registered: Jun 2004
Distribution: TopologiLinux 4.0.0, Slackware 10.0, LFS 6.0
Posts: 53

Original Poster
Rep: Reputation: 15
I use BitchX and pipes...I tried MANY IRC clients and BitchX was the only one taht liked pipes.

Oh...I didnet notice that...but still...I know !calc wasen't issued before that and it was doign random thigns after aku said !calc to. so yeah..

If you just enter !calc equation without END or anythign for that matter if gives a error...that's correct, but it has never caused the whole script tos tart doign random thigns :S.

EDIT: I was wondering, could server load have anythign to do with this...could a higher server load (past 2.0) cause this the if statements to randomly start passign when they arn't. Also, I sort of have a log, when grep passes I see the person who issued the command...when these random occurances happen I see no output from grep...so I know grep isen't even running yet the if statements are passing even thouhg grep failed.

Last edited by chatterbug89; 07-17-2005 at 09:39 PM.
 
Old 07-17-2005, 09:47 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Out of curiousity... is nohup.out a regular file? If so, how are you clearing it? Otherwise, you'd think it would see the same lines in there time and time again.
 
Old 07-17-2005, 09:51 PM   #5
chatterbug89
Member
 
Registered: Jun 2004
Distribution: TopologiLinux 4.0.0, Slackware 10.0, LFS 6.0
Posts: 53

Original Poster
Rep: Reputation: 15
bah, I shoudl jsut post the code, but oh well.

Here is how it works.

1. First the script logs in.
2. the script goes into a infinate loop...sort of. the loop will die if the quit variable is set to a differnt variable.
3. all the goodies are here...it tests what's in nohup.out . nohup.out is created when the command nohup $client & is issued.
4. at the end of the loop the following is done to clear out nohup.out
: > nohup.out
sending null into nohup.out and therefore clearing it.
5. repeat waht is said unless the quit variable is changed
6. delete the nohup.out file just to stay clean if the loop breaks and send /quit to the client.
7. end the program.

That is basicly how it works...nothign too advanced...simple BASH shell scripting...If I can do that after 3 days of learnign BASH shells cripting and then creating my first major program, i'm sure you can :-P

EDIT: Just noticed, it happened again, but it wasne't the same things that happened...this time is was the random number generator and the hello reply...however this time it didne't go baserk..jsut showed that and toped acting up.
EDIT2: I'm goign to try getting BitchX installed on my LFS box and see if the script runs better on that. I do my torrent downloading on my slackware server, so the server load is above 1.0 when i'm runnign that script...still..that isen't too high.

Last edited by chatterbug89; 07-17-2005 at 09:59 PM.
 
Old 07-17-2005, 10:09 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
I don't know how well clearing a file that's opened by two processes works. I'm off to do some testing on that.
 
Old 07-17-2005, 10:27 PM   #7
chatterbug89
Member
 
Registered: Jun 2004
Distribution: TopologiLinux 4.0.0, Slackware 10.0, LFS 6.0
Posts: 53

Original Poster
Rep: Reputation: 15
well, here is how the end works.
I simply do
Code:
: > nohup.out # Clear the nohup.out file.
sleep 1s
The sleep is so after the file is cleared there is a little extra time for any commands to accumlate in nohup.out. So far this usualy has worked fairly well except for a few times. The chaotic behavori can't posibly be due to problems with 2 programs writing to the same file continusly because if for some reason it was because something wasen't being cleared from nohup.out I would ahve seen it in my log. grep outputs what it finds to console, so I always see what's going on, but when this eratic behavorio is happening grep outputs nothing. So this must mean the problem lies somewhere else

I"m working on getting BitchX on my LFS box..but it dosen't want to work :S

EDIT: No fear, I got BitchX working on LFS...downloaded the wrong executable :-P ...now I just ahve to compile calc on the LFS box and i'll give it a try on that and see if it fixes the problem. My LFS box is much more dependable than my Slackbox :-P

Last edited by chatterbug89; 07-17-2005 at 10:38 PM.
 
Old 07-18-2005, 03:37 PM   #8
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
You may be getting some rubbish when grep actually finds a matching line. Your first script should be modified thus (and made more efficient!):
Code:
# Begin responses to messages
if grep "[Hh]i $nick" nohup.out >/dev/null; then
        echo 'Why hello there!' > $pipe &
fi
 
Old 07-18-2005, 03:51 PM   #9
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
That does make me wonder if grep is working well. Have you tried running the script with 'bash -x'?
 
Old 07-18-2005, 04:28 PM   #10
chatterbug89
Member
 
Registered: Jun 2004
Distribution: TopologiLinux 4.0.0, Slackware 10.0, LFS 6.0
Posts: 53

Original Poster
Rep: Reputation: 15
Eddie..thanks...i'm still very new to BASH shells scripting and this is more of my learnign project. I will do that right away

Also, I seemed to have fixed the problem ..with a lot of unecesairy code. I basicly repeated the first flag check for almost eveyrthing. This way, if the first grep check failed the secound one usualy woudlne't. So far this has been working great and Iw ill be testing it in a empty channel for another hour or two to see if I see the random behavior again. The only thign wiht this...I just added a bunch of code which I realy don't need...sort of a waste :S

Matir: No I have no, and I guess i can ry it. However, it ahs been working...the random events didne't happen right away...sometiems never. They were random :-P.

Anyways, I might as well the post the code..I didne't excaclty want to, but oh well...i'm sharing my first true BASH shell script creation in the next post. However, you need a little more than the following code ot get it to work prioperly :-P
 
Old 07-18-2005, 04:32 PM   #11
chatterbug89
Member
 
Registered: Jun 2004
Distribution: TopologiLinux 4.0.0, Slackware 10.0, LFS 6.0
Posts: 53

Original Poster
Rep: Reputation: 15
Heh, decided I didne't want to share it anymore :-P sorry....Also...my bug fixes seemed to ahve fixed eveyrthing :D IT sitll added a lot of unecesairy code, but oh well..atleast it fixed it.

EDIT3: Eddie...clearing otu the nohup.out file at that stage would cause all furuther commands not to be procesed ;-) So the final version of that segment with the bug fix is.
Code:
# Begin responses to messages
if grep "[Hh]i $nick" nohup.out; then
if grep "[Hh]i $nick" nohup.out; then # Bug fix
        echo 'Why hello there!' > $pipe &
fi # End bug fix
fi
Also, Matir...I ahve been using this bot quite a bit (in a active room) and having too thigns constantly writing to a file has yet to cause any sort of problem. ;-)

Last edited by chatterbug89; 07-18-2005 at 05:59 PM.
 
Old 07-19-2005, 12:02 AM   #12
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
So your random behavior is not a problem?

Out of curiousity, how are the two nested identical if statements supposed to be a bug fix? If and only if the first one passes will the second be tested, but if it reached the second, we can be sure it will run.

Conventionally, if greps are handled like this (as far as I know):
Code:
if [ `grep -c "query" file` -gt "0" ]
    then echo "query found in file"
fi
 
Old 07-19-2005, 10:45 PM   #13
chatterbug89
Member
 
Registered: Jun 2004
Distribution: TopologiLinux 4.0.0, Slackware 10.0, LFS 6.0
Posts: 53

Original Poster
Rep: Reputation: 15
at the moment it's jsut if grep "bla" file; then ...maybe doing it that way would fix the problem.

Anyways, I had it runnign fine for a day..the two if statements in a row were supose to be a backup in case the first if statement fails. However, I just started noticing it happening a few times again...very rarely..but still ahppening none the less...so i still have the problme. I'll try looking into your way above and changing it to that and hopefuly it may fix the problems.

It still seems weird that this is happening...

EDIT: Took a look at what -c does...I dont' realy want that. I like to be able to see the output of grep at console so I know what commands people are giving to it and it also helps me if someoen finds a flaw. mmm...

Anyways, grep etiher returns true or false accoridng to wheather it works or not, so I don't see why my way would cause it to so randomly pass if statements...it is completely random...nothign trigers it. Before the "bug fix" I could leave it in a open room and itw oudl just say random thigns every once in a while. Even know it's happening, but very rarely. I did notice though..that is jsut started happenign now again a little and that my server laod was around 2.0 ...while before it was just around 1.0 ...so maybe server load does have something to do wit this? I would try runnning it on my LFS box, but i'm gettign seg fault errors using pipes and no one on the LFS IRC channel new why.

Last edited by chatterbug89; 07-19-2005 at 10:50 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
Mysterious bash behavior in scripts Woodsman Slackware 14 11-14-2005 04:29 PM
can bash generate random digit? sorno Linux - Newbie 5 08-17-2005 06:05 AM
strange sed/bash behavior mpdavig Programming 1 07-24-2004 02:27 AM
Strange Script Behavior Stranger Linux - General 0 02-18-2004 03:30 AM
bash random function NSKL Programming 6 02-02-2003 01:39 PM


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