簡體   English   中英

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的輸出中的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(來自上面的評論)!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM