shell脚本不使用临时文件怎么使iftop的输出结果按行保存到数组中
要在Shell脚本中不使用临时文件,将iftop
的输出结果按行保存到数组中,可以使用进程替换和readarray
(或mapfile
)命令。以下是一个示例脚本:
#!/bin/bash
# 使用进程替换将iftop的输出传递给readarray
readarray -t iftop_output < <(sudo iftop -t -s 1)
# 打印数组内容
for line in "${iftop_output[@]}"; do
echo "$line"
done
在这个脚本中:
sudo iftop -t -s 1
运行 iftop
命令并以文本模式输出,持续时间为1秒。< <(command)
是进程替换,将 iftop
的输出作为输入传递给 readarray
。readarray -t iftop_output
读取 iftop
的输出并将其按行保存到数组 iftop_output
中。for
循环遍历数组并打印每一行。请注意,iftop
需要超级用户权限,因此使用了 sudo
。根据具体情况,您可能需要调整 iftop
的参数以满足