How to do it…

Now, we will write a script for an IRC logging bot. Create the logging_bot.sh script and write the following code in it:

#!/bin/bashnick="blb$$"channel=testchannelserver=irc.freenode.netconfig=/tmp/irclog[ -n "$1" ] && channel=$1[ -n "$2" ] && server=$2config="${config}_${channel}"echo "NICK $nick" > $configecho "USER $nick +i * :$0" >> $configecho "JOIN #$channel" >> $configtrap "rm -f $config;exit 0" INT TERM EXITtail -f $config | nc $server 6667 | while read MESSAGEdo    case "$MESSAGE" in        PING*) echo "PONG${MESSAGE#PING}" >> $config;;                                  *QUIT*) ;;        *PART*) ;;        *JOIN*) ;;        *NICK*) ;;        *PRIVMSG*) echo "${MESSAGE}" | sed -nr "s/^:([^!]+).*PRIVMSG[^:]+:(.*)/[$(date '+%R')] \1> \2/p";;        *) echo "${MESSAGE}";;    esacdone

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.