Computer Old Farts Forum
 help / color / mirror / Atom feed
From: Ralph Corderoy <ralph@inputplus.co.uk>
To: coff@tuhs.org
Subject: [COFF] Re: On Bloat and the Idea of Small Specialized Tools
Date: Sun, 12 May 2024 08:29:03 +0100	[thread overview]
Message-ID: <20240512072903.DA7E12064E@orac.inputplus.co.uk> (raw)
In-Reply-To: <20240511213532.GB8330@mit.edu>

Hi,

As an example of pushing the limit of Unix pipes, I found this awkward
to read, e.g. the noise of the redundant backslash after an end-of-line
pipe.

>             gcloud compute instances describe \
>                         --zone "$z" "$i" --format=json > "$inst_info"
>             kver=$(jq < "$inst_info" 2> /dev/null \
>                 '.metadata.items[] | select(.key == "kernel_version") | .value' | \
>                         sed -e 's/^"//' -e 's/"$//' \
>                             -e 's/^Linux xfstests-[0-9A-Za-z-]* //' -e 's/ .*//')
> `           gce_status=$(jq < "$inst_info" .status | \
>                             sed -e 's/^"//' -e 's/"$//')
>             status=$(jq < "$inst_info" 2> /dev/null \
>                 '.metadata.items[] | select(.key == "status") | .value' | \
>                             sed -e 's/^"//' -e 's/"$//')
>             ip=$(jq < "$inst_info" 2> /dev/null \
>                     '.networkInterfaces[] | .accessConfigs[] | select(.name == "external-nat") | .natIP' | \
>                         sed -e 's/^"//' -e 's/"$//')

Re-formatting, including the more idiomatic placement of I/O
redirection, gives

    gcloud compute instances describe \
        --zone "$z" "$i" --format=json >"$inst_info"

    kver=$(
        jq '.metadata.items[] | select(.key == "kernel_version") | .value' \
            <"$inst_info" 2>/dev/null |
        sed '
                s/^"//; s/"$//
                s/^Linux xfstests-[0-9A-Za-z-]* //
                s/ .*//
            '
    )
    gce_status=$(
        jq .status \
            <"$inst_info" |
        sed 's/^"//; s/"$//'
    )
    status=$(
        jq '.metadata.items[] | select(.key == "status") | .value' \
            <"$inst_info" 2>/dev/null |
        sed 's/^"//; s/"$//'
    )
    ip=$(
        jq '.networkInterfaces[] | .accessConfigs[] | select(.name == "external-nat") | .natIP' \
            <"$inst_info" 2>/dev/null |
        sed 's/^"//; s/"$//'
    )

The repetition is now more clear to me.  And I see jq's stderr is
discarded in all but one case.  Assuming that one can be, or should be,
the same allows factoring.

    gcloud compute instances describe \
        --zone "$z" "$i" --format=json >"$inst_info"

    query() {
        jq "${1?}" <"$inst_info" 2>/dev/null |
        sed 's/^"//; s/"$//'
    }

    kver=$(
        query '.metadata.items[] | select(.key == "kernel_version") | .value' |
        sed 's/^Linux xfstests-[0-9A-Za-z-]* //; s/ .*//'
    )
    gce_status=$(query .status)
    status=$(query '.metadata.items[] | select(.key == "status") | .value')
    ip=$(query '.networkInterfaces[] | .accessConfigs[] | select(.name == "external-nat") | .natIP')

-- 
Cheers, Ralph.

  parent reply	other threads:[~2024-05-12  7:29 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-10 16:36 [COFF] " Clem Cole
2024-05-10 23:02 ` [COFF] Re: [TUHS] " Steffen Nurpmeso
2024-05-11  9:18   ` [COFF] " Ralph Corderoy
2024-05-11 21:18     ` Steffen Nurpmeso
2024-05-11 21:33       ` Larry McVoy
2024-05-10 23:22 ` [COFF] Re: [TUHS] " Nevin Liber
2024-05-11  9:31   ` [COFF] " Ralph Corderoy
2024-05-11 12:51   ` [COFF] Re: [TUHS] " John P. Linderman
2024-05-11 13:12   ` Dan Cross
2024-05-11 14:41     ` Bakul Shah via COFF
2024-05-11 15:45     ` Grant Taylor via COFF
2024-05-11 17:18       ` [COFF] " Ralph Corderoy
2024-05-11 21:35     ` [COFF] Re: [TUHS] " Theodore Ts'o
2024-05-12  7:13       ` Gergely Buday
2024-05-12  7:29       ` Ralph Corderoy [this message]
2024-05-12 22:23         ` [COFF] " Dave Horsfall
2024-05-11 19:24   ` [COFF] Re: [TUHS] " Clem Cole
2024-05-13  6:20   ` [COFF] " Arno Griffioen via COFF

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240512072903.DA7E12064E@orac.inputplus.co.uk \
    --to=ralph@inputplus.co.uk \
    --cc=coff@tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).