Work the Shell - Our Twitter Autoresponder Goes Live!
The problem isn't that the data is being eaten, it's that the awk script is pairing the name information with the wrong tweet. Let's re-examine the awk script:
awk '{ if (NR % 4 == 0) {
printf ("name=%s; ", $0)
}
else if (NR % 4 == 1) {
printf ("id=%s; ",$0)
}
else if (NR % 4 == 2) {
print "msg=\"" $0 "\""
}
}'
NR%4=0 is correctly tagged as the name, NR%4=1 as the message ID, NR%4=2 as the msg, and NR%4=3 is skipped. (It's the Twitter user ID, not the tweet ID. It might be useful in a different context, but not for what we're doing.)
The problem is subtle, but it becomes obvious when you compare what the parser is generating against the actual tweets in the Twitter stream. We saw the first two like this:
id=7395681235; msg="African or European?" name=jeffrey; id=7395672894; msg="North Hall IStage"
But in fact, the tweet “African or European?” was sent by jeffrey, and the “North Hall IStage” was sent by the user identified in the subsequent line of parsed and formatted data.
Conclusion? We're splitting the data lines in the wrong place. Instead of adding the carriage return after NR%4==2 (it's subtle, we use print instead of printf), we actually should be adding it after the match for NR%4==0, like this:
awk '{ if (NR % 4 == 0) {
printf ("name=%s;\n", $0)
} else if (NR % 4 == 1) {
printf ("id=%s; ",$0)
} else if (NR % 4 == 2) {
printf ("msg=\"%s\"; ", $0) }
}'
Now, let's try that statement again:
id=7395681235; msg="African or European?"; name=jeffrey; id=7395672894; msg="North Hall IStage"; name=sideline;
Ah, that's the ticket!
With the problem solved, I'll remove the added debug statements and unleash the listener beast:
got name = jeffrey, id = 7395681235, and msg = African or European? got name = sideline, id = 7395672894, and msg = North Hall IStage got name = Genuine, id = 7395669466, and msg = ummmmm I know
Perfect. Bug debugged!
Now when we run the script, it correctly sees only the new tweets since it was last run, and it responds only to those it understands:
Twitter user @Larkin asked for the time @Larkin the time on our server is LOCALTIME Twitter user @jennyj asked for the time @jennyj the time on our server is LOCALTIME
Run the script again, and it sees only what's newer yet:
Twitter user @NoA asked for directions in tweet 7396527668 @NoA directions to our office are here: SOMEADDRESS
Perfect! Now, a tiny tweak. As we've debugged things, I have set the variable tweet to /bin/echo, so as not to flood my followers with unnecessary messages. Change it back to the tweet.sh script (as developed in an earlier series of columns), and the script actually responds with tweets.
The first run looks like this:
$ sh tweet-listen.sh Twitter user @mosa asked for directions in tweet 7396566048 (sent tweet @mosa directions to our office are here: SOMEADDRESS) Twitter user @xwatch asked for the time (sent tweet @xwatch the time on our server is TIME) Twitter user @NoA asked for directions in tweet 7396527668 (sent tweet @NoA directions to our office are here: SOMEADDRESS)
To ensure that it won't answer more than once to a tweet query, I'll run the script again:
$ sh tweet-listen.sh $
That's it! Now one tiny additional task is left, to add it to crontab so that it'll be an active listener, which is done by having it run every two minutes with the line:
*/2 * * * * bash $SCRIPTS/davesbot/tweet-listen.sh
That's all there is to it. Congratulations, we've just built a fully featured Twitterbot.
If you'd like to test it, it has its own account on Twitter, @davesbot. Start by sending a 2–3 word message, and it'll tell you what it can do. Grab the final source code from the LinuxJournal.com site at ftp.linuxjournal.com/pub/lj/listings/issue192/10711.tgz.
Dave Taylor has been hacking shell scripts for a really long time. 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.
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 |
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- RSS Feeds
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- Readers' Choice Awards
- The Secret Password Is...
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?




3 min 45 sec ago
2 hours 27 min ago
2 hours 30 min ago
2 hours 31 min ago
6 hours 56 min ago
8 hours 47 min ago
14 hours 56 sec ago
17 hours 12 min ago
19 hours 27 min ago
19 hours 56 min ago