- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
发现这种方法可以从 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&message_id=14aa8623548638bc&view=conv&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>
最佳答案
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 调试。每行将在执行前打印,然后再次打印所有内插变量。这将帮助您追踪代码中的错误。
OK so it was a permissions thing with GMAIL, which I solved by loosening the security, and now curl returns as above in the edited post
很高兴我们解决了这个问题。这是我用来获取计数的命令:
$ 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 -
关于linux - 标准的 linux sed 在 mac 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734415/
我最近在读 CSAPP。在 10.9 节中,它说标准 I/O 不应该与 socket 一起使用,原因如下: (1) The restrictions of standard I/O Restricti
似乎是一个足够标准的问题,可以保证解决方案中的标准设计: 假设我想在文件中写入 x+2(或更少)个字符串。 x 字符串构成一个部分的内容,这两个字符串构成该部分的页眉和页脚。要注意的是,如果内容中没有
代码版本管理 在项目中,代码的版本管理非常重要。每个需求版本的代码开发在版本控制里都应该经过以下几个步骤。 在master分支中拉取该需求版本的两个分支,一个feature分支,
我有以下sql查询,我需要获取相应的hibernate条件查询 SELECT COUNT(DISTINCT employee_id) FROM erp_hr_payment WHERE payment
所以我正在编写一些代码,并且最近遇到了实现一些 mixin 的需要。我的问题是,设计混音的正确方法是什么?我将使用下面的示例代码来说明我的确切查询。 class Projectile(Movable,
我的环境变量包含如下双引号: $echo $CONNECT_SASL_JAAS_CONFIG org.apache.kafka.common.security.plain.PlainLoginModu
示例: /** * This function will determine whether or not one string starts with another string. * @pa
有没有办法在 Grails 中做一个不区分大小写的 in 子句? 我有这个: "in"("name", filters.tags) 我希望它忽略大小写。我想我可以做一个 sqlRestriction
我搜索了很长时间,以查找将哪些boost库添加到std库中,但是我只找到了一个新库的完整列表(如此处:http://open-std.org/jtc1/sc22/wg21/docs/library_t
我已经通过使用这个肮脏的黑客解决了我的问题: ' Filter managerial functions ActiveSheet.Range("$A$1:$BW$2211").Auto
因此,我很难理解我需要遵循的标准,以便我的 Java 程序能够嵌入 HTML。我是否只需将我的主类扩展到 Applet 类,或者我还需要做更多的事情吗?另外,在我见过的每个 Applet 示例中,它都
我对在 Hibernate 中使用限制有疑问。 我必须创建条件,设置一些限制,然后选择日期字段最大值的记录: Criteria query = session.createCriteria(Stora
我有标准: ICriteria criteria = Session.CreateCriteria() .SetFetchMode("Entity1", FetchMo
我很难编写条件来选择所有子集合或孙集合为空的实体。我可以将这些作为单独的条件来执行,但我无法将其组合成一个条件。 类结构: public class Component { p
@Entity class A { @ManyToMany private List list; ... } @Entity class B { ... } 我想使用条件(不是 sql 查询)从 A
我的数据库中有以下表结构: Table A: Table B: Table C: _______________
请帮助我: 我有下一张 table : 单位 ID 姓名 用户 ID 姓名 利率 单位 ID 用户 ID 我不明白如何从 SQL 创建正确的条件结构: 代码: SELECT * FROM Unit W
我正在构建一个包含项目的网站,每个项目都有一个页面,例如: website.com/book/123 website.com/film/456 website.com/game/789 每个项目都可以
我需要使用两个属性的组合来过滤结果列表。一个简单的 SQL 语句如下所示: SELECT TOP 10 * FROM Person WHERE FirstName + ' ' + LastName L
我有一个“ super 实体”SuperEntity 和三个扩展父类(super class)的实体 ChildEntity1、...、ChildEntity3。 搜索数据库中的所有实体很容易,即我们
我是一名优秀的程序员,十分优秀!