Home > Software engineering >  Check duplicate disk LABEL before mounting it to the system in bash script
Check duplicate disk LABEL before mounting it to the system in bash script

Time:10-06

is there a way to check if there is duplicate disk LABEL before mounting it to the system?

i need to make sure that if the user have two external drives, if the two of them have the same label, prompt to the user a warning and asking it to remove the duplicated disk.

my code is in the early stages:

if mountpoint -q "${JOB_MOUNT_DIR}"; then
    echo " ${JOB_MOUNT_HD_LABEL} já está montado e está pronto para uso"
else
    echo "O dispositivo ""${JOB_MOUNT_HD_LABEL}"" não está montado no diretório ""${JOB_MOUNT_DIR}"""
    echo "Deseja montar o diretório?"
    echo -n "Qual sua opção? [s/n]: "
    read -r "opcao"
    if [ "$opcao" == "s" ]; then
        mkdir -p "${JOB_MOUNT_DIR}/${JOB_MOUNT_HD_LABEL}"
        mount -L "${JOB_MOUNT_HD_LABEL}" "${JOB_MOUNT_DIR}/${JOB_MOUNT_HD_LABEL}"
        exit 0
    else
        echo "Disco não irá ser montado"
        exit 0
    fi
fi
exit 0

some parts are in pt-br i think that will not be a problem

first it checks if that the disk is already mounted, if not it asks to mount, then there is the problem to know which of the two disk with the same LABEL is to mount

CodePudding user response:

You can use:

lsblk -o name,label
NAME         LABEL
mmcblk0      
└─mmcblk0p1  eMMC

Or you can use:

blkid
/dev/nvme0n1p1: UUID="36D7-B890" TYPE="vfat" PARTUUID="8614534f-01"
/dev/nvme0n1p5: UUID="65885781-bd9b-4c62-afb0-4a82a0e5759e" TYPE="ext4" PARTUUID="8614534f-05"
/dev/mmcblk0p1: LABEL="eMMC" UUID="79ff33b4-2add-4f2f-844e-d7d242c18578" TYPE="ext4" PARTUUID="d4b36674-ab5f-4f15-bb83-313cce242fe4"

You may need to prefix above commands with sudo


If the above commands get your labels correctly, you can pipe the output roughly as follows to list duplicates:

lsblk -o label | sort | uniq -d
  • Related