2

I have a simple bash script which i call from my php code to find out the version of my apache and nginx.

$webroot = getcwd();

function get_version($name)
{
    global $webroot;

    switch ($name)
    {
        case "apache":
            $path   = shell_exec("whereis apachectl | awk '{ print $2 }'");
            $version = shell_exec("sudo $webroot/scripts/get_version $path 1 2>&1");
            break;
        case "nginx":
            $path = shell_exec("whereis nginx | awk '{ print $2 }'");
            $version = shell_exec("sudo $webroot/scripts/get_version $path 2 2>&1");
        default:
            echo "error";
    }

    return $version;
}

As you can see i call my bash script with two arguments passed. The path and a integer number which i use in my bash script:

#!/bin/bash

_x=""
_programm=$1
_nr=$2

if [ "$_nr" -eq "1" ] ; then
    _x=$($_programm -v 2>/dev/null | grep -i 'version' | awk -F/ '{ print $4 }')
elif [ "$_nr" -eq "2" ] ; then
    _x=$($_programm -v 2>&1 | awk -F/ '{ print $2 }')
fi

cd $(pwd)
echo $_x

Output of function:

get_version("apache");     OUTPUT:     sh: 2: 1: not found
get_version("nginx");      OUTPUT:     sh: 2: 2: not found

But if i execute the bash script in the terminal, then it works and i get the version number as output, i tried it both with user root and www-data, both worked. The bash script is also entered in the visudo file and has execute rights, the user of the script is www-data.

./get_version /usr/sbin/apachectl 1     OUTPUT: 2.2.2
./get_version /usr/sbin/nginx 2         OUTPUT: 1.3

Can someone please explain why it does work in terminal but not in php?

2
  • 1
    Shouldn't you backslash $ in double quotes in print $2? Commented Feb 1, 2016 at 9:45
  • Do you mean like this: print \$2 ? I tried it but then i get syntax errors. Commented Feb 1, 2016 at 9:48

2 Answers 2

2

i found the problem and the solution. The command whereis in my php switch statement wrote a space character to the path variable for some unknown reason, so it did not worked because of it. I used rtrim on my $path variable to fix it.

    case "apache":
        $path   = shell_exec("whereis apachectl | awk '{ print $2 }'");
        $path   = rtrim($path);
        ...
Sign up to request clarification or add additional context in comments.

1 Comment

Does it work now? Note also, that the error message you received, was not produced by bash, but by sh.
1

You have to escape $ if you are using it inside double quotes in php or switch to single quotes:

...
$path   = shell_exec('whereis apachectl | awk \'{ print $2 }\'');
...
$path = shell_exec('whereis nginx | awk \'{ print $2 }\'');

7 Comments

Hmm. I used your code but unfortunately i still get the same error. I also think it makes no different to my code.
Please add debug output for $path and $version string and post in your question. echo $path . "\n" . $version; before break statement.
I get /usr/sbin/apachectl + sh: 2: 1: not found and /usr/sbin/nginx + sh: 2: 2: not found
If I set $webroot to /my/web/root I get sudo /my/web/root/scripts/get_version whereis apachectl | awk '{ print $2 }' 1 2>&1 which seems to be fine...
Just a remark: You can simplify your script by using which apachectl instead of whereis apachectl. First will just return the path to the executable, no additional info.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.