感謝您的澄清。
last reboot | grep ^reboot | wc -l
這就是系統重新啓動的次數。由於您的程序不會在重新啓動後「生存」,因此我認爲您需要重新啓動的次數爲,因爲程序第一次運行的時間爲。所以,你要存儲的重新啓動的次數第一次,並重新讀取上(第一和)隨後開始:
if [[ ! -e ~/.reboots ]]
then
echo $(last reboot | grep ^reboot | wc -l) > ~/.reboots
fi
INITIAL_REBOOTS=$(cat ~/.reboots)
# Now you can check if the *current* number of reboots
# is larger than the *initial* number by three or more:
REBOOTS=$(last reboot | grep ^reboot | wc -l)
if [[ $(expr $REBOOTS - $INITIAL_REBOOTS) -ge 3 ]]
then
echo "Three or more reboots"
else
echo "Less than three reboots"
fi
以上缺乏各種技巧和錯誤檢查(如萬一有人篡改爲~/.reboots
),但僅用於概念證明。
Errrr ....「一個不能輕易修改的數字」?我不明白你的意思。你得到重啓次數,你爲什麼要修改它? (爲什麼你認爲你不能?)請說明爲什麼相關答案不適合你。 – DevSolar
對不起,不清楚,但我想說的是,我希望我的變量在程序開始時爲空,並根據重新啓動次數增加。隨着我發現的程序,該命令已經給我一個結果。 – Kenwa