<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: pipes code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 03:00:47 GMT</pubDate>
    <description>DZone Snippets: pipes code</description>
    <item>
      <title>Twitter bot weatherlisbon</title>
      <link>http://snippets.dzone.com/posts/show/4159</link>
      <description>This little bash script shows how to use curl, grep, tail, sed and perl one-liners in order to compose a bleeding-edge twitter bot.&lt;br /&gt;This one returns daily weather forecasts for Lisbon city based on the BBC weather forecast rss feed.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#! /bin/sh&lt;br /&gt;&lt;br /&gt;#Goto here&lt;br /&gt;here=/home/guillaume/Personal&lt;br /&gt;cd $here&lt;br /&gt;&lt;br /&gt;#BBC Lisbon weather id&lt;br /&gt;id=0048&lt;br /&gt;&lt;br /&gt;#BBC weather RSS feed address&lt;br /&gt;feed="http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/${id}.xml"&lt;br /&gt;&lt;br /&gt;#City&lt;br /&gt;city=lisbon&lt;br /&gt;&lt;br /&gt;#temporary file&lt;br /&gt;file="weather${city}"&lt;br /&gt;&lt;br /&gt;#Weather twitter bot&lt;br /&gt;twitbot=weatherlisbon:*******&lt;br /&gt;&lt;br /&gt;#Timestamp the log file&lt;br /&gt;echo .&gt;&gt; $file.log&lt;br /&gt;date &gt;&gt; $file.log&lt;br /&gt;&lt;br /&gt;#Read the RSS feed and filter it&lt;br /&gt;curl $feed | grep 'title' | tail -n 1 | perl -wlne'm/title&gt;(.*)&lt;\/title/i &amp;&amp; print $1' | sed -e "s/&amp;#xB0;//g" &gt; $file.txt&lt;br /&gt;&lt;br /&gt;#Read the forecast into a weather variable&lt;br /&gt;read weather &lt; $file.txt&lt;br /&gt;&lt;br /&gt;#Twit the weather variable away&lt;br /&gt;curl --basic --user $twitbot --data status="$weather" http://twitter.com/statuses/update.xml &gt;&gt; $file.log&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 18 Jun 2007 21:37:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4159</guid>
      <author>griflet (guillaume riflet)</author>
    </item>
    <item>
      <title>Simple popen2 implementation</title>
      <link>http://snippets.dzone.com/posts/show/1134</link>
      <description>I needed a simple popen2 implementation. This is similar to popen, but allows for bidirectional communication with the application being executed. Based on some other examples, I put this together. It makes for a good base.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;sys/types.h&gt;&lt;br /&gt;#include &lt;fcntl.h&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;unistd.h&gt;&lt;br /&gt;&lt;br /&gt;#define READ 0&lt;br /&gt;#define WRITE 1&lt;br /&gt;&lt;br /&gt;pid_t&lt;br /&gt;popen2(const char *command, int *infp, int *outfp)&lt;br /&gt;{&lt;br /&gt;    int p_stdin[2], p_stdout[2];&lt;br /&gt;    pid_t pid;&lt;br /&gt;&lt;br /&gt;    if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)&lt;br /&gt;        return -1;&lt;br /&gt;&lt;br /&gt;    pid = fork();&lt;br /&gt;&lt;br /&gt;    if (pid &lt; 0)&lt;br /&gt;        return pid;&lt;br /&gt;    else if (pid == 0)&lt;br /&gt;    {&lt;br /&gt;        close(p_stdin[WRITE]);&lt;br /&gt;        dup2(p_stdin[READ], READ);&lt;br /&gt;        close(p_stdout[READ]);&lt;br /&gt;        dup2(p_stdout[WRITE], WRITE);&lt;br /&gt;&lt;br /&gt;        execl("/bin/sh", "sh", "-c", command, NULL);&lt;br /&gt;        perror("execl");&lt;br /&gt;        exit(1);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    if (infp == NULL)&lt;br /&gt;        close(p_stdin[WRITE]);&lt;br /&gt;    else&lt;br /&gt;        *infp = p_stdin[WRITE];&lt;br /&gt;&lt;br /&gt;    if (outfp == NULL)&lt;br /&gt;        close(p_stdout[READ]);&lt;br /&gt;    else&lt;br /&gt;        *outfp = p_stdout[READ];&lt;br /&gt;&lt;br /&gt;    return pid;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Simple usage would be:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int&lt;br /&gt;main(int argc, char **argv)&lt;br /&gt;{&lt;br /&gt;    int infp, outfp;&lt;br /&gt;    char buf[128];&lt;br /&gt;&lt;br /&gt;    if (popen2("sort", &amp;infp, &amp;outfp) &lt;= 0)&lt;br /&gt;    {&lt;br /&gt;        printf("Unable to exec sort\n");&lt;br /&gt;        exit(1);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    write(infp, "Z\n", 2);&lt;br /&gt;    write(infp, "D\n", 2);&lt;br /&gt;    write(infp, "A\n", 2);&lt;br /&gt;    write(infp, "C\n", 2);&lt;br /&gt;    close(infp);&lt;br /&gt;&lt;br /&gt;    *buf = '\0';&lt;br /&gt;    read(outfp, buf, 128);&lt;br /&gt;&lt;br /&gt;    printf("buf = '%s'\n", buf);&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 11 Jan 2006 04:40:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1134</guid>
      <author>chipx86 (Christian Hammond)</author>
    </item>
  </channel>
</rss>
