判断文件是否为图片

#!/bin/bash

#judge image file type

#判断是否只有一个参数

if [ $# != 1 ] 

then   

      echo "parameter error"

else  

     ## 读取前3个字节与前4个字节对应的16进制  

     len3=`xxd -p -l 3 $1`  

     len4=`xxd -p -l 4 $1`  

     if [ $len3 == "ffd8ff" ]    

      then      

        echo "The type is jpg"   

    elif [ $len4 == "89504e47" ]    

      then       

        echo "The type is png"  

   elif [ $len4 == "47494638" ]    

      then       

         echo "The type is gif"  

   else       

      echo "The type is others"   

  fi

fi

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章