需要一些帮助
我试图增强预提交挂钩,看看如果提交消息中有某个密钥("!ignore"),我是否可以忽略解析提交消息
此前的if条件,比如检查是否有空提交消息有效。但是,如果条件不知何故不起作用。
好的,当我提交消息" SMARTCOMMITTEST"它不包含我的检查键"!ignore",提交成功,这意味着下面的If条件从未执行或未按预期执行。因此,试图了解它的错误。
SMARTCOMMIT=1
$SVNLOOK log -t "$TXN" "$REPOS" | grep "!ignore" | wc -c || SMARTCOMMIT=0
if [ $SMARTCOMMIT = 0];
then
echo "Please use !ignore if you dont want to use smart commits in your commit message." 1>&2
exit 1
fi
非常感谢Etan的一些提示......
我改变了条件,就像其他条件在评论中一样,然后就可以了
SMARTCOMMIT=$($SVNLOOK log -t "$TXN" "$REPOS" | grep "!ignore" | wc -c)
if [ "$SMARTCOMMIT" = "0" ]; then
echo "Please use !ignore if you dont want to use smart commits in your commit message." 1>&2
exit 1
fi

这个工作得很好..
@David W ..我现在有一种情况来检查同一个if
中的多个条件
SMARTCOMMIT=$($SVNLOOK log -t "$TXN" "$REPOS" | grep '!ignore' | wc -c)
COMMITMESSAGENOREVIEW=$($SVNLOOK log -t "$TXN" "$REPOS" | grep '#comment' | wc -c)
COMMITMESSAGEWITHREVIEW=$($SVNLOOK log -t "$TXN" "$REPOS" | grep '+review' | wc -c)
if [ "$SMARTCOMMIT" = "0" -a "$COMMITMESSAGENOREVIEW" = "0" -a "COMMITMESSAGEWITHREVIEW" = "0" ]; then
echo "Please use #comment or +review to enable smart commits or !ignore to not use smart commits." 1>&2
exit 1
fi

我尝试了link here,但我仍然没有看到if条件被执行了。你能帮我解决这个问题吗?
答案 0 :(得分:0)
if
语句可以查看命令的退出输出,因此您不需要根据输出设置环境变量:
shopt extglob > /dev/null && extglob=1
if ! $SVNLOOK log -t "$TXN" "$REPOS" | grep -q '!ignore'
then
echo 'Please use "!ignore" if you dont want to use smart commits in your commit message.' 1>&2
exit 1
fi
这会运行$SVNLOOK log -t "$TXN" "$REPOS" | grep -q '!ignore'
-q
是安静模式。 grep
如果字符串存在则退出零,如果不存在则退出非零。通过将其放在if !
中,then
子句仅在找到!ignore
时执行。
您必须确保!ignore
被单引号包围,或者您在\
前放置!
。 Bash在那里有一个csh类型的历史机制,并且没有办法将其关闭。只要shell看到!
,它就会认为它与进程ID号有关。