bash - 如何在 shell 中使用 curl 检查内容类型

标签 bash shell http curl

我想检查服务器中文件的内容类型。 以下代码总是回显“图像大小无效”。

    TYPE=$(curl -sI $IMAGE_LINK | grep Content-Type)
    IFS=" "
    set $TYPE
    echo $2

    if [ $2 == "image/gif" ] 
    then
        echo "image size is valid"
        exit 0  
    else
        echo "image size is invalid"
        exit 1
    fi  

这是输出。为什么当 $2 是“image/gif”时比较不起作用?

image/gif
image size is invalid

此外, 这是我不想要的解决方案。

    TYPE=$(curl -sI $IMAGE_LINK | grep "Content-Type: image/gif")
    echo $TYPE

    if [ ! "$TYPE" = "" ]

最佳答案

提取 Content-Type: header 可以只用 curl 和 -w option 来完成,就像在这个 shell 脚本中一样:

type=$(curl -sI "$IMAGE_LINK" -o/dev/null -w '%{content_type}\n')

if [ "$type" = "image/gif" ]
then
    echo "image type is valid"
    exit 0  
else
    echo "image type is invalid"
    exit 1
fi

关于bash - 如何在 shell 中使用 curl 检查内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46442218/

相关文章:

linux - 如何在.sh文件中编写ssh连接代码以连接到远程计算机

shell - 如何使用 Fabric 设置环境变量

bash - 在 bash 中四舍五入

http - 诱导 Julia 任务完成

java - 用于Rich Client Java(Swing)应用程序的持久性Web服务

bash - 比较 Bash 中的特殊字符

python - 如何在 Mac 上的 python 脚本中删除 bash 历史记录?

linux - 读取数字,然后将它们添加到一个循环中。庆典/Linux

linux - 我很难理解 Shellshock 漏洞验证

json - Flutter 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型