til: bash_ignore-errors.md
- path
-
bash_ignore-errors.md
- topic
-
bash
- title
-
Ignoring errors in a section of a Bash script
- body
-
For [simonw/museums#32](https://github.com/simonw/museums/issues/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](https://stackoverflow.com/a/60362732) on StackOverflow lead me to the [following recipe](https://github.com/simonw/museums/blob/d94410440a5c81a5cb3a0f0b886a8cd30941b8a9/build.sh): ```bash #!/bin/bash set -euo pipefail yaml-to-sqlite browse.db museums museums.yaml --pk=id python annotate_nominatum.py browse.db python annotate_timestamps.py # Ignore errors in following block until set -e: set +e sqlite-utils add-column browse.db museums country 2>/dev/null sqlite3 browse.db < set-country.sql sqlite-utils disable-fts browse.db museums 2>/dev/null sqlite-utils enable-fts browse.db museums \ name description country osm_city \ --tokenize porter --create-triggers 2>/dev/null set -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: ```bash sqlite-utils disable-fts browse.db museums 2>/dev/null ```
- html
-
<p>For <a href="https://github.com/simonw/museums/issues/32">simonw/museums#32</a> I wanted to have certain lines in my Bash script ignore any errors: lines that used <code>sqlite-utils</code> to add columns and configure FTS, but that might fail with an error if the column already existed or FTS had already been configured.</p> <p><a href="https://stackoverflow.com/a/60362732" rel="nofollow">This tip</a> on StackOverflow lead me to the <a href="https://github.com/simonw/museums/blob/d94410440a5c81a5cb3a0f0b886a8cd30941b8a9/build.sh">following recipe</a>:</p> <div class="highlight highlight-source-shell"><pre><span class="pl-c"><span class="pl-c">#!</span>/bin/bash</span> <span class="pl-c1">set</span> -euo pipefail yaml-to-sqlite browse.db museums museums.yaml --pk=id python annotate_nominatum.py browse.db python annotate_timestamps.py <span class="pl-c"><span class="pl-c">#</span> Ignore errors in following block until set -e:</span> <span class="pl-c1">set</span> +e sqlite-utils add-column browse.db museums country <span class="pl-k">2></span>/dev/null sqlite3 browse.db <span class="pl-k"><</span> set-country.sql sqlite-utils disable-fts browse.db museums <span class="pl-k">2></span>/dev/null sqlite-utils enable-fts browse.db museums \ name description country osm_city \ --tokenize porter --create-triggers <span class="pl-k">2></span>/dev/null <span class="pl-c1">set</span> -e</pre></div> <p>Everything between the <code>set +e</code> and the <code>set -e</code> lines can now error without the Bash script itself failing.</p> <p>The failing lines were still showing a bunch of Python tracebacks. I fixed that by redirecting their standard error output to <code>/dev/null</code> like this:</p> <div class="highlight highlight-source-shell"><pre>sqlite-utils disable-fts browse.db museums <span class="pl-k">2></span>/dev/null</pre></div>
- created
-
2022-06-27T17:24:42-07:00
-
created_
utc -
2022-06-28T00:24:42+00:00
- updated
-
2022-06-27T17:24:42-07:00
-
updated_
utc -
2022-06-28T00:24:42+00:00
-
shot_
hash -
d8c7cdf1528485991e86832fb6951377
- slug
-
ignore-errors