I'm trying to write a Bash script that updates the hugo.toml
config file of a Hugo static site to insert a slogan
parameter in the [languages.xx.params]
section of each language.
For each language listed in the [languages]
table of hugo.toml
, I want to:
read
)slogan = "..."
under the corresponding [languages.xx.params]
section[languages.xx.params]
section does not exist, create it{{< slogan >}}
to the _index.md
of each language (which works)Given this config:
[languages.es]
languageName = "Español"
contentDir = "content/es"
[languages.fr]
languageName = "Français"
contentDir = "content/fr"
[languages.fr.params]
description = "A test site"
After running the script, I want to see:
[languages.fr.params]
description = "A test site"
slogan = "Bonjour"
[languages.es.params]
slogan = "Hola"
I wrote this Bash script (full code at bottom), which:
slogan = "..."
line in [languages.xx.params]
The logic tries to:
[languages.xx.params]
already exists → insert/replaceslogan
key at the end of the language blockBut when I run it:
[languages.xx.params]
ends up duplicated or ignoredWhat’s the best way to reliably insert or update a single key inside a TOML file via Bash?
Is there a clean way to handle [languages.xx.params]
blocks properly?
If needed, I can also share the full script I'm using.
https://github.com/wilonweb/multi-author/blob/main/batch/set-slogan.sh
You should use a specialized parser to parse TOML which is a
structured standard such as JSON or YAML, for
example dasel. The script could
look like the following script.bash
:
#!/usr/bin/env bash
for lang in $(dasel -r toml -f LANGUAGES.toml 'languages.all().key()' --write plain)
do
printf "Type slogan for %s: " "$lang"
read -r slogan
dasel put -r toml -w toml -f LANGUAGES.toml -v "$slogan" languages."$lang".params.slogan
done
For example - we have the following LANGUAGES.toml like the one you posted in your question:
[languages.es]
languageName = "Español"
contentDir = "content/es"
[languages.fr]
languageName = "Français"
contentDir = "content/fr"
[languages.fr.params]
description = "A test site"
Run the script:
$ ./script.bash
Type slogan for es: Hola
Type slogan for fr: Bonjour
The file will have the following contents:
[languages]
[languages.es]
contentDir = 'content/es'
languageName = 'Español'
[languages.es.params]
slogan = 'Hola'
[languages.fr]
contentDir = 'content/fr'
languageName = 'Français'
[languages.fr.params]
description = 'A test site'
slogan = 'Bonjour'