【问题标题】:How can I save the grep output of a curl to a variable and don't log it to the console?如何将 curl 的 grep 输出保存到变量而不将其记录到控制台?
【发布时间】:2019-03-19 15:39:18
【问题描述】:

我有一个 shell 脚本(由 apache cgi 调用),它应该 curl 和 grep 结果来检查服务器的运行状况。这就是我目前所得到的。

printf "Content-type: text/html\n"
PAGE="$(curl -v -u http://targetserver/xmlgatewayauth/login)"
CRES="$($PAGE|grep "login";)"
echo $CRES;

如果我在 Linux 中将脚本作为 shell 运行,则输出为:

...
< Location: login_success.jsp
< Content-Length: 0
< Date: Mon, 15 Oct 2018 08:42:39 GMT
...

但如果我从浏览器通过 CGI 调用脚本 - 结果是空的。 我已经用静态输出测试了 CGI 脚本,它工作正常。 (世界你好)。

问题是: 我不希望脚本输出变量“CRES”以外的任何内容。但它会将整个 curl (grep)(不是变量)输出到控制台,并且不会将变量输出到网站。

我该如何解决这个问题?

【问题讨论】:

  • CRES=$(curl ... | grep "login")
  • @MarkSetchell 仍然从​​ grep 输出所有内容
  • Mark 发布的内容很可能是将上面的第 2 行和第 3 行合并为一行。我很确定,您面临的问题是由第 1 行的错位引起的。
  • 您的第一行似乎缺少一个 shebang,例如 #!/bin/bash。您是否使用chmod +x YourScriptName 使您的脚本可执行?

标签: bash shell curl grep cgi


【解决方案1】:

问题是您在其他行之前打印第 1 行。所以把顺序改成这样:

PAGE="$(curl -v -u http://targetserver/xmlgatewayauth/login)"
CRES="$($PAGE|grep "login";)"
echo $CRES;
printf "Content-type: text/html\n"

PAGE="$(curl -v -u http://targetserver/xmlgatewayauth/login)"
CRES="$($PAGE|grep "login";)"
printf "Content-type: text/html\n"
echo $CRES;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多