[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Existence of a UUID in a system.



On Wed, Dec 17, 2025 at 06:56:01 -0700, peter@easthope.ca wrote:
> In a bash script, check that a USB store is connected before trying to 
> mount it.

And the UUID in question is the file system UUID, and it's known to you
in advance?

> If connected, then try to mount it.  Otherwise notify that it is not 
> available.

Sounds simple enough.


hobbit:~$ lsblk -a -o +UUID
NAME  MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS UUID
nvme0n1
│     259:0    0 238.5G  0 disk             
├─nvme0n1p1
│     259:1    0   260M  0 part /boot/efi   2E5D-D8D4
├─nvme0n1p2
│     259:2    0    16M  0 part             
├─nvme0n1p3
│     259:3    0 237.2G  0 part /           afdc7260-5289-4014-bc89-0fa3e843e334
└─nvme0n1p4
      259:4    0  1003M  0 part             D47AB7687AB74650

hobbit:~$ lsblk -a -o UUID
UUID

2E5D-D8D4

afdc7260-5289-4014-bc89-0fa3e843e334
D47AB7687AB74650


So...

#!/bin/sh

uuid=my-uuid-goes-here

if lsblk -a -o UUID | grep -q "$uuid"; then
    mount /it
else
    echo "UUID $uuid is not currently attached" >&2
    exit 1
fi


Doesn't even need bash features.  sh is more than enough.


Reply to: