【问题标题】:Standard linux sed not working in mac标准linux sed在mac中不起作用
【发布时间】:2015-02-28 07:52:04
【问题描述】:

发现这种方法可以从 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

像这样的 curl 错误,这让我抓狂,因为在我的浏览器中输入 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>

我更正了 google 邮件权限,现在 curl 返回了

我从中得到:

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>

【问题讨论】:

  • 它以什么方式“不起作用”?如果您将车开到车库并要求机械师修理它,因为它“不起作用”,您会期望他们能够帮助您吗?

标签: linux macos bash sed


【解决方案1】:

Mac 的 sed 基于 Unix 标准和 BSD 与 Linux 版本的 sed 基于 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'

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

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

对比

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

【讨论】:

  • 好的,所以这是 GMAIL 的权限问题,我通过放松安全性解决了这个问题,现在 curl 在编辑后的帖子中返回如上
  • 感谢,特别是向我展示了其他部分。感谢您的帮助!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-02-10
    • 2014-09-27
    • 2022-01-24
    • 2016-01-12
    • 2014-10-19
    • 1970-01-01
    • 2011-08-07
    • 2012-01-03
    相关资源
    最近更新 更多