Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created April 12, 2018 18:57
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 yaauie/59fae7c13972135fae7be6aad46ddb9e to your computer and use it in GitHub Desktop.
Save yaauie/59fae7c13972135fae7be6aad46ddb9e to your computer and use it in GitHub Desktop.
Ensure Trailing Newline: ensures that the given plaintext file ends with a newline character, appending in-place only if it is missing.
#!/usr/bin/env bash -e
#
# Ensure Trailing Newline: ensures that the given plaintext file ends with a
# newline character, appending in-place only if it is missing.
#
# Portable on POSIX-based or POSIX-compatible systems, as it uses POSIX-standard
# invocations of `wc`, `tail`, and `echo`.
#
# Copyright 2018 Ry Biesemeyer
#
# Made available under the [MIT license](https://opensource.org/licenses/MIT)
TARGET="${1:?USAGE: $(basename $0) <filename>}"
if [ $(tail -c1 "${TARGET}" | wc -l) -eq 0 ]; then
echo >> "${TARGET}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment