gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:23 27 4
gpt4 key购买 nike

Amazon Linux

上的 init.d 脚本中,有没有办法让下面的命令静音?
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  ]

最佳答案

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

curl -s
curl --silent

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

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

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

您可以了解有关 curl 可以在其 manpage 上执行的操作的更多信息

关于linux - init.d 脚本停止 echo 管道到 grep 输出到终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com