BASH - 检查是否在没有 AWS CLI 工具的情况下将卷附加到实例

BASH - Check if a volume is attached to instance without AWS CLI tools

我正在编写一个脚本,该脚本需要检查卷是否附加到实例 (/dev/sdf)。

我尝试以两种方式进行,但它一直告诉我该卷没有附加,即使它是附加的。

这是我的第一次尝试:

if test -d /dev/xvdf; then
    echo "Volume is attached!"
else
    echo "Volume is not attached! Please attach it first, then re-run this script!"
    exit 1
fi

这是我的第二次尝试:

if grep '/dev/xvdf' /etc/mtab > /dev/null 2>&1; then
    echo "Volume is attached!"
else
    echo "Volume is not attached!"
    exit 1
fi

这里是 lsblk 输出的卷已附加证明:

ubuntu@ip-10-XX-X-XX:~$ lsblk
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
xvda    202:0    0     8G  0 disk
└─xvda1 202:1    0     8G  0 part /
xvdf    202:80   0   100G  0 disk

非常感谢任何帮助!

好吧,我想出了一个解决方案(不是最漂亮的解决方案,但它有效)

vol='/dev/xvdf'
volcheck=`ls /dev/xvdf`

if [ $volcheck = $vol ]; then
        echo "Volume is attached!"
else
        echo "Volume isn't attached!"
fi

这也有效:

if test -b /dev/xvdf; then
    echo "Volume is attached!"
else
    echo "Volume is not attached! Please attach it first, then re-run this script!"
fi

感谢 Douglas Leeder(来自上面的评论)!