bash - 从/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
解决方案
您正在寻找的实际命令是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
推荐阅读
- scala - 带有柯里化函数和映射的评估顺序
- android - 我们可以在不使用任何外部硬件的情况下通过安卓应用程序将数据转储到 HF-RFID 卡中吗?
- objective-c - UI表格视图单元格上的水平集合视图重用下一行的集合视图数据
- reactjs - 如何在 React 中渲染地图?
- javascript - IE/Edge 分析器不显示 js 调用堆栈
- strapi - “ctx.query._q”中的“_q”是什么?
- angular - 如何知道标签是否在 Angular 中呈现?
- symfony - 带有 Symfony 4 的 LoginFormAuthenticator 中的 EntityManager
- caching - ARM Cortex-M4 中有缓存吗?
- javascript - 将 Chart.JS 的 DataSource 设置为数组