首页 > 解决方案 > 从/etc/fstab 读取除注释行之外的所有行,包含'bind|swap|shm' 的行并打印其中未安装的行?

问题描述

从/etc/fstab 读取除注释行之外的所有行,包含'bind|swap|shm' 的行并打印其中未安装的行?我已经尝试了以下方法,我想知道是否可以获得更好的更短代码。

mount_check()
{
fstb=$(cat /etc/fstab |egrep -vw 'bind|swap' |awk '$1 !~/#|^$/ {print $2}')
for i in ${fstb}
do
df -hPT | grep -wq ${i}
if [ $? -eq 1 ]
 then
 echo "The file system ${i} has an entry in /etc/fstab file but not mounted"
fi
done
}rc_mount_check=`mount_check |tee |wc -l`if [ $rc_mount_check -eq '0' ]
then
        echo -e "OK: All file systems listed in /etc/fstab are mounted"
        exit $OK
else
        echo -e "CRITICAL: Please verify and mount the file systems\n$(mount_check)\n"
        exit $CRITICAL
fi

标签: bashshellunixawk

解决方案


您正在寻找的实际命令是findmnt. 请您尝试以下操作。

awk '
!/bind|swap|shm/ && $1 !~/#|^$/{
  system("if [[ -n $(findmnt -m " $2 ") ]]; \
          then echo Mount " $2 " is mounted.;\
          else echo Mount " $2 " is NOT mounted.;\
          fi"\
  )
}
' /etc/fstab

或者以单线的形式尝试以下(如果你需要的话):

awk '!/bind|swap|shm/ && $1 !~/#|^$/ {system("if [[ -n $(findmnt -m " $2 ") ]]; then echo Mount " $2 " is mounted.;else echo Mount " $2 " is NOT mounted.;fi")}' /etc/fstab

推荐阅读