【问题标题】:Redirecting Shell output to SNS将 Shell 输出重定向到 SNS
【发布时间】:2017-05-01 22:08:16
【问题描述】:

我的 bash shell 脚本中有以下行;

 inotifywait -e "$EVENTS" -m -r --format '%T %:e %w%f' --timefmt "%a, %b %d %l:%M.%S"  ${basedir} | grep "${filetype}"

每当执行该行时,这将输出类似于 "Mon, May 01 9:29.20 DELETE /home/ubuntu/test/test.php" 的行。

是否可以将以上输出直接通过管道传输到 AWS SNS?我尝试在重定向之前使用 jq 将输出格式化为 json,但是如下使用时没有输出。

inotifywait -e "$EVENTS" -m -r --format '%T %:e %w%f' --timefmt "%a, %b %d %l:%M.%S"  ${basedir} | grep "${filetype}" | jq -R -s -c 'split("\n")'

而且直接 pipping 输出是行不通的。

inotifywait -e "$EVENTS" -m -r --format '%T %:e %w%f' --timefmt "%a, %b %d %l:%M.%S"  ${basedir} | grep "${filetype}" | aws  sns publish --topic-arn "${sns}"

【问题讨论】:

  • grep 的结果倒入标准输出会发生什么?
  • 为什么要格式化成json?似乎您需要将其解析为请求参数(请参阅a sample request)。顺便说一句,为什么不使用CLI
  • 我得到我想要的输出作为一个字符串。例如:“5 月 1 日星期一 9:29.20 删除 /home/ubuntu/test/test.php”
  • 我正在尝试使用 CLI,但据我了解,我需要将 JSON 字符串传递给它?
  • 哎呀我没注意到,划掉那个:/

标签: bash shell amazon-web-services amazon-sns


【解决方案1】:

我在文档中没有看到任何关于aws 命令能够接收来自stdin 的输入的引用,所以似乎没有办法做到这一点。

您可以使用 --cli-input-json 将所有参数作为 json 传递,但在我们的例子中它没有用。

您没有指定要使用inotifywait 的输出填充哪个字段,但假设您要传递message,您只需将输出重定向到option value

get_message() {
    inotifywait -e "$EVENTS" -m -r --format '%T %:e %w%f' --timefmt "%a, %b %d %l:%M.%S"  ${basedir} | grep "${filetype}"
}
aws sns publish --topic-arn "${sns}" --message "$(get_message)"

【讨论】:

    猜你喜欢
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    相关资源
    最近更新 更多