将 linux 正常运行时间转换为格式良好的日期

ZPU*_*F19 2 linux shell-script date uptime

我想在没有| 的情况下将正常运行时间转换为日期 DD:MM:YY 我想输入一个字符串,例如“计算机自 16 年 2 月 23 日开始运行”

tel*_*coM 7

您可以从以下输出中免费获得它last reboot

$ last reboot
reboot   system boot  4.14.81-i7       Sat Nov 17 23:25   still running
reboot   system boot  4.14.80-i7       Fri Nov 16 09:16 - 15:49  (06:33)

$ printf "On since: "; last reboot | grep "still running" | cut -c 40-56
On since: Sat Nov 17 23:25 

$ printf "On since: " ; last reboot --time-format iso | grep "still running" | cut -c 40-49
On since: 2018-11-17
Run Code Online (Sandbox Code Playgroud)

您的uptime命令可能还有以下-s选项:

$ uptime -s
2018-11-17 23:25:23
Run Code Online (Sandbox Code Playgroud)

由于这种格式是可以接受的date -d,你可以像这样重新格式化时间:

$ date -d "$(uptime -s)" "+On since: %d:%m:%y"
On since: 17:11:18
Run Code Online (Sandbox Code Playgroud)