home / tils / til

Menu
  • GraphQL API

til: bash_ignore-errors.md

This data as json

path topic title url body html shot created created_utc updated updated_utc shot_hash slug
bash_ignore-errors.md bash Ignoring errors in a section of a Bash script https://github.com/simonw/til/blob/main/bash/ignore-errors.md 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 ``` <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&gt;</span>/dev/null sqlite3 browse.db <span class="pl-k">&lt;</span> set-country.sql sqlite-utils disable-fts browse.db museums <span class="pl-k">2&gt;</span>/dev/null sqlite-utils enable-fts browse.db museums \ name description country osm_city \ --tokenize porter --create-triggers <span class="pl-k">2&gt;</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&gt;</span>/dev/null</pre></div> <Binary: 67,518 bytes> 2022-06-27T17:24:42-07:00 2022-06-28T00:24:42+00:00 2022-06-27T17:24:42-07:00 2022-06-28T00:24:42+00:00 d8c7cdf1528485991e86832fb6951377 ignore-errors
Powered by Datasette · How this site works · Code of conduct