Post to Twitter from a Linux Shell

I was having a very slow morning at work and I started experimenting with sending tweets from the command line in Linux. I realize this is a little silly and there are plenty of good twitter apps out there but I found it amusing so I decided to post it.


NOTE: You must have curl installed for this to work but I believe its standard on most Linux distros.

The first way to do it is just a one liner:

curl -u user:pass -d status="Here is my tweetl" http://twitter.com/statuses/update.xml

The only problem with the one liner is that it doesnt check to make sure your tweet is 140 chars or less and it also gives a bunch of xml output after it posts to the twitter API.

So I decided to whip up a quick shell script:

#!/bin/bash
host=http://twitter.com/statuses/update.xml
max=140
echo "Please enter your twitter username:  "
read user
while [ "$user" == "" ];
do
                echo
                echo "Please enter your twitter username:  "
                read user

done

echo "Please enter your password:  "
read passwd
while [ "$passwd" == "" ];
do
                echo
                echo "Please enter your password:  "
                read passwd

done

echo "Enter a status update (140 chars max):  "
read status

while [ "$status" == "" ];
do
                echo
                echo "Enter a status update (140 chars max):  "
                read status

done

while [ ${#status} -gt  $max ];
do
                echo
                echo "Your status is longer then 140 chars"
                echo -en "nEnter a status update (140 chars max):  "
                read status
done
curl -u  $user:$passwd -d status="$status" $host  > /dev/null 2>&1

Just save the script and call it something like twitter_shell.sh and make it executable:

chmod 755 twitter_shell.sh

Thats it, now you can run the script when you are working on Linux servers and make twitter updates when you should be working!

The Twitter Book (Paperback)

By (author): Tim O'Reilly, Sarah Milstein


List Price: $19.99 USD
New From: $0.79 USD In Stock
Used from: $0.01 USD In Stock


List Price: $16.95 USD
New From: $5.91 USD In Stock
Used from: $0.01 USD In Stock

Share