Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a shell script to make writing my RSS a whole lot easier. Right now I am stuck at how to properly edit my files. See, I want the program to take in data from me, and then save those to variables. Then I want the shell script to navigate to my XML file, enter my variables ant given lines, and then save that new XML file with my latest post at the line under my <language></language> tags. Here is a view of my XML and Shell Script as they stand. I am currently using OSX and I know I use funny variable names :)

XML:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>CJ Cahala's Blog Feed</title>
<link>http://www.cjcahala.net/</link>
<description>This is my blog in RSS Form</description>
<lastBuildDate>Mon, 29 Sep 2014 09:16:00 GMT</lastBuildDate>
<language>en-us</language>
<item>
    <title>Hey Guys! Namecheap accepts Bitcoin! Awesome!</title>
    <link>http://namecheap.com/</link>
    <guid>http://namecheap.com/</guid>
    <pubDate>Thu, 2, Oct 2014, 00:15:00 GMT</pubDate>
    <description>Namecheap has web hosting and a payment option for Bitcoin!</description>
</item>
<item>
    <title>This is my first post</title>
    <link>http://www.cjcahala.net/resume.html</link>
    <guid>http://www.cjcahala.net/resume.html</guid>
    <pubDate>Mon, 29 Sep 2014 09:16:00 GMT</pubDate>
    <description>Hey there, this is my resume- check it out if you want!</description>
</item>
</channel>
</rss>

Shell:

#!/bin/bash 
read -p "Channel Title:" ct
read -p "Channel Description:" chand
read -p "Post Title:" pt
read -p "Post Link:" pl
read -p "Post GUID (same URL as link):" pg
read -p "Post Description:" pd
dater=`date`
dirt=/Users/cjcahala/Desktop/test.xml
echo "hello\n"$dater > $dirt
share|improve this question
    
There are plenty of languages with xml libraries that are much more suitable for the task of working with XML. –  jordanm Oct 4 '14 at 5:03
    
hmm, what would you think would be something better for automation like this? –  is-cj Oct 4 '14 at 17:57

2 Answers 2

Don't do XML like this. It's bad juju. XML is not a line oriented data structure, so what you're doing creates brittle code.

XML parsers are the way forwards. Most languages have them. I like Perl and XML::Twig.

#!/usr/bin/env perl

use strict;
use warnings;

use XML::Twig;

sub insert_new_post {
    my ( $twig, $lang_elt ) = @_;
    my $new_item = XML::Twig::Elt->new('item');
    #you can probably omit the 'last_child' field, as the RSS readers aren't 
    #going to care about ordering, probably. 
    $new_item->insert_new_elt( 'last_child', 'title', "New title" );
    $new_item->insert_new_elt( 'last_child', 'link',  "http://unix.stackexchange.com" );
    $new_item->insert_new_elt( 'last_child', 'guid', "Somenew GUID" );
    $new_item->insert_new_elt( 'last_child', 'pubdate', "Today or something" );
    $new_item->insert_new_elt( 'last_child', 'description', "Fishy fishy fishy" );
    print "Inserting:\n", $new_item->sprint, "\n";
    $new_item->paste_after($lang_elt);
}

my $twig = XML::Twig->new(
    'pretty_print'  => 'indented',
    'twig_handlers' => { 'language' => \&insert_new_post },
);
$twig->parsefile ( 'your_file.xml' );
$twig->print;  #prints to stdout. 
share|improve this answer

Not sure if an answer is still required for this. You can use xmlstarlet to edit an XML template. For example,

xmlstarlet edit --update '/rss/channel/title' --value "$ct" "$dirt"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.