Question

I have Ubuntu Linux. I found one command will let me download unread message subjects from Gmail:

curl -u USERNAME:PASSWORD --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | sed -n "s/<title>\(.*\)<\/title.*name>\(.*\)<\/name>.*/\2 - \1/p"

...and then another command to let me send mail easily (once I installed the sendemail command via apt-get):

sendEmail -f EMAIL@DOMAIN.COM -v -t PHONE@SMS.COM -u Gmail Notifier -m test -s MAILSERVER:PORT -xu EMAIL@DOMAIN.COM -xp PASSWORD

(Note when in production I'll probably swap -v above with -q.)

So, if one command downloads one line subjects, how can I pipe these into the sendEmail command?

For instance, I tried using a pipe character between the two, where I used "$1" after the -m parameter, but what happened was that when I had no unread emails it would still send me at least one empty message.

If you help me with this, I'll use this information to share on StackOverflow how to build a Gmail Notifier that one can hook up to SMS messages on their phone.

Was it helpful?

Solution

I think if you mix viraptor & DigitalRoss' answers you get what you want. I created a sample test by creating a fake file with the following input:

File contents:

foo

bar

baz

Then I ran this command:

% cat ~/tmp/baz | while read x; do if [[ $x != "" ]]; then echo "x: '$x'"; fi; done

This will only print lines with input out. I'm not familiar with sendEmail; does it need the body to be on stdin or can you pass it on the cmdline?

OTHER TIPS

You do know you can do that directly in gmail by using a filter and your SMS email gateway, right?

But back to the question...

You can get control in a shell script for command output with the following design pattern:

command1 | while read a b c restofline; do
    : execute commands here
    : command2
done

Read puts the first word in a, the second in b, and the rest of the line in restofline. If the loop consists of only a single command, the xargs program will probably just do what you want. Read in particular about the -I parameter which allows you to place the substituted argument anywhere in the command.

Sometimes the loop looks like ... | while read x; do, which puts the entire line into x.

Try this structure:

while read line
do
    sendemailcommand ... -m $line ...
done < <(curlcommand)

I'd look at the xargs command, which provides all the features you need (as far as I can tell).

http://unixhelp.ed.ac.uk/CGI/man-cgi?xargs

Maybe something like this:

curl_command > some_file
if [[ `wc -l some_file` != "0 some_file" ]] ; then
  email_command < some_file
fi
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top