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

meh*_* oz 1 bash shell curl http

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

    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  
Run Code Online (Sandbox Code Playgroud)

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

image/gif
image size is invalid
Run Code Online (Sandbox Code Playgroud)

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

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

    if [ ! "$TYPE" = "" ]
Run Code Online (Sandbox Code Playgroud)

Dan*_*erg 7

Content-Type:只需使用 curl 和-w 选项即可提取标头,就像在这个 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
Run Code Online (Sandbox Code Playgroud)