感谢您的澄清。
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
),但仅作为概念证明。