linux - 检查字符串是否在命令答案中

标签 linux bash shell unix raspberry-pi

我正在努力解决 linux bash 中的问题。
我想要一个脚本来执行命令

curl -s --head http://myurl/ | head -n 1

如果命令的结果包含 200,它会执行另一个命令。 否则它会回应某些东西。 我现在拥有的:

CURLCHECK=curl -s --head http://myurl | head -n 1
            if [[ $($CURLCHECK) =~ "200" ]]
            then
            echo "good"
            else
            echo "bad"
            fi

脚本打印:

HTTP/1.1 200 OK
bad

我尝试了很多方法,但似乎都不起作用。 有人可以帮助我吗?

最佳答案

我会做这样的事情:

if curl -s --head http://myurl | head -n 1 | grep "200" >/dev/null 2>&1; then
    echo good
else
    echo bad
fi

关于linux - 检查字符串是否在命令答案中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26722405/

相关文章:

linux - cygwin 中 WinMain@16 下的 undefined reference

c - 如何编写 C 应用程序代码来测试 Linux 音频驱动程序

linux - 如何制作 awk 变量的 bash 副本?

linux - 我如何复制一个 bash 实例

linux - 当我运行 test.sh 文件时,Jenkins 控制台显示权限被拒绝错误

linux - 我如何在 Go 中列出 block 设备?

bash - 在 csv 文件中输出第一个副本

perl - 用于从文件中提取特定行的最快/最简洁的 bash 单行代码

linux - 我的 CIFS 安装有时钟差异吗?

bash - 从终端/bash 获取 Macbook 屏幕尺寸