[COFF] On Bloat and the Idea of Small Specialized Tools
Ralph Corderoy
ralph at inputplus.co.uk
Sun May 12 17:29:03 AEST 2024
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.
More information about the COFF
mailing list