簡體   English   中英

帶有多個變量的 Bash if 語句

[英]Bash if statement with multiple variables

您好,我正在嘗試制作一個簡單的 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM