0

I want to write something detecting where PHP is installed and then execute php after that eg:

./find_php.sh /path/to/php/file.php

So I can get it to detect the php I want to use and stick it in a variable, but I don't know how to get it to execute the following php file after. Here's a skeleton of what I've got:

#!/bin/bash
if [ -e /usr/local/zend/bin/php ]; then
   PHP=/usr/local/zend/bin/php
else
   PHP=/usr/bin/php
fi
$PHP <execute php>

Edit - thanks got it, just needed exec $PHP "$@"

  • In your script, why not just set the PATH to locations you think php will be and then run php and let bash do the testing. – Zoredache Nov 04 '10 at 02:22
  • Can't do it like that - we have at least 30 servers out there, we're and they have php installed in a different place. this will stop us having to see where it's been installed and "just work" - it goes to their server over our svn up cron already –  Nov 04 '10 at 02:45
  • @Zordache: Using conditionals gives you more control. `PATH=foo:bar:$PATH` What if you only want to execute it if it's in `foo` or `bar` but not in the previous `PATH` (exiting with an error), but you need the previous `PATH` to be intact for other commands? I would suggest, however, that Michael change his `else` to `elif [ -e /usr/bin/php ]; then` and add an `else echo 'PHP not found'; exit 1` or similar. – Dennis Williamson Nov 04 '10 at 02:59
  • @Dennis Williamson yes I thought about getting error stuff in there, might do, it will echo out "-bash: /blah/php: No such file or directory" if it can't find it in either of those directories anyway won't it? –  Nov 04 '10 at 03:43

1 Answers1

2

Make your last line:

exec $PHP "$@"

That will run the php binary with the arguments you gave to the script and have it take over the shell script process as well.

If you would like to have your script do other things after executing the php script, drop the exec.

mark
  • 2,325
  • 14
  • 10