【问题标题】:Using cURL and sed to output an image based on a bool's state使用 cURL 和 sed 根据布尔状态输出图像
【发布时间】:2013-09-04 07:56:38
【问题描述】:

我正在制作一个 geeklet(位于 OS X 桌面上的脚本,来自 Geektool),它使用以下命令获取来自 Reddit 的未读消息的状态(使用 API 中的 me.json

echo curl -X GET -u 'username:password' http://www.reddit.com/api/me.json | sed -e "s/.*\"has_mail\":以某种方式得到布尔值的真/假"

然后取bool的状态设置图片。

输入应该是 TRUE(有未读消息)或 false(没有未读消息)。输出应该是本地目录中的图像,根据真/假变化。

两个问题:

  1. 如何使用 sed 返回 TRUE/FALSE?
  2. 如何取 TRUE/FALSE 并设置图像(本地)?

谢谢!

【问题讨论】:

  • 最好给出样本输入和预期输出!!!

标签: json api curl sed geektool


【解决方案1】:

Sed 不是执行此操作的更好工具,请尝试执行此操作:

#!/bin/bash

out=$(curl -X GET -u 'username:password' http://www.reddit.com/api/me.json | ...)
if [[ $out == *TRUE* ]]; then
    echo 'Do something'
    exit 0
elif [[ $out == *FALSE* ]]; then
    echo 'Do another thing'
    exit 1
else
    echo >&2 "Command fails"
fi

curl ... | grep -q TRUE && do_Something || do_another_thing

【讨论】:

  • 有没有办法做到如果为真,脚本成功,如果为假,脚本失败?
  • 在我的帖子中添加了exit
猜你喜欢
  • 2022-10-13
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 2013-11-05
  • 2021-11-27
  • 1970-01-01
相关资源
最近更新 更多