我隐式地将布尔值分配给变量
x=false
我可以做到
$x || echo "the value is set to true"
但反向测试不仅仅是那么简短和甜蜜
{ ! $x || echo "the value is set to false " ; }
在伯恩 (Ksh) 中,它的评估结果像
! false
结果不一样
因此,可以使用opp条件的测试值(
![ "$x" = "true" ]
这是反向测试布尔值的唯一简短方法,还是有更好的表达方式
我不确定我是否理解你的问题。你想根据$x真或假打印一些东西吗?如果是这样,我认为以下内容应该有效:
$x && echo "the value is true" || echo "the value is set to false " ;
if ! $x
then
YourThenCode
else
YourElseCode
fi
样本
$ x=true;if ! $x ; then echo "True if"; else echo "false if" ;fi
false if
$ x=false;if ! $x ; then echo "True if"; else echo "false if" ;fi
True if