5

The following works great:

phpmemory_limit=256M #or what ever you want it set to
sed -i 's/memory_limit = 16M/memory_limit = '${phpmemory_limit}'/' /etc/php5/apache2/php.ini

If the memory_limit is set to 16M, but I've found that in some distributions it doesn't default to 16M, but instead will default to 32M. So my question is how to I have SED account for that and replace whatever the number is to ${phpmemory_limit}?

9

you can use regexp - for instance:

phpmemory_limit=256M #or what ever you want it set to
sed -i 's/memory_limit = .*/memory_limit = '${phpmemory_limit}'/' /etc/php5/apache2/php.ini
2

Assuming that:

  • Values will always be specified in Mb.
  • You wish to preserve trailing comments.
  • You don't wish to modify any commented occurances.
  • phpmemory_limit only contains an integer.

    sed -ri 's/^(memory_limit = )[0-9]+(M.*)$/\1'${phpmemory_limit}'\2/' /etc/php5/apache2/php.ini
    

This uses..

  • Backreferences to shorten the regex and preserve the comment.
  • seds extended regex support.
  • It's important that the shortened options are specified in that order, otherwise it will create a backup with the file extension r.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.