shell_exec未按预期执行

时间:2017-03-31 09:11:26

标签: php shell

我不想用PHP代码执行shell脚本,这将检查git分支是最新的还是需要推或拉。当我运行我的代码时,我得到错误。

这是我的代码

$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);

这是我得到的错误/回应

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

那么我的代码应该如何才能开始工作?如果您需要任何其他信息,请告诉我,我会提供。谢谢

1 个答案:

答案 0 :(得分:1)

这可能是因为[[是bash-builtin。这可能对你有用。

$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);