Skip to content

Instantly share code, notes, and snippets.

@ruslo
Created February 5, 2024 12:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruslo/09b3e10a21522d43c354d5eb6299f22d to your computer and use it in GitHub Desktop.
Save ruslo/09b3e10a21522d43c354d5eb6299f22d to your computer and use it in GitHub Desktop.
Template bash script file
#!/bin/bash
# Exit immediately if command returns a non-zero status.
# Do not move it to shebang since it will be ignored
# if run by "/bin/bash ./bash_script.sh"
set -e
# No arguments are expected, so exit with an error if anything is provided
# https://unix.stackexchange.com/a/25947
if [ ! $# -eq 0 ];
then
echo "Unexpected arguments: '$*'"
exit 1
fi
# Self location helper variables
# https://stackoverflow.com/a/68359914
THIS_SCRIPT_PATH="${BASH_SOURCE:-$0}"
THIS_SCRIPT_DIR="`dirname "${THIS_SCRIPT_PATH}"`"
# Sanity check
# Note:
# If it's a symbolic link, it will only work
# if the symbolic link name is the same
EXPECTED_FILE="${THIS_SCRIPT_DIR}/bash_script.sh"
if [ ! -r "${EXPECTED_FILE}" ];
then
echo "File not found: ${EXPECTED_FILE}"
exit 1
fi
echo "Hello, world!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment