Reputation: 14250
I would like a command line function that I can run on any file to change the include("myinc.inc");
PHP statement to include 'myfile.inc';
I have made a start by adding the following to my ~/.bashrc file:
function makestandard() {
perl -p -i -e 's/include\("([\w\.]+)"\)/include '$1'/g' $*
}
I source ~/.bashrc;
and run the command at the command line as follows:
$ makestandard myfile.php
I get myfile.php modified but instead of capturing the included file name, the included file name is corrupted to be the name of the current file. As a uninformed guess, I think the bash $1
variable is interfering with the $1
perl regexp variable.
How can I fix this?
Background info (no need to read): We have started using PHP_CodeSniffer (phpcs) to sniff PHP code and report any bad "smells". Unfortunatly, phpcs doesn't fix the non-standard code, it only reports it. Hence I would like to make a script that fixes some of the easy and common non-standard elements of our PHP code. I plan to fill my makestandard
bash function with a bunch of perl pie.
Shell environment: Whatever is out-of-the-box in Ubuntu 10.04.
Upvotes: 0
Views: 2282
Reputation: 93177
You should escape your simple quotes an remove the last parenthesis :
perl -p -i -e "s/include\(\"([\w\.])+\"\)/include '\$1'/g" $*
Upvotes: 5
Reputation: 6985
The original Perl statement, if it had worked, would have found
include("something.php")
and written
include 'something.php')
that is, it would have removed the opening parenthesis and replace double quotes with single .. and not changed the extension as required.
The following appears to work the way the OP intended:
function makestandard() {
perl -p -i -e 's/include\("(\w+)\.inc"\)/include("$1.php")/g' $*
}
Upvotes: 0
Reputation: 64929
It isn't the bash variable, it is the single quotes in your single quoted bash string.
Upvotes: 4