Shell scripts check the php file syntax in a directory

Source: Internet
Author: User
Tags parse error php file
The code is as follows: Copy code


Check_php_syntax.sh

#! /Bin/bash
# Check php syntax
If [$ #-lt 1]; then
Echo 'Usage: '$ 0' directory ';
Exit
Fi
If [! -D $1]; then
Echo $1 'not a directory, please check! ';
Exit
Fi
Directory = $1
Temp_file = "/tmp/file $"
# Echo $ temp_file
Ls-R $ directory | awk'
BEGIN {
FS = "n"
Folder = "'$ directory '"
Logname = "'$ temp_file '"
    }
    {
If ($0 ~ /. Php $ /){
System ("php-l" folder "/" $0 ">" logname "2> & 1 ")
        }
If ($0 ~ /: $ /){
Folder = substr ($1, 1, length ($1)-1)
        }
    }
'
If [-e $ temp_file]; then
Cat $ temp_file | awk'
BEGIN {
Error = 0
       }
        {
If ($0 ~ /Parse /){
Error ++
Errorfile [$0] = $0
            }  
        }
END {
Print "error File:" error "files"
If (length (errorfile)> 0) print "number of error rows :"
For (I in errorfile)
Print I
        }
    '
Else
Echo "php file not found ."
Exit;
Fi

For example:

The code is as follows: Copy code
./Check_php_syntax/home/liuzhichao/test/

Output:

Error File: 2
Number of wrong lines:
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.