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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- What's the tweeting protocol?
- Readers' Choice Awards
- New Products
- RSS Feeds
- Dart: a New Web Programming Experience
- Reply to comment | Linux Journal
10 hours 53 min ago - Reply to comment | Linux Journal
13 hours 25 min ago - Reply to comment | Linux Journal
14 hours 43 min ago - great post
15 hours 17 min ago - Google Docs
15 hours 40 min ago - Reply to comment | Linux Journal
20 hours 28 min ago - Reply to comment | Linux Journal
21 hours 15 min ago - Web Hosting IQ
22 hours 49 min ago - Thanks for taking the time to
1 day 26 min ago - Linux is good
1 day 2 hours ago
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




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