UNIX chat shell code

[problem]

As most UNIX bods know, with fairly recent audit lock downs – talkd has been disabled.

But you want to chat remotely with someone, i.e. another techie bod! 🙂

[/problem]

[solution]

Now where is the fun in that! 🙂

Don’t know about you, but quite often I’ve needed to talk to other people remotely and don’t want to hold a phone or use hands free.

See the example.

[/solution]

[example]

Here is a quick and dirty solution, that works with bash – should work with zsh too.

I welcome any enhancements or suggestions, via the comments.


#!/bin/bash

trap 'kill $tailpid;echo "*$nick leaves chat*" >> ${chatdir}/discussion;
rm ${chatdir}/.${nick};exit 0' 2

chatdir="/var/tmp/unixchat"

[ -d $chatdir ] || {
mkdir $chatdir || (echo "failed to mkdir $chatdir";exit 1)
}

chmod 777 /var/tmp/unixchat

while [ $(echo X${nick} | grep -c "^X$") -eq 1 ]
do
echo -n "Nick: "
read nick

[ -f "${chatdir}/.${nick}" ] && {
echo "$nick already exists, try another";nick=""
}

done

touch ${chatdir}/.${nick}

[ ! -f ${chatdir}/discussion ] && {
touch ${chatdir}/discussion
chmod 666 ${chatdir}/discussion
}

echo "*$nick joins chat*" >> ${chatdir}/discussion

tail -1000f ${chatdir}/discussion&
tailpid=$!

echo ">> type quit to exit <<"

while [ $(echo X${entry} | grep -c "^Xquit$") -ne 1 ]
do
read entry
echo "[${nick}@`date '+%d-%b %H:%M:%S'`] says: $entry" >> ${chatdir}/discussion&

done

kill $tailpid; rm ${chatdir}/.${nick}

echo "*$nick leaves chat*" >> ${chatdir}/discussion

exit 0

Here is a screen shot:

UNIX chat code - simple solution written in shell

[/example]

[reference]

[tags]UNIX, Chat code, Shell Script[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *