linux - 使用 tee 命令打印垃圾字符

标签 linux bash awk grep

我正在使用的脚本如下:

#!/bin/bash

lla=$(top -n 1 | grep "load average" | awk '{print $13,$14,$15}')
mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
now=`date`
cur_time=$(echo $now | awk '{print $4}')

for i in {1..60}
do
        echo "System Performance Statistics at: "$cur_time | tee -a hp.txt
        echo "Linux Load Average: "$lla | tee -a hp.txt
        echo "Memory Usage: "$mem_usage | tee -a hp.txt
        echo "" | tee -a hp.txt

        sleep 3
done

结果:

System Performance Statistics at: 19:00:29
Linux Load Average: 0.13, 0.11, 0.14^[(B^[[m^[[39;49m^[[K
Memory Usage: 82.7672

标准输出根本不显示任何乱码。我需要更改什么?

最佳答案

在 top 命令中添加了 -b,它起作用了。

来自 man top:

-b :Batch-mode operation

Starts top in Batch mode, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the `-n' command-line option or until killed.

关于linux - 使用 tee 命令打印垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426854/

相关文章:

linux - 使用脚本编号和复制具有奇数和偶数的文件

linux - linux 中 sed 的输出不会写入文件

c++ - 如何将 Qt 库连接到标准 C++ 项目?

macos - 将多个参数从 Applescript 传递到终端命令脚本

linux - bash:静默杀死后台函数进程

windows - 将基于 Windows 斜杠 (/) 的参数从 bash 脚本传递给程序

awk: "default"如果没有匹配的模式怎么办?

PHP 无法在 Linux 上运行

linux - Apache、Linux 和远程命令,如 'Ping'

scripting - 使用 sed,在模式上方或下方插入一行?