• Difficulty with a shell function.

    From peter@3:633/10 to All on Sunday, January 25, 2026 06:40:01
    # blkid --uuid 2026-01-19-03-28-45-00
    /dev/sdc
    Meaning that the block device with that UUID is connected to the system.

    When this executes,

    destination="2026-01-19-03-28-45-00";
    printf "destination=$destination\n";
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then
    printf "destination device not connected. Aborting.\n"
    else
    printf "destination device is connected.\n"
    dev="$( blkid --uuid $destination )";
    printf "dev=$dev\n";
    # FTH;
    fi;

    this is the output.
    destination=2026-01-19-03-28-45-00
    destination device not connected. Aborting.

    With the device being present, I expect this. destination=2026-01-19-03-28-45-00
    destination device is connected.
    dev=/dev/sdc

    Does this line have a syntax error?
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then

    Thanks, ... P.

    --
    mobile: +1 778 951 5147
    VoIP: +1 778 508 0020
    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.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Will Mengarini@3:633/10 to All on Sunday, January 25, 2026 07:50:01
    * peter@easthope.ca <peter@easthope.ca> [26-01/24=Sat 21:12 -0700]:
    # blkid --uuid 2026-01-19-03-28-45-00
    /dev/sdc
    Meaning that the block device with that UUID is connected to the system.

    When this executes,

    destination="2026-01-19-03-28-45-00";
    printf "destination=$destination\n";
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then
    printf "destination device not connected. Aborting.\n"
    else
    printf "destination device is connected.\n"
    dev="$( blkid --uuid $destination )";
    printf "dev=$dev\n";
    # FTH;
    fi;

    this is the output.
    destination=2026-01-19-03-28-45-00
    destination device not connected. Aborting.

    With the device being present, I expect this. destination=2026-01-19-03-28-45-00
    destination device is connected.
    dev=/dev/sdc

    Does this line have a syntax error?
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then

    The syntax is correct but the logic is confusing. Try this:

    $ PS1='$?\$ '
    0$ blkid --uuid 473f5ac6-f392-488c-b51f-1798531db64a
    /dev/vda2
    0$ blkid --uuid 473f5ac6-f392-488c-b51f-1798531db64aXXXX
    2$ isConnectedUUID(){ lsblk -alno UUID|grep "$1" >/dev/null;}
    0$ isConnectedUUID 473f5ac6-f392-488c-b51f-1798531db64a
    0$ isConnectedUUID 473f5ac6-f392-488c-b51f-1798531db64aXXXX
    1$ if isConnectedUUID 473f5ac6-f392-488c-b51f-1798531db64a;then echo OK;else echo OOPS;fi
    OK
    0$

    --- PyGate Linux v1.5.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michel Verdier@3:633/10 to All on Sunday, January 25, 2026 10:20:01
    On 2026-01-24, peter@easthope.ca wrote:

    Does this line have a syntax error?
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then

    As say Will you could use a function to clarify your code and it is
    better to protect your variables. Also you could use "grep -qs" and
    remove "> /dev/null". And you could use only blkid and not lsblk. But
    this do not answer your question. I try your code and it works here so
    the problem is elsewhere.
    Do you run bash or sh ?
    Could you provide us
    type -a blkid
    type -a lsblk
    type -a grep

    --- PyGate Linux v1.5.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Kamil Jo?ca@3:633/10 to All on Sunday, January 25, 2026 11:20:01
    peter@easthope.ca writes:

    # blkid --uuid 2026-01-19-03-28-45-00
    /dev/sdc
    Is this a real uuid or redacted one? Its syntax is strange for me.

    Meaning that the block device with that UUID is connected to the system.

    When this executes,

    destination="2026-01-19-03-28-45-00";
    printf "destination=$destination\n";
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then
    printf "destination device not connected. Aborting.\n"
    else
    printf "destination device is connected.\n"
    dev="$( blkid --uuid $destination )";
    printf "dev=$dev\n";
    # FTH;
    fi;

    this is the output.
    destination=2026-01-19-03-28-45-00
    destination device not connected. Aborting.

    With the device being present, I expect this. destination=2026-01-19-03-28-45-00
    destination device is connected.
    dev=/dev/sdc

    Does this line have a syntax error?
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then

    Thanks, ... P.

    what returns:
    destination="2026-01-19-03-28-45-00";
    lsblk -alno UUID | grep $destination
    ?
    KJ

    --
    http://wolnelektury.pl/wesprzyj/teraz/
    EMACS = Each Manual's Audience is Completely Stupified

    --- PyGate Linux v1.5.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Greg Wooledge@3:633/10 to All on Sunday, January 25, 2026 16:00:01
    On Sat, Jan 24, 2026 at 21:12:48 -0700, peter@easthope.ca wrote:
    destination="2026-01-19-03-28-45-00";
    printf "destination=$destination\n";

    Never put expansions in the first argument of printf. That argument
    is interpreted by printf, with characters like % and \ being special.
    What you want is:

    printf 'destination=%s\n' "$destination"

    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then

    Error 1: "$destination" must be quoted.

    Error 2: With no options, grep will treat the first non-option
    argument as a Basic Regular Expression, which I don't think is what
    you want. Use the -F option to treat it as a plain string.

    Error 3: Use the -- option terminator in case the argument begins with
    a "-" character.

    Also, you may replace "grep >/dev/null" with grep -q.

    Finally, the parentheses are not needed.

    Thus:

    if ! lsblk -alno UUID | grep -qF -- "$destination"; then
    printf 'not connected...'

    You could also use "case" (sh) or "[[" (bash) to get rid of the grep altogether. With case, you would need to reverse the order of the
    checks, with the positive (connected) case first:

    #!/bin/sh
    case $(lsblk -alno UUID) in
    *"$destination"*) printf 'connected...' ;;
    *) printf 'not connected...' ;;
    esac

    With [[ you may continue checking for the negative first:

    #!/bin/bash
    if [[ $(lsblk -alno UUID) != *"$destination"* ]]; then
    printf "not connected..."

    My personal preference would be to check the positive case first,
    regardless of which approach you use, because it removes a negation,
    making the code easier to read and understand.

    dev="$( blkid --uuid $destination )";

    The quotes you've used are optional. The quotes around "$destination"
    are required.

    dev=$( blkid --uuid "$destination" )

    or

    dev="$( blkid --uuid "$destination" )"

    printf "dev=$dev\n";

    Same as before: use %s in the format argument and "$dev" (quoted!) as
    a second argument. That's the only way to print the content *exactly*
    as is, with no interpretation.

    this is the output.
    destination=2026-01-19-03-28-45-00
    destination device not connected. Aborting.

    With the device being present, I expect this. destination=2026-01-19-03-28-45-00
    destination device is connected.
    dev=/dev/sdc

    Without seeing the output of lsblk -alno UUID we can't help any further.

    Does this line have a syntax error?
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then

    No, just the 3 errors that I listed earlier.

    --- PyGate Linux v1.5.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From peter@3:633/10 to All on Sunday, January 25, 2026 21:20:01
    Hello again,

    From a private message,
    I modified your script slightly and it works for me.

    Thanks.

    The drive isn't formatted and has a UUID such as
    2026-01-19-03-28-45-00. Shorter than a UUID for a file system.

    After reading the reply I noticed that the UUID had changed. It's
    revised periodically. After writing? Elapsed time is involved? I
    don't understand in detail.

    UUID isn't a time-invariant identity for the drive. Two external
    drives here are the same model. Serial number might identify.

    Revised the code listed yesterday. Now similar to this.

    printf "destination is the serial number of a LaCie HDD connected by USB.\n"; destination="10000E000D959403";
    printf 'destination=%s\n' "$destination";
    if ! lsblk -o SERIAL | grep -F -- "$destination" > /dev/null ;
    then
    printf "destination device not connected ";
    printf "or the serial number is wrong. Aborting.\n";
    else
    printf "destination device is connected.\n"
    dev= lsblk --nodeps -o name,serial | grep -F -- "$destination" | cut -d ' ' -f1 ;
    printf 'dev=%s is ready to receive backup.\n' "$dev";
    printf "Press any key to continue.\n";
    read -n 1 c;
    FTH;
    fi;

    OK in a simple test. Suggestions always welcome. Something may be
    more efficient than the long pipeline.
    lsblk --nodeps -o name,serial | grep -F -- "$destination" | cut -d ' ' -f1

    As happens too often, I was looking in the wrong place. Thanks for
    the reply.

    Thx, ... P.


    --
    mobile: +1 778 951 5147
    VoIP: +1 778 508 0020
    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.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Greg Wooledge@3:633/10 to All on Sunday, January 25, 2026 21:30:01
    On Sun, Jan 25, 2026 at 11:54:37 -0700, peter@easthope.ca wrote:
    The drive isn't formatted

    What?

    and has a UUID such as
    2026-01-19-03-28-45-00. Shorter than a UUID for a file system.

    This looks like a timestamp.

    OK in a simple test. Suggestions always welcome. Something may be
    more efficient than the long pipeline.
    lsblk --nodeps -o name,serial | grep -F -- "$destination" | cut -d ' ' -f1

    It would help if we knew what the output looks like. On my system:

    hobbit:~$ sudo lsblk --nodeps -o name,serial
    [sudo] password for greg:
    NAME SERIAL
    nvme0n1 UPJQD01ZTJ03L4

    So... you *have* the serial number (somehow) and you *want* the "name"
    field? The obvious way would be to use awk:

    lsblk --nodeps -o name,serial |
    awk -v serial="$serial" '$2 == serial {print $1}'

    On my system:

    hobbit:~$ serial=UPJQD01ZTJ03L4
    hobbit:~$ sudo lsblk --nodeps -o name,serial | awk -v serial="$serial" '$2 == serial {print $1}'
    nvme0n1

    awk -v isn't 100% safe with all inputs, but if your serial number is
    reasonable (alphanumeric, maybe with some basic punctuation, but no
    backslashes or control characters) it should be OK.

    --- PyGate Linux v1.5.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From tomas@3:633/10 to All on Monday, January 26, 2026 08:10:01
    On Sun, Jan 25, 2026 at 11:54:37AM -0700, peter@easthope.ca wrote:
    Hello again,

    From a private message,
    I modified your script slightly and it works for me.

    Thanks.

    The drive isn't formatted and has a UUID such as
    2026-01-19-03-28-45-00. Shorter than a UUID for a file system.

    After reading the reply I noticed that the UUID had changed. It's
    revised periodically. After writing? Elapsed time is involved? I
    don't understand in detail.

    UUID isn't a time-invariant identity for the drive. Two external
    drives here are the same model. Serial number might identify.

    Revised the code listed yesterday. Now similar to this.
    OK, since you asked for them, some stylistic comments
    (I didn't reply in the first round since I couldn't come
    up with an idea on what was actually failing and didn't
    want to distract from the main question)
    Comments inline:
    printf "destination is the serial number of a LaCie HDD connected by USB.\n"; destination="10000E000D959403";
    printf 'destination=%s\n' "$destination";
    # You quoted "$destination" and moved it to the right spot in printf: good
    if ! lsblk -o SERIAL | grep -F -- "$destination" > /dev/null ;
    then
    printf "destination device not connected ";
    printf "or the serial number is wrong. Aborting.\n";
    else
    printf "destination device is connected.\n"
    dev= lsblk --nodeps -o name,serial | grep -F -- "$destination" | cut -d ' ' -f1 ;
    printf 'dev=%s is ready to receive backup.\n' "$dev";
    printf "Press any key to continue.\n";
    read -n 1 c;
    FTH;
    fi;
    You worked in the suggestions elsethread, that's good. Especially the
    quoting of "$destination" was a timebomb -- once the string contains
    a space the script would stop working mysteriously, years after it
    had been working. That "--" is similar: once $destination starta with
    a dash, things go south. And so on.
    One difficult thing in shells, when you come from other languages,
    is to wrap one head's around variable substitution (and the other
    expansions). The variable is expanded textually in place, it is not
    a "placeholder". So
    if ! lsblk -o SERIAL | grep -F -- $destination > /dev/null
    (without the quotes) is first converted into
    if ! lsblk -o SERIAL | grep -F -- 10000E000D959403 > /dev/null
    Then, the command lsblk is invoked, and its result piped to
    "grep -F -- $10000E000D959403 > /dev/null", for "if" to act on its
    exit code. This would work, mind you. Now, if the value of destination
    suddenly changes to
    destination="1000 0E00 0D95 9403", the "if" line is first expanded
    to
    if ! lsblk -o SERIAL | grep -F -- 1000 0E00 0D95 9403 > /dev/null
    ...and the grep would see those unexpected arguments 0E00, 0D95 and
    9403 (unexpected for us: for grep they would be just extra file names
    to work on), so in the best case you'd get:
    grep: 0E00: No such file or directory
    grep: 0D95: No such file or directory
    grep: 9403: No such file or directory
    I wrote "best case" because, of course, you might have some files
    named like that lying around: then, your script's behaviour starts
    to get /really/ strange :-)
    Another suggestion, more aesthetic: the semicolons at the end of
    the lines are unnecessary. You only need one when putting two
    lines together (as a replacement for a newline). For a reader of
    your script they are confusing.
    (A place where you might see it is in the construction
    if <condition> ; then
    do this
    ...
    But you already separated the "if" and "then" by a newline, which
    is perfectly fine, too.
    Cheers
    --
    tom s


    --- PyGate Linux v1.5.6
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Jeffrey Walton@3:633/10 to All on Monday, January 26, 2026 13:40:01
    On Sun, Jan 25, 2026 at 12:30?AM <peter@easthope.ca> wrote:
    # blkid --uuid 2026-01-19-03-28-45-00
    /dev/sdc
    Meaning that the block device with that UUID is connected to the system.

    When this executes,

    destination="2026-01-19-03-28-45-00";
    printf "destination=$destination\n";
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then
    printf "destination device not connected. Aborting.\n"
    else
    printf "destination device is connected.\n"
    dev="$( blkid --uuid $destination )";
    printf "dev=$dev\n";
    # FTH;
    fi;

    this is the output.
    destination=2026-01-19-03-28-45-00
    destination device not connected. Aborting.

    With the device being present, I expect this. destination=2026-01-19-03-28-45-00
    destination device is connected.
    dev=/dev/sdc

    Does this line have a syntax error?
    if ! ( lsblk -alno UUID | grep $destination > /dev/null ); then

    You should run your script through Shellcheck. It will tell you about the obvious problems, like quoting variables.
    Jeff


    --- PyGate Linux v1.5.6
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Michael@3:633/10 to All on Monday, January 26, 2026 15:10:01
    On Sunday, January 25, 2026 5:12:48 AM CET, peter@easthope.ca wrote:
    # blkid --uuid 2026-01-19-03-28-45-00
    /dev/sdc
    Meaning that the block device with that UUID is connected to the system.

    why not just do something like:

    dev="$( blkid --uuid "$destination" )"
    if [[ $? -eq 0 ]]
    then
    echo "destination device is connected."
    echo "dev=$dev"
    else
    echo "destination device not connected. Aborting."
    fi

    am i missing something?

    greetings...

    --- PyGate Linux v1.5.6
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From peter@3:633/10 to All on Monday, January 26, 2026 21:10:01
    From: Jeffrey Walton <noloader@gmail.com>
    Date: Mon, 26 Jan 2026 07:37:31 -0500
    You should run your script through Shellcheck. It will tell you about the obvious problems, like quoting variables.

    Thanks Jeffrey. Wasn't aware of Shellcheck and will look.

    Several functions are declared in a file invoked with ". file" at the end of .bashrc.

    Thx, ... P.

    --
    mobile: +1 778 951 5147
    VoIP: +1 778 508 0020
    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.6
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From David Wright@3:633/10 to All on Tuesday, January 27, 2026 06:20:01
    On Mon 26 Jan 2026 at 08:02:34 (+0100), tomas@tuxteam.de wrote:

    Another suggestion, more aesthetic: the semicolons at the end of
    the lines are unnecessary. You only need one when putting two
    lines together (as a replacement for a newline). For a reader of
    your script they are confusing.

    I agree with the aesthetics, but readers might as well get used to
    at least /seeing/ them, as type-ing a function adds them back in.

    (A place where you might see it is in the construction

    if <condition> ; then
    do this
    ...

    But you already separated the "if" and "then" by a newline, which
    is perfectly fine, too.

    And as it happens, type will turn:

    if <condition>
    then

    into:

    if <condition>; then

    anyway. Ah well.

    Cheers,
    David.

    --- PyGate Linux v1.5.6
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From peter@3:633/10 to All on Friday, January 30, 2026 00:30:01
    From: Greg Wooledge <greg@wooledge.org>
    Date: Sun, 25 Jan 2026 15:26:19 -0500
    What?

    My knowledge of ISO 9660 is cursory but note the first sentence
    in https://en.wikipedia.org/wiki/Optical_disk_image ,
    "... disk image ... written to an optical disk, disk sector by disc sector ...".
    Not directly comparable to ext4.

    2026-01-19-03-28-45-00. Shorter than a UUID for a file system.

    This looks like a timestamp.

    Explained by Thomas Schmitt in <16912500410373272277@scdbackup.webframe.org>

    lsblk --nodeps -o name,serial | grep -F -- "$destination" | cut -d ' ' -f1

    So... you *have* the serial number (somehow) ...

    As you did.
    # lsblk --nodeps -o name,serial

    The obvious way would be to use awk: ...

    OK, thanks. cut, awk and others. cut seemed straightforward.

    awk -v isn't 100% safe with all inputs, but if your serial number is reasonable (alphanumeric, maybe with some basic punctuation, but no backslashes or control characters) it should be OK.

    The cut I used relied only on blank separation.

    Thanks, ... P.

    --
    mobile: +1 778 951 5147
    VoIP: +1 778 508 0020
    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.6
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)