michoprogrammer
michoprogrammer

Reputation: 1199

PHP shell_exec() doesn't return the correct status

I'm trying to run a sh script file from the php function shell_exec() but every time the return is uncorrect.

This is the sh file content:

if [[ -z $1 ]]
then
        echo "svn st: NULL"
else
        command=$(ssh -q USER@IP_OF_THE_REMOTE_NODE "svn st $1")
        if [[ -z $command ]]
        then
                test="svn st: OK"
        else
                test="svn st: KO"
        fi
        echo $test
fi

every time that I run this sh script from the shell the echo is ALWAYS correct. The "$1" contains the working directory that I have to check.

But if I run the same script in this way:

$w_path = $working_path['path'];
$com = "sh /tmp/my_script_test.sh $w_path";

echo "content com ".$com;

$res = shell_exec($com);

echo "content res ".$res;

the last echo print ALWAYS "svn st: OK", that is correct for some $w_path, but completely uncorrect for others! Why? How can I find the mistake?

I don't think that is a ssh key problem cause I used a guide to resolve that and now I don't need to insert the password for the remote server.

Thanks Regards

Upvotes: 0

Views: 389

Answers (1)

Armali
Armali

Reputation: 19375

Change

        command=$(ssh -q USER@IP_OF_THE_REMOTE_NODE "svn st $1")
        if [[ -z $command ]]

to

        if ssh -q USER@IP_OF_THE_REMOTE_NODE "svn st $1"

and remember: Whenever something doesn't work, it's because it's too complicated (can't recall where I read that).

Upvotes: 2

Related Questions