天天看點

shell程式設計測試檔案類型

#!/bin/bash

#This script is used to test the file type.

#6/20/11

function usage()

{

  echo "Error:Must have a parameter."

  echo "Usage: "$0" filename"

  exit 1

}

function test_file()

{

  FILE_NAME=$1

  if [ ! -e $FILE_NAME ]

    then

      echo "file no find."

      return 1

  fi

  if [ -d $FILE_NAME ]

    then

      echo $FILE_NAME": Directory."

      return 0

  elif [ -c $FILE_NAME ]

    then

      echo $FILE_NAME": Character device file."

      return 0

  elif [ -L $FILE_NAME ]

    then

      echo $FILE_NAME": Link file."

      return 0

  elif [ -b $FILE_NAME ]

    then

      echo $FILE_NAME": Block device file."

      return 0

  elif [ -f $FILE_NAME ]

    then

      echo $FILE_NAME": Regular file."

      return 0

  else

      echo $FILE_NAME": Unknown type."

      return 1

  fi

}

if [ $# = 0 ]

  then

    usage

    exit 1

fi

if MESSAGE=`test_file $1`

  then

   echo $MESSAGE

   exit 0

else

   echo $MESSAGE

   exit 1

fi

繼續閱讀