简体   繁体   中英

Bash script Partition skip if already exists

I am trying to create partition using the below script. It works, but sometimes I need to re-run the same script due to some automation stuff during the time I am getting error like already in use.

parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%
mkfs.xfs /dev/sdc1
mkdir -p /opt/app
lsblk
echo "/dev/sdc1    /opt/app    xfs     defaults   0  1"  >>/etc/fstab
mount -a
df -Th

Error log when I executing the same script again.

+ parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%
Error: Partition(s) on /dev/sdc are being used.
+ mkfs.xfs /dev/sdc1
mkfs.xfs: /dev/sdc1 contains a mounted filesystem

How can I skip if the file system from /dev/sdc is already mounted?

You can check if the mount is already there , and skip the script if it's found;

if grep -qs '/dev/sdc' /proc/mounts; then
    echo "Skipping mount since /dev/sdc already exists"
else
    parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%
    mkfs.xfs /dev/sdc1
    mkdir -p /opt/app
    lsblk
    echo "/dev/sdc1    /opt/app    xfs     defaults   0  1"  >>/etc/fstab
    mount -a
    df -Th
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM