160 words
1 minute
Ignoring errors in a section of a Bash script
Ignoring errors in a section of a Bash script
For simonw/museums#32 I wanted to have certain lines in my Bash script ignore any errors: lines that used sqlite-utils
to add columns and configure FTS, but that might fail with an error if the column already existed or FTS had already been configured.
This tip on StackOverflow lead me to the following recipe:
#!/bin/bashset -euo pipefail
yaml-to-sqlite browse.db museums museums.yaml --pk=idpython annotate_nominatum.py browse.dbpython annotate_timestamps.py# Ignore errors in following block until set -e:set +esqlite-utils add-column browse.db museums country 2>/dev/nullsqlite3 browse.db < set-country.sqlsqlite-utils disable-fts browse.db museums 2>/dev/nullsqlite-utils enable-fts browse.db museums \ name description country osm_city \ --tokenize porter --create-triggers 2>/dev/nullset -e
Everything between the set +e
and the set -e
lines can now error without the Bash script itself failing.
The failing lines were still showing a bunch of Python tracebacks. I fixed that by redirecting their standard error output to /dev/null
like this:
sqlite-utils disable-fts browse.db museums 2>/dev/null
Ignoring errors in a section of a Bash script
https://mranv.pages.dev/posts/ignoring-errors-in-a-section-of-a-bash-script/