bash - Bash 嵌套条件显示意外行为
问题描述
请看一下:这应该与 X10 相呼应,但与大奖相呼应……谁能明白为什么它的行为不正常?可能只是我犯的一些错误,不会引发错误?
dice1=1
dice2=40
#These two lines are just tests to see if my brain still function:
echo "Is dice 1 less than 2? $(($dice1 < 2))"
echo "Is dice 2 between 6 and 54? $(($dice2 > 5 && $dice2 < 55))"
if [[ $dice1 == 1 ]]
then
if [[ $dice2 < 6 ]]
then
#dice1 has to be equal 1 and dice2 less than 6:
echo "jackpot"
else
#Since dice2 is larger than 5, if smaller than 55
#it should be between 6 and 54...
if [[ $dice2 < 55 ]]
then
echo "X10"
else
echo "X5"
fi
fi
else
echo "Dice one is not equal 1."
fi
解决方案
与 一起使用时[[
,<
and>
运算符使用当前语言环境按字典顺序排序。
我看到两个选项。
在算术上下文中进行比较:
if (( $dice1 == 1 ))
then
if (( $dice2 < 6 ))
或老式的方式:
if [[ $dice1 -eq 1 ]]
then
if [[ $dice2 -lt 6 ]]
推荐阅读
- c# - С# 自由控件(UWP/WPF/Winforms)类似于mxGraph(draw.io)
- plsql - 将数据从一个表发送到另一个表
- docker - Timescaledb 扩展未间歇性启用
- c# - 如何确定合并范围内的哪些单元格可见并且可以有值?
- angular - 合并 observables 但同时返回
- ms-access - MS Access:在两个表中查找具有不同字段的行
- java - 无法使用 Intent 启动服务,无法添加窗口 - 令牌 null 无效,您的活动是否正在运行?
- php - 如何在没有 eval() 的情况下在 php 中创建动态链接
- excel - 保护关于单元格格式/边框的数据表
- ios - UINavigationController 无法同时推送两个 vc