我遇到了一个奇怪的问题。在下面的代码中,echo语句工作正常并且在success.txt中我得到b4 running=false with RUNNING:false
,这意味着$ RUNNING = false。
但它没有进入if
区块。
echo "b4 running=false with RUNNING:"$RUNNING >> /tmp/success.txt
if [[ $RUNNING == "false" ]]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
我也试过
if [[ $RUNNING == false ]]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
if [ "$RUNNING" == "false" ]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
if [ "$RUNNING" == false]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
if [ "$RUNNING" == "false" ]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
这些都不起作用。我相信我错过了一些非常小的东西。
答案 0 :(得分:1)
当我尝试
时RUNNING=false
if [[ $RUNNING == false ]]; then
echo 1
fi
if [ "$RUNNING" == "false" ]; then
echo 2
fi
if [ "$RUNNING" == false]; then
echo 3
fi
if [ "$RUNNING" == "false" ]; then
echo 4
fi
我得到了
1
2
/tmp/a.sh:第10行:[:缺少`]'
4
修复第三个
if [ "$RUNNING" == false]; then
到
if [ "$RUNNING" == false ]; then
请注意false
和]
之间的额外空格。
现在我得到
1
2
3
4
所有四个都有效,如果 $RUNNING
是false
。如果从某个命令的输出中捕获值,则差异可能是尾随的新行字符或false
/ $RUNNING
周围的一些空格。
有关详细信息,请参阅Conditional Constructs和Bash Conditional Expressions。
答案 1 :(得分:0)
您是否尝试过以下操作:
if [ $RUNNING == "false" ]; then
echo "test"
fi
这似乎对我有用。
干杯