• blkid and shell usage.

    From peter@3:633/10 to All on Wednesday, July 01, 2026 18:40:01
    Hi,

    This shell function is a prototype.

    Test() { \
    workingVolume=40d81969-8f9d-4964-b214-87bcf273192a
    echo "working volume asigned a value; now try blkid."
    if ! blkid -t UUID="$workingVolume" > /dev/null; then
    printf "workingVolume not found.\n"
    else
    printf "workingVolume found.\n"
    fi
    echo "Now try to catch the name of the device."
    blkid -t UUID="$workingVolume" | cut -d':'
    }

    The if statement gives the result I expect.

    I miss the syntax to make the last statement work.

    Thx, ... P.



    --
    mobile: +1 778 951 5147
    VoIP: +1 778 508 0020 <= decommissioning
    Bcc: peter at e a s t h o p e dot c a
    projects: en.wikibooks.org/wiki/User:PeterEasthope

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Charles Curley@3:633/10 to All on Wednesday, July 01, 2026 19:10:02
    On 01 Jul 2026 08:58:08 -0700
    peter@easthope.ca wrote:

    I miss the syntax to make the last statement work.

    You might do better with lsblk. See the --list-columns and --output
    options for ideas on how to do it.

    --
    Does anybody read signatures any more?

    https://charlescurley.com
    https://charlescurley.com/blog/

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Greg Wooledge@3:633/10 to All on Wednesday, July 01, 2026 19:20:01
    On Wed, Jul 01, 2026 at 08:58:08 -0700, peter@easthope.ca wrote:
    Hi,

    This shell function is a prototype.

    Test() { \
    workingVolume=40d81969-8f9d-4964-b214-87bcf273192a
    echo "working volume asigned a value; now try blkid."
    if ! blkid -t UUID="$workingVolume" > /dev/null; then
    printf "workingVolume not found.\n"
    else
    printf "workingVolume found.\n"
    fi
    echo "Now try to catch the name of the device."
    blkid -t UUID="$workingVolume" | cut -d':'
    }

    The if statement gives the result I expect.

    I miss the syntax to make the last statement work.

    Move the last two commands inside the "else ... fi" block, so they aren't executed when the working volume is not found.

    hobbit:~$ blkid
    hobbit:~$ sudo blkid
    [sudo] password for greg:
    /dev/nvme0n1p3: UUID="afdc7260-5289-4014-bc89-0fa3e843e334" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="599cd65f-67f9-400c-ad22-1c3ee95f5341"
    /dev/nvme0n1p1: LABEL_FATBOOT="SYSTEM" LABEL="SYSTEM" UUID="2E5D-D8D4" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="3d23b4eb-5477-417b-9bc9-c2a6d263c2ed"
    /dev/nvme0n1p4: LABEL="Windows RE Tools" BLOCK_SIZE="512" UUID="D47AB7687AB74650" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="bd4e5997-6d42-4f64-9c8b-812355b573bb"
    /dev/nvme0n1p2: PARTLABEL="Microsoft reserved partition" PARTUUID="9ca2c59a-85fe-4602-9e1e-1937a59834a5"

    It looks like what you were trying for was cut -d: -f1

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Franco Martelli@3:633/10 to All on Wednesday, July 01, 2026 20:50:02
    On 01/07/26 at 19:16, Greg Wooledge wrote:
    It looks like what you were trying for was cut -d: -f1

    In addiction I'd suggest to use the so called "exit status variable: $?"
    in the "if ; then" statement:

    ...
    echo "working volume assigned a value; now try blkid."
    blkid -t UUID="$workingVolume" >/dev/null 2>&1
    if [ $? -ne 0 ] ; then
    printf "workingVolume not found.\n"
    else
    ...

    it makes the code more readable, IMHO.

    --
    Franco Martelli

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Greg Wooledge@3:633/10 to All on Wednesday, July 01, 2026 22:20:01
    On Wed, Jul 01, 2026 at 20:40:30 +0200, Franco Martelli wrote:
    In addiction I'd suggest to use the so called "exit status variable: $?" in the "if ; then" statement:

    ...
    echo "working volume assigned a value; now try blkid."
    blkid -t UUID="$workingVolume" >/dev/null 2>&1
    if [ $? -ne 0 ] ; then
    printf "workingVolume not found.\n"
    else
    ...

    it makes the code more readable, IMHO.

    I disagree, but of course this is an opinion, and you're entitled to
    feel this way.

    In the general case where you want to run a command, and print an error
    message of your own if it fails, inside of a function, I would probably
    use this format:

    checkvolume() {
    local workingVolume=...
    blkid -t UUID="$workingVolume" >/dev/null 2>&1 || {
    echo "working volume not found" >&2
    return 1
    }
    ...
    }

    However, in this *specific* example, the OP is actually calling blkid
    twice -- once to check whether it succeeds, then again to get the output
    and parse it. I would reduce that to a single call instead. Then it
    becomes more "interesting" to extract the exit status. Assuming we're
    using bash, and not sh, we can use pipefail:

    checkvolume() {
    local workingVolume=...
    local device
    device=$(set -o pipefail;
    blkid -t UUID="$workingVolume" 2>/dev/null | cut -d: -f1) || {
    echo "working volume not found" >&2
    return 1
    }
    printf "working volume found: device is '%s'\n" "$device"
    ...
    }

    Otherwise, if we're using sh, the best solution is probably to store
    the full output of lsblk, and then parse it in a second pass. In fact,
    this may even be preferred in bash.

    checkvolume() {
    # use local if your shell has it
    workingVolume=...
    device=$(blkid -t UUID="$workingVolume" 2>/dev/null) || {
    echo "working volume not found" >&2
    return 1
    }
    device=${device%%:*}
    printf "working volume found: device is '%s'\n" "$device"
    ...
    }

    That's probably the cleanest version.

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)