init.d脚本停止echo管道输出到终端的grep

时间:2015-08-19 10:05:06

标签: linux bash init.d

无论如何,在init.d上的Amazon Linux脚本中沉默以下命令

response=`curl 'http://localhost:9200'`
if (echo "$response" | grep -e "\"status\" : 200"); then
... some logic

此命令和条件按预期工作,但是当脚本运行service scriptname start时,以下内容将打印到控制台

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   333  100   333    0     0  54278      0 --:--:-- --:--:-- --:--:-- 55500
  "status" : 200,
Starting scriptname                                            [  OK  ]

理想情况下,如果可能,我希望显示器只显示:

Starting scriptname                                            [  OK  ]

1 个答案:

答案 0 :(得分:1)

Curl有一个静默标志来阻止它将输出写入stdout和stderr。您可以使用以下任一项:

curl -s
curl --silent

如果使用silent标志,则脚本将停止工作,因为在if语句中找不到grep。使用以下命令查看curl的返回值,而不是解析其响应:

if response=`curl -sS 'http://localhost:9200'`; then
... some logic

这将成功打印或失败时出现错误消息。响应变量将包含从请求中读取的所有数据。

您可以详细了解curl在其manpage

上可以执行的操作
相关问题