Tweeter catches featuritis

One of the strengths of Linux lies in the abundance of commandline utils that you can chain together through pipes. Instead of installing big monstrous monolithic applications one simply pipes the output of one lean and fast util into the input of the next equally lean and fast util.

In the first installment I introduced Tweeter a bash script that chains several commandline utils into a one-trick app that posts updates to Twitter.

In this installment Tweeter – the little bashscript that can catches a dose of creeping featuritis and has a char-counter added to it. The better to be able to monitor if our tweet is within Twitter’s constraint of 140 chars.

The trick is pretty simple. We use the bash built-in echo to pipe our tweet to wc the word-counter and store the result in the variable a. By passing the -m switch to wc we make sure that it only counts characters instead of its default behaviour of returning a word and line count as well.

This line in the script does the trick:
a=$(echo -n "$tweet" | wc -m)

Next we test with if [[ $a -le 140 ]] wether our tweet is within Twitters constraint of 140 chars. If all is well the tweet is passed to curl which transports it to Twitter. In the case of an overrun the script returns an error message and passes the tweet back to stdout so we can grab it and edit it.

The observant reader will also notice that read has acquired a new switch, namely -e which makes read use the goodness of the readline library, thus giving us access to all the editing shortcuts of an interactive shell’s commandline.

Tweeter Mk II


#!/bin/bash
read -e -p "Tweet: " rtweet
tweet=${rtweet[*]}
a=$(echo -n "$tweet" | wc -m)
if [[ $a -le 140 ]]
then
REP=$(curl -s -S -u username:password -d status="$tweet"\
http://twitter.com/statuses/update.json)
EXP=$(expr match "$REP" ".*id\":\([0-9]*\)")
sqlite3 ~/dbs/tweets<<EOF
insert into mems values("$EXP","$tweet");
EOF
else
b=$(($a - 140))
echo "$b extraneous chars in: $tweet"
fi
exit;

Still pretty crude, isn’t it? Well, lets see what we can improve in the next installment.

ShortURL for the permalink to this post: http://bit.ly/qwfGe

Advertisements

About dozykraut

Proud member of Hillbilly's on Linux, promoting open source redneckism in remote parts of the Milky Way.
This entry was posted in Bash and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s