Bash Script to Post to Twitter

By sterlo in Bash on 06/03/2009 at 17:03

Views: 892
Tagged: twitter, bash, cli, command, line, interface
URL: http://www.sterlinghamilton.com/blog/article/bash_script_to_post_to_twitter/

Starter:

Here is a quick/easy way to post to twitter from the command line interface.

Put the script in /usr/local/bin/
Then it is as easy as:

user@river:~$ twitter Your Message Here

Main Course:

#!/bin/bash
 
# Login information.
USERNAME="email address"
PASSWORD="password"
URL=http://twitter.com/statuses/update.xml
 
# Check message length.
if [ ! -n "$1" ]; then
        echo "Message not long enough"
        exit
fi
 
# Check message length again.
message="$@"
maxlen=140;
len=`echo ${#message}`
 
if [ $len -gt $maxlen ]; then
        echo "Your message was longer than 140 characters...";
fi
 
# Post to Twitter.
result=`curl -u $USERNAME:$PASSWORD -d status="$message" $URL`

Expand Report Code | Install Coda Clip

Please log in to vote.

Compliments to the Chef

  1. Comment by Cody on 14/06/2009 at 08:33

    Well that is very nice, but twitter is a little hover-hyped no? Oh well, its fun though.

  2. Comment by Harald on 11/02/2010 at 07:39

    Works like a charm, great idea.

    Had to install "curl" on my Debian Lenny desktop. Very easy, just type "apt-get install curl".

    Would be great if it were also possible to automatically shorten urls.

    Maybe also add a version that reads the pw from the command line for security reasons.

How was your Meal?


  • Allowed HTML: <b><strong><u><i><em><a>

  • Why ask? It helps us stop spam comments.