标准linux sed无法在mac中工作

时间:2015-01-01 20:33:46

标签: linux macos bash sed

发现这种方式可以从gmail获取未读的电子邮件数,但它在我的mac上无效:

我认为mac上的sed实现可能不是'标准' Linux操作系统。

这是我正在努力的路线:

MAILCOUNTER=`curl -u $USERID:$PASSWORD --silent "https://mail.google.com/mail/feed/atom" \
   | sed -n 's|<fullcount>\(.*\)</fullcount>|\1|p'`


USERID=youremail@gmail.com
PASSWORD=yoursecretpassword

WAIT=10

# Loop to check for new mail every X minutes:
while [ "1" -eq "1" ]; do

    # Command line to fetch the number of unread emails:
    MAILCOUNTER=`curl -u $USERID:$PASSWORD --silent "https://mail.google.com/mail/feed/atom" \
| sed -n 's|<fullcount>\(.*\)</fullcount>|\1|p'`

    if [[ "$MAILCOUNTER" = "" ]]; then
        echo "ERROR: The program coulndn't fetch the account for user \"$USERID\"."
        echo "- Are you connected to the Internet?"
        echo -e "- Is the userid and password correct for \"$USERID\"?\n"
        curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount=-1&

    elif [[ "$MAILCOUNTER" -eq "0" ]]; then
        echo "* There is 0 new email for user $USERID."
        curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount=0&

    elif [[ "$MAILCOUNTER" -gt "0" ]]; then
        echo "* There is $MAILCOUNTER new email for user $USERID."
        curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount=$MAILCOUNTER&
    fi

    echo "* Waiting $WAIT seconds before checking for emails again."
    echo "* (^C to quit the program)"
    sleep $WAIT

done

像这样的卷曲错误,这让我疯了,因为在我的浏览器中输入URL会正确返回错误

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

网址返回:

<feed xmlns="http://purl.org/atom/ns#" version="0.3">
<title>Gmail - Inbox for myGmailName@gmail.com</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>1</fullcount>
<link rel="alternate" href="http://mail.google.com/mail" type="text/html"/>
<modified>2015-01-02T00:14:36Z</modified>
<entry>

我更正了谷歌邮件权限,现在卷曲返回

我从中得到:

curl -u MYUSRNAME:MYPASSWORD --silent "https://mail.google.com/mail/feed/atom"

这样:

<?xml version="1.0" encoding="UTF-8"?><feed version="0.3" xmlns="http://purl.org/atom/ns#"><title>Gmail - Inbox for MYEMAIL@gmail.com</title><tagline>New messages in your Gmail Inbox</tagline><fullcount>1</fullcount><link rel="alternate" href="http://mail.google.com/mail" type="text/html" /><modified>2015-01-02T02:03:50Z</modified><entry><title>test</title><summary>test</summary><link rel="alternate" href="http://mail.google.com/mail?account_id=MYEMAIL@gmail.com&amp;message_id=14aa8623548638bc&amp;view=conv&amp;extsrc=atom" type="text/html" /><modified>2015-01-02T02:03:36Z</modified><issued>2015-01-02T02:03:36Z</issued><id>tag:gmail.google.com,2004:1489150113099430076</id><author><name>the author</name><email>themail@email.com</email></author></entry></feed>MacBookPro-2:arduino_gmail_checker admin$ 

我只想在这里输入数字:

<fullcount>1</fullcount>

2 个答案:

答案 0 :(得分:3)

基于Unix标准的Mac sed和BSD之间存在差异,sed的Linux版本基于gnu并且包含许多不同的扩展与其他版本兼容。但是,查看代码时,实际上并没有任何关于它们不应该在Mac上工作的内容。

如果看到错误消息有助于了解正在发生的事情。我怀疑这不是Mac / sed问题,因为你没有正确设置。使用curl命令绝对没有错误检查。如果其中一个命令失败,它将继续快速继续,并将输出传递给sed。您确定这些curl命令正在获取数据吗?

选择具有sed命令的行,并尝试在没有sed的情况下手动运行它:

例如:

MAILCOUNTER=`curl -u $USERID:$PASSWORD --silent "https://mail.google.com/mail/feed/atom" \
| sed -n 's|<fullcount>\(.*\)</fullcount>|\1|p'`

从命令行尝试以下命令:

$ USERID=xxxxxx
$ PASSWORD=xxxxxx
$ curl -u $USERID:$PASSWORD --silent "https://mail.google.com/mail/feed/atom"

curl命令会产生什么吗?如果没有,则问题不是sed,而是curl

您还可以尝试将set -xv添加到脚本的开头,然后set +xv添加到结尾。这会打开shell调试。每行将在执行前打印,然后再次打印并插入所有变量。这有助于您跟踪代码中的错误。


  

好的,所以这是GMAIL的权限,我通过放松安全性来解决,现在curl在编辑的帖子中返回如上所述

很高兴我们得到了解决。这是我用来计算的命令:

$ curl -s  -u$username:$password "https://mail.google.com/mail/feed/atom" \
| xmllint --format - \
| sed -n 's#<fullcount>\(.*\)</fullcount>#\1#p'

-s中的curl会阻止进度报告。 xmllint命令重新格式化输出以使sed更容易使用。要查看其功能,请尝试:

$ curl -s  -u$username:$password "https://mail.google.com/mail/feed/atom"

VS

$ curl -s  -u$username:$password "https://mail.google.com/mail/feed/atom" \
| xmllint --format -

答案 1 :(得分:1)

Mac sed实现基于BSD sed,与Linux sed略有不同。 最简单的解决方案是安装gsed(GNU sed,例如Mac Ports提供)