Grep result to a shell variable – no line breaks

bash-shellThere was this website containing regularly updated content I wanted to follow. Unfortunately, they had no RSS feed available and I didn’t feel like checking on the website every now and then. Also, what I needed was just the posts containing particular keywords in the Title. So I wrote a tiny script to do the mundane task for me and let me know when the keywords I was interested in would appear:

#!/bin/bash

TMPFILE=`mktemp /tmp/website_content.XXXXXX` || exit 1

wget --output-document="$TMPFILE" --user-agent="Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.1.0" http://someinterestingurl.com/

RESULT=`cat $TMPFILE | grep -i 'keyword-1\|keyword-2\|keyword-3'`

if [[ `echo ${#RESULT}` -gt "0" && `echo $RESULT | wc -l` -ge "1" ]]; then
	echo "yes"
	echo $RESULT | mail -s "new keywords found" example@example.com
fi

rm $TMPFILE

However, grep would return only one line with all the results when assigning the result to the $RESULT variable, not line by line as it deos when printing to the standard output. The problem turned out to be bad usage of echo. echo $RESULT had to be replaced by echo “${RESULT}, my bad, after that the script worked just fine.

#!/bin/bash

TMPFILE=`mktemp /tmp/website_content.XXXXXX` || exit 1

wget --output-document="$TMPFILE" --user-agent="Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.1.0" http://someinterestingurl.com/

RESULT=`cat $TMPFILE | grep -i 'keyword-1\|keyword-2\|keyword-3'`

if [[ `echo ${#RESULT}` -gt "0" && `echo "${RESULT}" | wc -l` -ge "1" ]]; then
	echo "yes"
	echo "${RESULT}" | mail -s "new keywords found" eaxmple@example.ccom
fi

rm $TMPFILE
Tagged with: , ,