当前位置:首页 > PHP教程 > php高级应用 > 列表

shell 脚本检查某目录下php文件语法

发布:smiling 来源: PHP粉丝网  添加日期:2014-07-15 14:25:15 浏览: 评论:0 

利用php的自带检查文件方法 php -l 来使用shell 批量检查某目录下的文件,每次写完脚本都要 php -l 一下,看看有没有发生低级的语法错误,(疏忽过导致事故),写了个简单的shell 脚本批量检查目录下的 php文件语法,代码如下:

  1. check_php_syntax.sh 
  2.  
  3. #!/bin/bash 
  4. # check php syntax  
  5. if [ $# -lt 1 ];then 
  6.     echo 'Usage: ' $0  'directory'; 
  7.     exit 
  8. fi 
  9. if [ ! -d $1 ];then 
  10.     echo $1  'not a directory,please check!'; 
  11.     exit 
  12. fi 
  13. directory=$1 
  14. temp_file="/tmp/file$$" 
  15. #echo $temp_file 
  16. ls -R $directory | awk  ' 
  17.     BEGIN{ 
  18.         FS="n"     
  19.         folder="'$directory'" 
  20.         logname="'$temp_file'" 
  21.     } 
  22.     { 
  23.         if($0~/.php$/){ 
  24.             system("php -l " folder "/" $0  "   >>  " logname  " 2>&1")   
  25.         } 
  26.         if($0~/:$/){ 
  27.             folder=substr($1,1,length($1)-1) 
  28.         } 
  29.     } 
  30. ' 
  31. if [ -e $temp_file ];then 
  32.     cat $temp_file | awk ' 
  33.         BEGIN{ 
  34.             error = 0 
  35.        } 
  36.         { 
  37.             if($0~/Parse/) { 
  38.                 error++  
  39.                 errorfile[$0] = $0 
  40.             }    
  41.         } 
  42.         END{ 
  43.             print "错误文件:" error "个" 
  44.             if(length(errorfile)>0) print "错误行数:" 
  45.                 for (i in errorfile) 
  46.                     print i 
  47.         } 
  48.     ' 
  49. else 
  50.     echo "php file not found." 
  51.     exit; 
  52. fi 

使用,例如,代码如下:

./check_php_syntax /home/liuzhichao/test/

输出:

错误文件:2个

错误行数:

PHP Parse error:  syntax error, unexpected T_VARIABLE in /home/liuzhichao/test/awktest/a.php on line 3

PHP Parse error:  syntax error, unexpected T_STRING, expecting ')' in /home/liuzhichao/test/levelcity.php on line 19

Tags: shell 脚本检查 文件语法

分享到: