0

I have a file that starts like this:

[global]
pid = /var/run/php-fpm/php-fpm.pid
error_log = /var/log/php5-fpm.log

and want to replace

error_log = /var/log/php5-fpm.log

with

error_log = /var/log/php-fpm/error.log

I want to use a script for that and did the following:

search='error_log = /var/log/php5-fpm.log'
replace='error_log = /var/log/php-fpm/error.log'
for file in `find -name 'php-fpm.conf'`; do
  grep "$search" $file &> /dev/null
  if [ $? -ne 0 ]; then
    echo "Search string not found in $file!"
  else
    sed -i "s#$search#$replace#" $file
  fi  
done

That throws this errors:

sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
TheNiceGuy
  • 85
  • 1
  • 7

1 Answers1

1

IIRC the -i switch is an extension to the original sed and probably therefore implemented slightly differently in different versions. BSD sed unlike GNU sed requires you to explicitly use the -e switch to denote your command when using the -i switch.

Use:

sed -i-e"s#$search#$replace#" $file

as already hinted by your error message.

Writing truly portable scripts is a PITA when details such as that conspire against you.

HBruijn
  • 72,524
  • 21
  • 127
  • 192