It's looking good!
Perhaps a bit of refactoring with regards to the three calls to sed;
Code: Select all
#!/bin/bash
# set some variables
authority="USER:PASS@"
url="https://${authority}mail.google.com/mail/feed/atom"
db_file=/tmp/db.txt
# set IFS to newline for slurping lines into arrays
OIFS="$IFS" IFS=$'\n'
# get messages
message=($(curl -s $url | tee $db_file| sed -nr '
#
# each message should resemble this: title@@link@@name
#
# only interested in tags between entries
/<entry>/,/<\/entry>/{
# bodge link tag to look like title or name
s/<link.*href="([^"]*)".*\/>/<link>\1<\/link>/
# extract details and add place holder
s/<(title|link|name)>(.*)<\/\1>/\2@@/
# save lines with a placeholder into HS
/@@/ H
# end of an entry: process HS
/<\/entry>/ {
# overwrite PS with HS
g
# drop newlines and final placeholder
s/amp;|\n|@@$//gp
# clear PS
s/.*//
# swap empty PS into HS
x
}
}'))
# reset IFS (be kind to the maintainer!)
IFS="$OIFS"
# show number of messages
echo "${#message[@]} messages"
# show messages
for (( i=0; i<${#message[@]}; i++)){
msg=${message[i]} tmp=${msg#*@@}
author=${msg##*@@} subject=${msg%%@@*} link=${tmp%@@*}
printf "[%02d] %s | %s\nURL: %s\n\n" $(( i + 1 )) ${author:-"no author"} \
${subject:-"no subject"} ${link:-"no link"}
}
The definitive sed site is here
Check out the lookup table method here
HTH
Potong
p.s. you often see
Code: Select all
echo "$some_variable"| whatever ....
Code: Select all
whatever... <<<"$some_variable"