判斷文件是否爲圖片

#!/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

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