This is a script that must send an email at each new article published on a specific website. Any suggestions or improvements to do?

SENDER_EMAIL="sender@example.com"
TO_EMAIL="myemail@example.com"
RSS_SITE="example.com/feed.xml"
CHECK_INTERVAL=10

while [ 1 ]; do

    LINK_ARTICLE=$(rsstail -i 1 -u $RSS_SITE -l -n 0 -1 | grep -oP "Link:+ \K.*")
    TITLE_ARTICLE=$(rsstail -i 1 -u $RSS_SITE -n 0 -1 | grep -oP "Title:+ \K.*")

    if [ "$LINK_ARTICLE" != "" ] && [ "$TITLE_ARTICLE" != "" ]; then
        echo "New article published on the site. TITLE: $TITLE_ARTICLE - LINK: $LINK_ARTICLE" | EMAIL="$SENDER_EMAIL"  mutt -s "Nuovo Articolo BDO" "$TO_EMAIL"
        echo "New article published on the site. TITLE: $TITLE_ARTICLE - LINK: $LINK_ARTICLE"
    fi

    sleep $CHECK_INTERVAL
done
up vote 6 down vote accepted

I see a number of things that may help you improve your code.

Use "shebang" line

The shebang is the line at the beginning of a shell script that tells which program to use. In this case, you probably want this:

#! /usr/bin/env bash

See this question for details.

Consider using cron instead of sleep

If this is something you want to run automatically, consider running it as a cron tab instead of using sleep within the script.

Include some comments

The program requires rsstail, mutt and sleep which is a requirement that should be documented in a comment.

Be cautious about handing variables to programs

The mutt program, like many Linux programs, has a -- option which specifies that no further options are on the command line. This prevents the contents of $TO_EMAIL in a line like the following from being misinterpreted as a command line option.

mutt -s $TITLE -- $TO_EMAIL < $BODYTEXT

Combine strings

The echo is used twice with an identical string. An alternative approach is

TITLE="Nuovo Articolo BDO"
BODYTEXT="New article published on the site. TITLE: $TITLE_ARTICLE - LINK: $LINK_ARTICLE"
mutt -s $TITLE -- $TO_EMAIL < $BODYTEXT
echo $BODYTEXT

Avoid creating extraneous variables

Instead of creating SENDER_EMAIL, you could just specify EMAIL and then the reassignment of the latter variable before mutt is called would not be necessary.

Consider writing a portable script

By sticking closely with Posix and avoiding bashisms your code could run on many different kinds of systems, including recent versions of Ubuntu which don't use bash.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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