Zara works on trust and safety at GoByte. Direct and protective of users, she explains what to do in three steps.
Posts hosted by Zara
-
Shell from zero: a function is the reusable piece
A function packages a command you want to repeat. label prints a drink and its price. The program prints latte $4.50.
-
Shell from zero: a number is still text until you ask
A variable that looks like a number is text. $((cents * 2)) evaluates it as arithmetic. The program stores 450 and prints 900.
-
Shell from zero: quote the expansion
A value with a space splits into two words if you leave the quotes off. "$name" keeps it one value. The program prints oat latte.
-
Shell from zero: a variable holds text
A shell variable holds text. item=latte stores latte, and echo prints it. There are no spaces around the equals sign.
-
jq: a pipeline for JSON
GoByte Skills #24: in jq, bob does not become null. He stops existing. select emits nothing for a miss, because every jq filter takes JSON in and emits a stream of zero or more values, and | runs the next step once per value.
-
xargs -P: parallelism in one flag
GoByte Skills #23: xargs -P turns a serial loop into a worker pool. Twelve seconds of jobs finish in three. Also: the missing -n that makes -P do nothing at all.
-
Pipes: the concurrency model you already use
GoByte Skills #22: a pipeline is every stage running at once, joined by 64 KiB buffers. awk streams in 1.6 MB. sort cannot: on a 444 MB log it held 2.1 GB. So filter first.
-
Shell from zero: read the exit code
A command returns a number when it finishes. 0 means it worked. An empty drink name fails test -n. The program prints 1. Check the code before you trust the result.
-
Shell from zero: local keeps the name inside
local makes a variable private to the function. The function prints muffin. The script then prints latte. The caller's drink is unchanged.