bash - 带有多个变量的 Bash if 语句
问题描述
您好,我正在尝试制作一个简单的 bash 脚本,并且对它很陌生。
使用 if 语句,我试图像时钟一样计数。
所以输出例如是 16:59:58, 16:59:59, 17:00:00
秒和分钟有效,但第一个 if 语句不起作用。
当它到达 23:59:59 时,它意味着去 00:00:00 但是它去到 24:60:00
有什么帮助吗?
if [[ "$hours" -eq 23 && "$minutes" -eq 60 && "$seconds" -eq 60 ]]
then
seconds=0
minutes=0
hours=0
((date++))
else
if [[ "$minutes" -eq 60 ]]
then
minutes=00
((hours++))
else
if [[ "$seconds" -eq 60 ]]
then
seconds=00
((minutes++))
fi
fi
fi
解决方案
从最低阶字段向上工作,而不是测试所有变量。
((seconds++))
if [[ $seconds -eq 60 ]]
then
seconds=0
((minutes++))
fi
if [[ $minutes -eq 60 ]]
then
minutes=0
((hours++))
fi
if [[ $hours -eq 24 ]]
then
hours=0
((date++))
fi
推荐阅读
- java - 有没有办法在 Java 的抽象接口方法中要求特定值作为参数?
- chart.js - 如何删除 Chart.js 图例
- google-bigquery - 如何使用 BigQuery 中时间戳的 where 子句选择数据?
- ionic-framework - 离子角原生贝宝既不工作也不抛出任何异常
- tensorflow2.0 - 如何在tensorflow和numpy中从元组转换为ndarray
- javascript - 使用 opentype.js 和 fabric 和 promises 将文本转换为画布上的路径
- java - Firebase 存储:如果存储参考中存储的数据发生更改,存储参考的下载 URL 是否会更改?
- c# - 如何在 DataFlow Block 中为每个线程创建对象,但不是每个请求的对象?
- python - python-docx 返回空的 .docx 文件?
- c - 制作 Makefile 以构建新创建的文件