Last active
January 22, 2025 03:31
-
-
Save Offirmo/f7495d3ff7883c59b442625ac0cf4557 to your computer and use it in GitHub Desktop.
[Bash -- template] #bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## * #!/usr/bin/env bash is more portable than #!/bin/bash | |
| #!/usr/bin/env bash | |
| # | |
| # This script does this and that. | |
| ## https://stackoverflow.com/questions/31313305/portably-trapping-err-in-shell-script | |
| ## http://mywiki.wooledge.org/BashFAQ/105 | |
| ## TODO review those lines: | |
| set -o errexit | |
| set -eu -o pipefail | |
| ## * add set -eu -o pipefail | |
| ## * -e exits on failure of a simple command | |
| ## * -u exits on dereference of an undefined variable | |
| ## * -o pipefail exists on failures in pipe connected commands | |
| ## * put all commands in functions: this makes sure your script fully parses before anything executes; | |
| ## * pass all arguments to main() | |
| # Prints a small usage help. This is called on `--help` and on parameter validation errors. | |
| # Syntax follows http://docopt.org/. | |
| usage() { | |
| cat <<EOF | |
| ... | |
| EOF | |
| } | |
| # Foos the bar. | |
| foo() { | |
| ... | |
| } | |
| main() { | |
| if [[ "$*" =~ ^(-h|--help)$ ]]; then | |
| usage | |
| exit | |
| fi | |
| foo | |
| } | |
| main "$@" | |
| ## current shell can be tested with this: | |
| ## https://stackoverflow.com/a/11097703/587407 | |
| echo "* current shell : '`ps -p $$ -ocomm=`'" | |
| ########## | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment