Valor_
Valor_

Reputation: 3591

shell_exec is not executing as expected

I wan't to execute shell script with PHP code which will check if git branch is up to date or push or pull is needed. When i run my code i get error.

This is my code

$check = shell_exec(
'LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u})
BASE=$(git merge-base @ @{u})

if [[ $LOCAL = "$REMOTE" ]]; then
    echo "Up-to-date"
elif [[ $LOCAL = "$BASE" ]]; then
    echo "Need to pull"
elif [[ $REMOTE = "$BASE" ]]; then
    echo "Need to push"
else
    echo "Diverged"
fi');
var_dump($check);

And this is the error / response which im getting

sh: 5: [[: not found
sh: 7: [[: not found
sh: 9: [[: not found
string(9) "Diverged
"

So how should my code look like in order to start working? If you need any additional informations, please let me know and i will provide. Thank you

Upvotes: 1

Views: 74

Answers (1)

Ashish K
Ashish K

Reputation: 935

This is probably because [[ is a bash-builtin. This may work for you.

$check = shell_exec(
'LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u})
BASE=$(git merge-base @ @{u})

if [ "$LOCAL" -eq "$REMOTE" ]; then
    echo "Up-to-date"
elif [ "$LOCAL" -eq "$BASE" ]; then
    echo "Need to pull"
elif [ "$REMOTE" -eq "$BASE" ]; then
    echo "Need to push"
else
    echo "Diverged"
fi');
var_dump($check);

Upvotes: 1

Related Questions