1

I'm working with bash scripts and would like to embed a Python snippet inside a bash function.

So I got this working Python snippet, which simply reads from stdin and parse it to get the title of the entry[0]:

import feedparser, sys
root = feedparser.parse(sys.stdin.read())
print root['entries'][0].title

And everything seems fine with it:

$ curl -sf https://feedforall.com/sample.xml | python xmlparser.py
RSS Solutions for Restaurants

An IndexError happens when I execute this way:

$ curl -sf https://feedforall.com/sample.xml | python - <<EOF
import feedparser, sys
root = feedparser.parse(sys.stdin.read())
print root['entries'][0].title
EOF

Got this IndexError:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
IndexError: list index out of range

Seems like root['entries'] in this case is returning an empty list..and I don't know why.

Thanks for help

Hudson Santos
  • 155
  • 10

1 Answers1

0

Worked fine executing like that:

$ curl -sf https://feedforall.com/sample.xml | python <( cat <<EOF
import feedparser, sys
root = feedparser.parse(sys.stdin.read())
print root['entries'][0].title
EOF
)
Hudson Santos
  • 155
  • 10