views:

62

answers:

5

Hi I want my bash script to find where php is installed - this will be used on different linux servers, and some of them won't be able to use the which command. Anyway I need help with that second line:

#!/bin/bash
if (php is located in /usr/bin/php); then
    PHP = /usr/bin/php
else
    PHP = /usr/local/zend/bin/php
fi
$PHP script.php
+1  A: 
if [ -e /usr/bin/php ]; then
Simon Whitaker
ok that worked, thanks!
beatbreaker
Cool, glad it helped! :)
Simon Whitaker
A: 

whereis php locates it

Zhenya
The whereis command doesn't work for /usr/local/zend/bin/php ...not sure why, but it may be because it was installed from a tar.gz that just ran a crude bash install program
beatbreaker
whereis, if I remember right, will only look in your current PATH variable for the executable you are looking for. Most standard paths include /usr/bin /bin and if you are root: /usr/sbin and /sbin. Since in this case you have php in a special directory whereis likely won't find it unless you have a modified PATH variable.You can check by typing echo $PATH
MadcapLaugher
A: 

For short piece of code you can use: && and ||

[ -x /usr/bin/php ] && PHP=/usr/bin/php || PHP=/usr/local/zend/bin/php

BTW -x return true if the file is executable -e return true if the file exists

+1  A: 

Use this:

`which php`

But this is what I would do:

#!/bin/env php
<?php

require 'script.php';
hopeseekr
A: 

Bash has a type command.

type -p php

will give you the location of the executable based on your $PATH.

You have spaces around your equal signs which Bash doesn't allow. This is what your command should look like:

PHP=$(type -p php) 

or you could even execute it directly:

$(type -p php) script.php
Dennis Williamson