Last active
June 19, 2023 10:50
-
-
Save DennisGaida/b768eb994deebe2d7238a4f6b0b8bd11 to your computer and use it in GitHub Desktop.
Shell Script Best-Practice
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 | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
if [[ "${TRACE-0}" == "1" ]]; then | |
set -o xtrace | |
fi | |
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then | |
echo 'Usage: ./script.sh arg-one arg-two | |
This is an awesome bash script to make your life better. | |
' | |
exit | |
fi | |
cd "$(dirname "$0")" | |
main() { | |
echo do awesome stuff | |
} | |
main "$@" |
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 | |
# via https://sharats.me/posts/shell-script-best-practices/ | |
set -o errexit # when a command fails, bash exits instead of continuing with the rest of the script | |
set -o nounset # make the script fail, when accessing an unset variable | |
set -o pipefail # ensure that a pipeline command is treated as failed, even if one command in the pipeline fails | |
if [[ "${TRACE-0}" == "1" ]]; then | |
set -o xtrace # enable debug mode, by running your script as TRACE=1 ./starter-script.sh | |
fi | |
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then # Use [[ ]] for conditions, instead of [ ] and always add help | |
echo 'Usage: ./script.sh arg-one arg-two | |
This is an awesome bash script to make your life better. | |
' | |
exit | |
fi | |
cd "$(dirname "$0")" # Always quote variable accesses with double-quotes, change to the script’s directory | |
main() { | |
echo do awesome stuff | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment