我想将uptime命令的输出保存到 Bash 脚本中的 csv 文件中。由于该uptime命令根据自上次重新启动以来的时间具有不同的输出格式,因此我想出了一个基于 的非常繁重的解决方案case,但肯定有一种更优雅的方法来做到这一点。
正常运行时间输出:
8:58AM up 15:12, 1 user, load averages: 0.01, 0.02, 0.00
Run Code Online (Sandbox Code Playgroud)
想要的结果:
15:12,1 user,0.00 0.02 0.00,
Run Code Online (Sandbox Code Playgroud)
当前代码:
case "`uptime | wc -w | awk '{print $1}'`" in
#Count the number of words in the uptime output
10)
#e.g.: 8:16PM up 2:30, 1 user, load averages: 0.09, 0.05, 0.02
echo -n `uptime | awk '{ print $3 }' | awk '{gsub ( ",","" ) ; print $0 }'`","`uptime | …Run Code Online (Sandbox Code Playgroud)