Friday, July 15, 2016

Reading RSS with Bash

Over at Linux Journal, I recently wrote an article about Parsing an RSS News Feed with a Bash Script.

My background with Linux started with Unix systems. I was a Unix systems administrator for several years before I introduced Linux at work. Over my career, I've learned to do a lot of cool things in shell scripts.

My LJ article idea originated with an update to the FreeDOS website. Like many other project websites, we fetch our news from a separate news system (in our case, from SourceForge) then parse it to display on the front page. Today, we do that every time someone loads the page. Effective, but inefficient. As I update the FreeDOS website, I wanted to automate the news feed to generate a static news "include" file, so I decided to do it in Bash.

Go read the LJ article for details, but the secret is in this Bash function:

xmlgetnext () {
   local IFS='>'
   read -d '<' TAG VALUE
}

That function reads a line of input until the next less-than (<) symbol, then breaks it into TAG and VALUE at the first greater-than (>) symbol. That makes it really simple to read well defined XML data like RSS, where the interesting data is always enclosed by opening and closing tags, like this:

<link>http://www.example.com/link/to/news/item/</link>

The LJ article describes step-by-step how to build a Bash script to parse an RSS feed. I'm using the script on our test site, and it's working well!
image: Linux Journal

No comments:

Post a Comment