Linux - Send STDOUT of command to an RSS Feed

杀马特。学长 韩版系。学妹 提交于 2019-12-19 12:04:09

问题


I'm looking to use a personal RSS Feed for system reporting, so I'm wondering if it's possible to create a script that sends its $1 to an RSS feed, ala self_test_command > rss_report.sh. I don't currently have an RSS feed set up, either, so what would be the easiest way to set up an RSS feed running from a Linux box?


回答1:


I have a proper solution for you, in command line. That use Perl Template::Toolkit module in background (no need to learn Perl just now) :

first install the package perl-template-toolkit, then create the template file rss.tpl :

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>[% title %]</title>
    <description>[% desc %]</description>
  </channel>
  <!-- rest of the RSS -->
</rss>

And run the command :

tpage --define title=foobar --define desc=description --interpolate rss.tpl

Output is :

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>foobar</title>
    <description>description</description>
  </channel>
  <!-- rest of the RSS -->
</rss>

You will find a complete template to modify here




回答2:


There is another solution using xmlstarlet:

Create an initial rss feed file feed.xml:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>My RSS Feed</title>
    <description>This is my RSS Feed</description>
  </channel>
</rss>

Create a shell script that uses xmlstarlet to add items:

#!/bin/sh

TITLE="My RSS entry"
LINK="http://example.com/entry4711"
DATE="`date`"
DESC="Good news"
GUID="http://example.com/entry4711" 

xmlstarlet ed -L   -a "//channel" -t elem -n item -v ""  \
     -s "//item[1]" -t elem -n title -v "$TITLE" \
     -s "//item[1]" -t elem -n link -v "$LINK" \
     -s "//item[1]" -t elem -n pubDate -v "$DATE" \
     -s "//item[1]" -t elem -n description -v "$DESC" \
     -s "//item[1]" -t elem -n guid -v "$GUID" \
     -d "//item[position()>10]"  feed.xml ; 

To have a generic solution one would prefer passing the parameters from the commandline of course.

The -d command ensures that the feed does not grow inifinet but has at most 10 items.



来源:https://stackoverflow.com/questions/12827343/linux-send-stdout-of-command-to-an-rss-feed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!