【问题标题】:loop to test all NFS mount point循环测试所有 NFS 挂载点
【发布时间】:2019-05-08 07:34:32
【问题描述】:

这段代码运行良好

mountpoint="/mnt/testnfs"
read -t1 < <(stat -t "$mountpoint" 2>&-)
if [ -z "$REPLY" ] ; then
echo "NFS mount stale. Removing..."
fi

如果我尝试将其放入循环中:

declare -a nfs_array=( "/mnt/testnfs1" "/mnt/testnfs2/" )

for i in "${nfs_array[@]}"
    do
        read -t1 < <(stat -t "$nfs_array" 2>&-)
        if [ -z "$REPLY" ] ; then
            echo "NFS dead"
        fi 
done

目的是测试所有挂载点,此代码测试并仅读取 nfs_array 中的第一个条目。如果我将 testnfs1 与 testnfs2 交换,此代码将测试 testnfs2 安装点并忘记 testnfs1 :-(

【问题讨论】:

    标签: linux bash nfs


    【解决方案1】:

    在你的循环中应该是:

    read -r -t1 < <(stat -t "$i" 2>&-)
    

    目前它只是读取第一个数组值,没有使用$i

    【讨论】:

    • @jimbolino:不客气!如果解决了问题,请将答案标记为正确。
    【解决方案2】:

    如果您真的想列出 所有 nfs 挂载(标题如此说明),请使用:

    mount | grep ' type nfs' | ...
    

    这可能有误报,因为挂载点或挂载路径包含type nfs

    如果 /proc/ 文件系统可用,这是一个更好的方法:

    awk '$3 ~ /^nfs/ {print}' /proc/mounts | ...
    

    这里我不确定会发生什么,如果挂载点或挂载路径包含空格——我从来没有遇到过这种情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      相关资源
      最近更新 更多