Wednesday, May 13, 2009

Twittering from the linux command line using curl

Recently I signed up for twitter and have found the need to "twit" from the command line. I found a quick tutorial on how to use curl to send twitter updates here: http://foxyurl.com/2CK

I've decided to extend it a little bit by adding some error checking. I called the script "twit". Here is the source code:


#!/bin/sh

user="someuser"
pass="somepass"
curl="/usr/bin/curl"

if [ ${#@} -gt 140 ]; then
echo "The update you are attempting to send is ${#@} characters long."
echo "Maximum update length is 140 characters. Twitter update was not sent!"
exit 1
fi

$curl --basic --user "$user:$pass" --data-ascii \
"status=`echo $@ | tr ' ' '+'`" \
"http://twitter.com/statuses/update.json" > /dev/null 2>&1

if [ $? -ne 0 ]; then
echo "Curl returned a non zero return code. Failed sending twitter update!"
exit 1
fi

exit 0



Nothing groundbreaking I know but it works as well enough.

I would probably make sure that you are the only one who can read this script as it has your password in plain text. To do this you can type: "chmod 700 twit".