Work the Shell - Twittering from the Command Line
If you've been around the Linux and UNIX world as long as I have (is it really going on 30 years? How could that be?), you'll be familiar with the various attempts at multiperson chat that have come down the pipeline, from simple command-line tools to the curses-based “talk” program to Internet Relay Chat (IRC) chaos. Occasionally fun, but often a complete waste of time, there's still something appealing about having an open line with a circle of friends and colleagues.
A few years ago, that mantle was assumed by the status line in Facebook, where hard-core users update their status throughout the day to reflect the meetings they're attending, conferences they're involved with, dates with their spouses or significant others, concerts, fights with parents and so on. The problem is, that's useful only if the people in your circle are also rabid Facebook fanatics—a shortcoming that's true of any of these services, of course.
Simultaneously, flashmob instigators found that Web-based tools could help them organize, and services like Dodgeball were created. (A flashmob is a spontaneous gathering of people organized by cell phone or text messaging.) Dodgeball was bought by Google and then strung out to die, but the meme of status messages as a form of shared communication continued to evolve, and the latest evolution is a weird, sometimes overly voyeuristic, on-line service called Twitter (visit twitter.com).
During the past few months, I have found Twitter oddly compelling, in a manner perhaps analogous to Jimmy Stewart being unable to tear himself away from his binoculars in Rear Window (even while the breathtakingly gorgeous Grace Kelly was administering to him, but that's another column entirely). Twitter is immediately useful if a group of people are at a conference, allowing you to meet up easily for meals, evening activities, shared cab rides and so on, but it's also rather fun to keep a running commentary of your goings-on and know what your friends and associates are doing too.
Twitter works directly from a Web page and also is completely short message service (SMS)-compliant too, so it's extraordinarily cell-phone-friendly, adding significantly to its utility.
Okay, nice history lesson. What about some sort of shell script, Dave?
What makes Twitter interesting to me is that like so many modern Web services, it has a public application programming interface (API) that makes it both easy and fun to hack and fiddle with. Although some of the so-called Web 2.0 services are still closed, more and more are making their back ends accessible to open queries, creating many possibilities for darn interesting shell scripts and more sophisticated software and mashups.
For this column, I want to show you how incredibly easy it is to update your Twitter status from the Linux command line, and then noodle a bit on how this could be used for useful, interesting or just mundane tasks. To tantalize you, imagine that you could launch a big software build and have it text your cell phone when it was done, rather than you having to sit at your office.
First, though, you can find the API at twitter.com/help/api. Read through it, and you'll find that just about all interactions are done with specially constructed URLs. That's good, because it's easy to manipulate and tweak a string within a shell script.
The most basic use of the Twitter API is to send a status update message to your account. Here's an example of how to do that:
curl --basic --user "$user:$pass" --data-ascii \ "status=testing123" http://twitter.com/statuses/update.json
You can see one of the big challenges of writing this as a shell script already. User validation is done through sending an account name and password pair, and that means you're probably going to have the password embedded in your script. Never a good idea. But, prompting for it each and every time you want to send an update isn't good either.
Before we address that though, notice how I'm utilizing the wonderful curl utility—a must-have for your Linux distro. If you don't have it, grab a copy from curl.haxx.se. curl makes it very easy to work with Web pages via the command line, and I consider it essential for any modern shell script programmer.
Looking back at the command invoked, you'll notice that the URL to which we are going to send the update is status/update.json. Read the API, and you'll notice that it supports four different output formats, all of which are a pain to parse within a script, unfortunately. One of those is json, and it re-occurs here as the update-receiving URL address.
If you've already worked with Web sites from the command line, you know there are lots of illegal characters that cannot be included in URLs and, by extension, on command lines of utilities that interact with the Web, such as curl. As a result, one of the tasks of our send.twitter.update script will be to make all of the necessary substitutions before sending the new status message to the Twitter server.
On a lightweight service like Twitter, I think it's probably crazy to go through too many hoops to ensure security, so I actually will be including the account name and password in the script. Given some of the suggested applications we'll explore later, it makes sense to create a new Twitter account just for the command-line updates, in which case, a shared password isn't that big a problem anyway.
Here's a first stab at a simple stu (sent twitter update) script:
#!/bin/sh user="DaveTaylor" pass="--mypw-- " curl="/usr/bin/curl" $curl --basic --user "$user:$pass" --data-ascii \ "status=`echo $@ | tr ' ' '+'`" \ "http://twitter.com/statuses/update.json" exit 0
In use, simply type in the script name and desired status update:
$ stu Writing makes me sleepy
{"user":{"name":"Dave Taylor","description":"Blogger, entrepreneur,
public speaker,
dad!","screen_name":"DaveTaylor","profile_image_url":"http:\/\/s3.amazon
aws.com\/twitter_production\/profile_images\/35534842\/dticon_normal.gif"
,"location":"Boulder,
Colorado","url":"http:\/\/www.AskDaveTaylor.com\/","id":9973392,"protect
ed":false},"created_at":"Sat Jan 12 21:31:37 +0000
2008","truncated":false,"text":"Writing makes me
sleepy","source":"web","id":592217322}
$
Eek. That's a scary output, isn't it? So, before wrapping up this column, I strongly suggest that immediately after the invocation of curl, you append >& /dev/null, so you can discard the output. If you want to be fancy, check $? to see whether it's nonzero, but let's talk about that level of improvement in the next column.
Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com. Follow him on Twitter if you'd like: twitter.com/DaveTaylor.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
- Roll your own dynamic dns
1 hour 45 min ago - Please correct the URL for Salt Stack's web site
4 hours 56 min ago - Android is Linux -- why no better inter-operation
7 hours 12 min ago - Connecting Android device to desktop Linux via USB
7 hours 40 min ago - Find new cell phone and tablet pc
8 hours 38 min ago - Epistle
10 hours 7 min ago - Automatically updating Guest Additions
11 hours 16 min ago - I like your topic on android
12 hours 2 min ago - This is the easiest tutorial
18 hours 38 min ago - Ahh, the Koolaid.
1 day 16 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
twitter
i gust want to say some thing "great job"
Update your Twitter randomly according to your intrest Or, from Rss Feed Or, from your own tweet message list Or, Any combination of the above three http://feedmytwitter.com