我有一个脚本用于将磁盘使用情况写入csv文件,如下所示:
#!/bin/bash
disklist=(
/
)
[ -f "disk.conf" ] && disklist=($(<disk.conf))
datesed=$(date +"%d\/%m\/%Y")
df -P "${disklist[@]}" | sed -ne 's/^.* \([0-9]\+\)% \(.*\)$/'$datesed', \2, \1%/p' >> disk.csv
输出如下:
11/06/2015, /, 18%
11/06/2015, /dev, 1%
11/06/2015, /bin, 5%
但有些文件系统有时会变为只读。所以我需要检查并写下这个。我可以使用while-do,如果文件系统只读或不喜欢这个(简单触摸tmp文件):
#!/bin/bash
while read -r diskpath2;
do
diskpath=${diskpath2}
touch $diskpath/readonly-check.tmp
if [ ! -f $diskpath/readonly-check.tmp ]; then
readonly="yes"
else
readonly="no"
fi
echo "$diskpath --> $readonly"
rm -rf $diskpath/readonly-check.tmp
done < disk.conf
我需要在我的数组中为每个文件系统使用它,如果是只读的,请按以下方式编写:
11/06/2015, /, 18%(READ-ONLY)
11/06/2015, /dev, 1%(READ-ONLY)
11/06/2015, /bin, 5%
disk.conf:
/
/dev
/bin
我该怎么做?
答案 0 :(得分:1)
#!/bin/bash
DateSed="$( date +'%d/%m/%Y' )"
if [ -a disk.conf ]
then
cat disk.conf
else
# list of default disk (1 by line) to test
echo "/"
fi \
| while read ThisDisk
do
# return status of this disk
echo "${DateSed}, ${ThisDisk}, $( df -P "${ThisDisk}" | sed 's#.*[[:space:]]\([0-9]\{1,\}[%]\)[[:space:]]/.*#\1#;2!d' )$( [ ! -w "${ThisDisk}" ] && echo "(Read-Only)" )"
done
$()
AnyNumber%