This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

exec, grep, and shells oh my

Old posts that have not been replied to for several years.
Locked
B
BinaryBob
Voice
Posts: 4
Joined: Sun Mar 13, 2005 11:20 pm
Location: 3rd Dimension - Earth, Milkyway

exec, grep, and shells oh my

Post by BinaryBob »

I'm trying to create a script that will let me lookup text in log files. I figured using grep externally would provide me with alot of flexability. I got it working, but it only works using one file, and I'm having my log files divided out by day, so I want to use the wildcard * liek I do on a shell to have grep lookinto all of them. Here's the code for the wildcard version:

Code: Select all



bind pub - {??} lookup_string

proc lookup_string {nick host handle chan args} {
        putquick "NOTICE $nick :Looking up: $args"
        set chanmod [string trimleft $chan #]
     
        set out [exec /bin/bash -c 'grep -i $args /home/eggdrop/eggdrop/logs/[string tolower $chanmod].log*']
        set arr [split $out \n]
        foreach line $arr {
                putquick "NOTICE $nick :$line"
        }
}

putlog "FlashBack loaded."
The error I get when DCCed into eggdrop is:

Code: Select all

<ADMINBOT> [19:35] Tcl error [lookup_string]: -i: -c: line 1: unexpected EOF while looking for matching `''
<ADMINBOT> -i: -c: line 2: syntax error: unexpected end of file
The command : /bin/bash -c 'grep -i $args /home/eggdrop/eggdrop/logs/[string tolower $chanmod].log*' works in the shell tho if im logged in. (after fixing up the varible parts of course)

Anyone have any ideas to make this work? Or... If i cant do that i could have 1 log file, which I know how to, but is there a way to change the timestamps to include the ddMMMyy date along with the time? I didnt see an option for that in the conf.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

no need to fork bash; also get rid of those single quotes:

Code: Select all

exec /bin/grep -i $args /path/$chan.log*
B
BinaryBob
Voice
Posts: 4
Joined: Sun Mar 13, 2005 11:20 pm
Location: 3rd Dimension - Earth, Milkyway

Post by BinaryBob »

Thank you!
Ok, some progress. Now I get this error

Code: Select all

<ADMINBOT> [19:51] Tcl error [lookup_string]: child process exited abnormally
Ideas?
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

grep(1) returns 1 if no matches were found, and any return code but 0 means "abnormal program termination"

so you need to catch the error and continue:

Code: Select all

if ![catch {exec /bin/grep -i $args /path/$chan.log*}] {
  # match(es) found, do your stuff 
} else {
  # no matches
}
B
BinaryBob
Voice
Posts: 4
Joined: Sun Mar 13, 2005 11:20 pm
Location: 3rd Dimension - Earth, Milkyway

Post by BinaryBob »

Mistake on my part, when i did your first modification, i didn't incldue the *, duh.

So I fixed that, included your catch lines.

And the error is now:

Code: Select all

<ADMINBOT> [20:33] Tcl error [lookup_string]: grep: /home/eggdrop/eggdrop/logs/sdcara.log*: No such file or directory
It seems to take the wildcard literally...
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

well you may need that bash after all hehe... expanding * metachar

try your /bin/bash -c with double quotes
B
BinaryBob
Voice
Posts: 4
Joined: Sun Mar 13, 2005 11:20 pm
Location: 3rd Dimension - Earth, Milkyway

Post by BinaryBob »

Woohoo! Double quotes it is!

Thank you very much.
Locked