1

Not being an sed expert, I need to replace all instances of the string $PHP_SELF with $_SERVER['PHP_SELF'] globally in a directory.

This is an "old school" osCommerce application that I need to resurrect temporarily for a showcase.

Jamie_D
  • 979
  • 6
  • 13

1 Answers1

0

sed doesn't recurse on it's own so you'll need to use another tool for that, find is generally the right answer.

Run this in the top-level directory. sed will make a backup of each file it reads with a .bak extension (regardless if it actually does a replacement). If you don't want the backup behavior, use -i instead of -i.bak

I recommend you first try this on a test directory to make sure it meets expectations.

#!/bin/bash

while IFS= read -r -d $'\0' file; do
   sed -i.bak $'s/$PHP_SELF/$_SERVER[\'PHP_SELF\']/g' "$file"
done < <(find . -type f -name "*.php" -print0)
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • For the POST vars, I was actually successful when I ran `find ./ -type f -exec sed -i 's/HTTP_POST_VARS/_POST/g' {} \;` but was a bit timid to run `sed` with the dollar ($) sign. – Jamie_D May 22 '20 at 03:27
  • Working perfectly! Thanks much! – Jamie_D May 22 '20 at 03:56
  • 1
    The $ and literal single quotes in the match and/or replacement can get tricky. The `bash` solution is to us `$''` which won’t expand $ and let you specify a literal single-quote with `\'` – SiegeX May 22 '20 at 04:11