如何在 shell 脚本中将每日磁盘使用情况导出为 csv 格式?
How to export daily disk usage to csv format in shell scripting?
我的脚本如下。当我们 运行 脚本时,它会自动将磁盘 space 使用情况保存在单独的单元格中。
SIZES_1=`df -h | awk 'FNR == 1 {print ","","","","","}'`
SIZES_2=`df -h | awk 'FNR == 2 {print ","","","","","}'`
SIZES_3=`df -h | awk 'FNR == 3 {print ","","","","","}'`
SIZES_4=`df -h | awk 'FNR == 4 {print ","","","","","}'`
SIZES_5=`df -h | awk 'FNR == 5 {print ","","","","","}'`
SIZES_6=`df -h | awk 'FNR == 6 {print ","","","","","}'`
SIZES_7=`df -h | awk 'FNR == 7 {print ","","","","","}'`
SIZES_8=`df -h | awk 'FNR == 8 {print ","","","","","}'`
echo `date +%Z-%Y-%m-%d_%H-%M-%S` >>/home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_1" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_2" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_3" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_4" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_5" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_6" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_7" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_8" >> /home/jeevagan/test_scripts/sizes/excel.csv
这个脚本适合我的机器。
我的疑问是,如果别人的机器有很多文件系统,我的脚本将无法获取所有文件系统的使用情况。如何让它自动抓取所有这些?
假设您需要所有文件系统,您可以将其简化为:
printf '%s\n' "$(date +%Z-%Y-%m-%d_%H-%M-%S)" >> excel.csv
df -h | awk '{print ","","","","","}' >> excel.csv
我会将其简化为
{ date +%Z-%F_%H-%M-%S; df -h | tr -s ' ' ','; } >> excel.csv
- 组命令,因此只需要一个重定向
- 压缩 space 并使用
tr
将它们替换为单个逗号
- 不需要
echo `date`
或类似的:它与 date
相同
date +%Y-%m-%d
等同于 date +%F
请注意,df -h
输出的第一行有一个小缺陷,最初看起来像这样
Filesystem Size Used Avail Use% Mounted on
在最后一栏的标题中有一个space,所以变成了
Filesystem,Size,Used,Avail,Use%,Mounted,on
多了一个逗号。不过,原始的 awk 解决方案只是切断了该行的最后一个字。同样,路径中的 spaces 会使该解决方案失败。
要解决逗号问题,您可以 运行
sed -i 's/Mounted,on$/Mounted on/' excel.csv
时不时地。
顺便说一句,替换 awk 中的所有字段分隔符,而不是
awk '{print ","","","","","}'
你可以使用
awk 'BEGIN { OFS = "," } { = ; print }'
或者,更短,
awk -v OFS=',' '{=}1'
我的脚本如下。当我们 运行 脚本时,它会自动将磁盘 space 使用情况保存在单独的单元格中。
SIZES_1=`df -h | awk 'FNR == 1 {print ","","","","","}'`
SIZES_2=`df -h | awk 'FNR == 2 {print ","","","","","}'`
SIZES_3=`df -h | awk 'FNR == 3 {print ","","","","","}'`
SIZES_4=`df -h | awk 'FNR == 4 {print ","","","","","}'`
SIZES_5=`df -h | awk 'FNR == 5 {print ","","","","","}'`
SIZES_6=`df -h | awk 'FNR == 6 {print ","","","","","}'`
SIZES_7=`df -h | awk 'FNR == 7 {print ","","","","","}'`
SIZES_8=`df -h | awk 'FNR == 8 {print ","","","","","}'`
echo `date +%Z-%Y-%m-%d_%H-%M-%S` >>/home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_1" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_2" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_3" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_4" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_5" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_6" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_7" >> /home/jeevagan/test_scripts/sizes/excel.csv
echo "$SIZES_8" >> /home/jeevagan/test_scripts/sizes/excel.csv
这个脚本适合我的机器。
我的疑问是,如果别人的机器有很多文件系统,我的脚本将无法获取所有文件系统的使用情况。如何让它自动抓取所有这些?
假设您需要所有文件系统,您可以将其简化为:
printf '%s\n' "$(date +%Z-%Y-%m-%d_%H-%M-%S)" >> excel.csv
df -h | awk '{print ","","","","","}' >> excel.csv
我会将其简化为
{ date +%Z-%F_%H-%M-%S; df -h | tr -s ' ' ','; } >> excel.csv
- 组命令,因此只需要一个重定向
- 压缩 space 并使用
tr
将它们替换为单个逗号
- 不需要
echo `date`
或类似的:它与date
相同
date +%Y-%m-%d
等同于date +%F
请注意,df -h
输出的第一行有一个小缺陷,最初看起来像这样
Filesystem Size Used Avail Use% Mounted on
在最后一栏的标题中有一个space,所以变成了
Filesystem,Size,Used,Avail,Use%,Mounted,on
多了一个逗号。不过,原始的 awk 解决方案只是切断了该行的最后一个字。同样,路径中的 spaces 会使该解决方案失败。
要解决逗号问题,您可以 运行
sed -i 's/Mounted,on$/Mounted on/' excel.csv
时不时地。
顺便说一句,替换 awk 中的所有字段分隔符,而不是
awk '{print ","","","","","}'
你可以使用
awk 'BEGIN { OFS = "," } { = ; print }'
或者,更短,
awk -v OFS=',' '{=}1'