错误:“[:缺少`]'”,括号前有空格

错误:“[:缺少`]'”,括号前有空格

当我运行以下代码时:

#!/bin/bash

PREVIOUS_COMMIT=e099d95d52b8fca99e47fd7cee5f782287178b27
SERVICE=service-web-prj1

if [ ! git diff "$PREVIOUS_COMMIT" HEAD --name-only | grep -qs "$SERVICE" ] || [ ! git diff "$PREVIOUS_COMMIT" HEAD --name-only | grep -qs 'service-web' ];
    then
        echo Didnt pass first
        exit 0;
fi

echo passed first with $SERVICE

if ( ! echo "$SERVICE" | grep -q "^service-web" );
    then
        echo Didnt pass second
        exit 0;
fi

echo passed second with $SERVICE

我正进入(状态:

scripts/getLastCommit.1.sh: line 9: [: missing `]'
scripts/getLastCommit.1.sh: line 9: [: missing `]'

我环顾四周,每个人都提到了最后一个“]”之前的空格,我已经检查了三遍,我确实有这个

有人知道我做错了什么吗?

谢谢,丹尼斯

发现这对我有用:

if ! echo $GITDIFF | grep -q -e 'service-web*' -e $SERVICE;

答案1

您无法测试 shelltest括号内的整个命令。您想要完全删除这些括号,这将导致它只测试命令的退出代码。

#!/bin/bash

PREVIOUS_COMMIT=e099d95d52b8fca99e47fd7cee5f782287178b27
SERVICE=service-web-prj1

if ! git diff "$PREVIOUS_COMMIT" HEAD --name-only | grep -qs "$SERVICE" || ! git diff "$PREVIOUS_COMMIT" HEAD --name-only | grep -qs 'service-web'
then
        echo "Didn't pass first"
        exit 0
fi

echo passed first with "$SERVICE"

if ( ! echo "$SERVICE" | grep -q "^service-web" )
then
        echo "Didn't pass second"
        exit 0
fi

echo passed second with "$SERVICE"

相关内容