TechRepublic : A ZDNet Tech Community

Linux and Open Source

Host: Jack Wallen
Contact

Tweeting from the command line

Twitter is one of the biggest and fastest-growing social media sites currently online. If you’re not already “tweeting,” you should know that Twitter is a micro-blog: short little posts, called tweets, allow you to share what you are doing or thinking with others. The posts are 140 characters at a maximum, and you can follow other people on Twitter, as well as have other people follow you.

A number of companies and projects are starting to use Twitter as a means of making announcements, which means that you can follow projects or organizations that interest you to see what is going on with them. Twitter is a Web-based service, so you log in to tweet and to look at the folks you are following. Through its available API, however, you can use other clients to send your tweets and read Twitter.

If you are more interested in sharing your thoughts or project announcements than in reading other people’s tweets, there is a very easy way to accomplish this: using the command-line.

The script you will see takes exactly one argument: your tweet. You use it like this:

$ ~/bin/tweet "Writing my TechMails"
Successful tweet!

Once you run the script and look on Twitter, your post is there for all to see. The script itself uses nothing fancier than cURL to post the text provided to the script. The text must observe shell constraints: it must be properly escaped for special characters (such as “!” and “?”). The script doesn’t require the tweet to be in quotes; you could use:

$ ~/bin/tweet Does my tweeter need quotes\?
Successful tweet!

and it would work just as well.

The script itself:

#!/bin/sh
tweet="${@}"
user="username"
pass="sekret"
if [ $(echo "${tweet}" | wc -c) -gt 140 ]; then
    echo "FATAL: The tweet is longer than 140 characters!"
    exit 1
fi
curl -k -u ${user}:${pass} -d status="${tweet}" https://twitter.com/statuses/update.xml >/dev/null 2>&1
if [ "$?" == "0" ]; then
    echo "Successful tweet!"
fi

Output of the cURL command is directed to /dev/null because it returns some XML that we don’t need to care about. The script also makes sure that the tweet is 140 characters or less, and exits with an error if it is longer.

The applications for this very simple script can be quite interesting. You could hook this into a subversion repository to notify of new commits to your project or you could put together a script to take the title of a new regular blog post and turn it into a tweet using a URL shortening service to generate a link — there are a number of ways to automate interesting things with this script.

Get the PDF version of this tip here.

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years. Read his full bio and profile.

Print/View all Posts Comments on this blog

Tweeting from the command line b4d93r | 08/17/09
Cygwin? vdanen | 08/18/09
why? csmith.kaze | 08/18/09
Overstatement, combined with false assumption. Palmetto | 08/18/09
yea csmith.kaze | 08/19/09
Not stupid, ignorant Palmetto | 08/19/09
tranny! csmith.kaze | 08/19/09
Oh, I'm with you on purposeful ignorance, Palmetto | 08/19/09
replying to palm, above csmith.kaze | 08/19/09
RE: tranny! Ralph.Stoos@... | 08/20/09
Windows users . . . pkvelins@... | 08/19/09
Thats a good stuff altax | 08/20/09
RE: Tweeting from the command line Andrew Mager | 08/17/09
Is this the Week of the Tweet? csmith.kaze | 08/18/09
Not only persons can tweet amabilis@... | 08/18/09
tweet v. email csmith.kaze | 08/18/09
Costs? rmerchberger@... | 08/18/09
So don't use it Thrash Cardiom | 08/18/09
so what you are saying csmith.kaze | 08/19/09
Not at all Thrash Cardiom | 08/19/09
You're kidding?!?!? Palmetto | 08/18/09
some links csmith.kaze | 08/19/09
Thanks. Palmetto | 08/19/09
Just tryng to be cool The 'G-Man.' | 08/20/09
It's not just you pkvelins@... | 08/19/09
RE: Tweeting from the command line dshepherd@... | 08/18/09
Cool DT2 | 08/18/09
while cool idea csmith.kaze | 08/18/09
I can't speak for other backup utilities, Palmetto | 08/18/09
that's what i meant EOM csmith.kaze | 08/19/09
Not to pick nits.. david.goodwin@... | 09/08/09
But The 'G-Man.' | 08/20/09
@dshepherd. RE: Tweeting from the command line corePro | 08/24/09
RE: Tweeting from the command line morourke@... | 08/18/09
RE: Tweeting from the command line dennyboy@... | 09/02/09

What do you think?

White Papers, Webcasts, and Downloads

Recent Entries

TR on Twitter

Archives

TechRepublic Blogs



Quick Reference: Linux Commands
Reduce stress and speed up resolutions with the easiest command references right at your fingertips. You'll receive a PDF file covering Linux, packed with the most common commands you'll need and use daily.
Buy Now
500 Things Every Technology Professional Needs to Know
Did you know Microsoft's RegClean does not work with XP but you can use shareware to clean your registry? Did you know most wireless access points don't have encryption enabled by default? Did you know there are 500 tidbits of information contained in TechRepublic's 500 Things Every Technology Professional Needs to Know that will help you become a successful IT professional.
Buy Now

SmartPlanet

Click Here