Read while table
Parsing strings can be made easier with liberal use of Bash's builtins, which comes in especially handy when documenting things like APIs in Org-mode.
Say you’re given an HTTP API that takes two query parameters, and you want to issue requests to that API with a set of parameters.
One starts by defining a table using Org-mode syntax:
#+name: parameters
| Name | Age |
|--------------------+-----|
| john-johnson | 32 |
| james-jameson | 44 |
| ken-kenson | 27 |
| phil-philson | 65 |
| stephen-stephenson | 56 |
And now we can loop over our table and issue a request by parsing the two
columns with read
:
#+begin_src sh :results silent :var requests=parameters
echo "$parameters" | while read -r name age; do
out="${name}+${age}.json"
[[ -f "$out" ]] || \
curl \
-H "Accept: application/json" \
-H "User-Agent: Emacs" \
"https://example.com/api?name=${name}&desiredRole=${role}" \
| jq . \
> "$out"
done
#+end_src
while read -r name age
assigns the $name
and $age
variables to the first
and second columns by splitting on whitespace.
If the response has already been stored, we continue onto the next pair of name
and age. Otherwise, we invoke curl
, pretty print the result with jq
and
write to our desired path.
One thing I learnt during this process was that =jq= and =prettier= format JSON differently, which makes for some faff with reproducibility and version control.