0

My goal is to write an as simple as possible (but not more simple) that generates a list of currently installed PHP extensions (*.so files) that the user can simply copy/paste into their php.ini. It should be compatible at minimum with OS X and at least some major Linux distros (Ubuntu, Arch Linux, RHEL, etc.).

The expected output would look like:

extension=iconv.so
extension=mcrypt.so
...

So far, I have come up with this:

find `php-config --extension-dir` -name "*.so" | rev | cut -d / -f 1 | rev

It works well, but I feel it's a bit clumsy with all the string reversions. And, it only generates the extensions' names, it does not add the extension= prefix.

I needed to use the reversions because the extension-dir might be of any depth, thus I cannot simply cut the string at a pre-determined path separator position. However, I know that if I cut it from the end at the first separator, I will get only the filename.

Then I discovered the replace utility and wrote this, which is a perfect fit for my task, except it does not seem to be available on OS X and Arch Linux, the two systems I use most:

find `php-config --extension-dir` -name "*.so" | replace `php-config --extension-dir`/ 'extension='

Basically, I am looking for other creative (but simple) ways of completing this task. What other utilities could I use? Educate me! :)

3

How about this?

find "`php-config --extension-dir`" -name "*.so" | sed 's!^.*/!extension=!'

For each line, sed will match the longest string that starts at the beginning (^) and ends at /. It will always match everything up to and including the last /, because it is a greedy match, i.e., it will match everything but the filename. It then replaces that with extension=.

It ends up like so:

$ find "`php-config --extension-dir`" -name "*.so" | sed 's!^.*/!extension=!'
extension=pdo_pgsql.so
extension=pdo_sqlite.so
extension=mysqlnd.so
extension=mysqli.so
extension=enchant.so
extension=pgsql.so
extension=mysql.so
extension=pdo_odbc.so
extension=odbc.so
extension=curl.so
extension=pdo.so
extension=pdo_mysql.so
extension=sqlite3.so

which seems to be what you want.

sed ought to be available everywhere, because it's part of the POSIX standard.

  • Ah, awesome! I tried sed, too, but my approach was to use a substitution - something along the lines of ... | sed 's/`php-config --extension-dir`/extension=/g' (this is probably wrong, but you get the picture), but it kept complaining due to the presence of / in the path. Let's see if others can come up with other ideas, but this is a good one! – Robert Rossmann May 21 '15 at 8:45
  • @RobertRossmann: You can do it that way as well, but you'd have to use a different separation character than /. (In case you didn't know, that character can be anything except a command letter, as long as it's the same all three times and doesn't occur in the pattern unescaped.) E.g. sed "s~`php-config --extension-dir`/~extension=~" works the way you intended. – marinus May 21 '15 at 8:48
  • Huh, nice one. sed's learning curve seems to me like a 90º uphill slope, but oh, the things you can do once on top... – Robert Rossmann May 21 '15 at 8:55
2

If you have a GNU find you can use -printf option

find `php-config --extension-dir` -name "*.so" -printf 'extension=%f\n' 
  • This looks great, too! Unfortunately, Mac do not support the -printf option so I cannot use this for my purposes. But thanks for introducing me to that switch! – Robert Rossmann May 22 '15 at 7:58

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.