我在bash中有这个函数,其中if语句直到作用域结束才会运行,但只在if语句之后运行第一个命令,然后继续执行第一个 - 下一个命令。范围的结束。我似乎无法弄清楚为什么会发生这种情况,或者为什么它首先是可能的。
有问题的代码:
function log() {
if [[ "x$disablelog" != "xtrue" && "x$logfile" != "x" ]]; then
if [[ ! -w "$logfile" ]]; then
touch "$logfile" &>/dev/null
if [[ "$?" != "0" ]]; then
debugout "Cannot write log file $logfile, disabling log."
disablelog="true"
echo "This, and the lines above/below are never executed. However, debugout is."
return 1
fi
fi
echo "[$(date +%s)] $@" >> "$logfile"
fi
}
我过去曾经遇到过这个问题,而且从来没有找到任何关于它的信息,我想一劳永逸地找出我现在做错的事。
更新:根据请求添加debugout:
function debugout() {
if [[ "x$debug" != "xtrue" ]]; then
return
fi
if [[ "x$color" == "xtrue" ]]; then
echo -e "\e[1;36mDEBUG:\e[0m $@"
else
echo -e "DEBUG: $@"
fi
log "DEBUG: $@"
}
当我只使用普通回声或任何其他命令时,会发生同样的事情。 (编辑:显然不是......)
答案 0 :(得分:3)
debugout
之后没有任何内容表明问题存在的事实。
debugout
和log
以递归方式相互调用,这阻止了debugout
的调用点之后的执行。