开发者

How to get bash to check for the location of a file?

开发者 https://www.devze.com 2023-01-15 07:14 出处:网络
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. I need help with that second line:

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. I need help with that second line:

#!/bin/bash
if (php is located in /usr/bin/php);开发者_StackOverflow社区 then
    PHP = /usr/bin/php
else
    PHP = /usr/local/zend/bin/php
fi
$PHP script.php


Use this:

`which php`

But this is what I would do:

#!/bin/env php
<?php

require 'script.php';


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


Try this:

if [ -e /usr/bin/php ]; then


whereis php locates it


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

0

精彩评论

暂无评论...
验证码 换一张
取 消