我想检查服务器中文件的内容类型。 以下代码总是回显“图像大小无效”。
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/