Created
November 19, 2021 01:01
-
-
Save mulle-nat/e6b1428ef9e39dfa1feadbbd8d5e6f48 to your computer and use it in GitHub Desktop.
πππ mulle-strip-whitespace strips off leading and trailing spaces
This file contains 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
#! /bin/sh | |
# | |
# Copyright (c) 2021 Nat! - Mulle kybernetiK | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# Redistributions of source code must retain the above copyright notice, this | |
# list of conditions and the following disclaimer. | |
# | |
# Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions and the following disclaimer in the documentation | |
# and/or other materials provided with the distribution. | |
# | |
# Neither the name of Mulle kybernetiK nor the names of its contributors | |
# may be used to endorse or promote products derived from this software | |
# without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
# POSSIBILITY OF SUCH DAMAGE. | |
# | |
[ "${TRACE}" = 'YES' ] && set -x && : "$0" "$@" | |
#### | |
# | |
# Prelude to be placed at top of each script. Rerun this script either in | |
# bash or zsh, if not already running in either (which can happen!) | |
# Allows script to run on systems that either have bash (linux) or | |
# zsh (macOS) only by default. | |
if [ "$1" != --no-auto-shell ] | |
then | |
if [ -z "${BASH_VERSION}" -a -z "${ZSH_VERSION}" ] | |
then | |
exe_shell="`command -v "bash" `" | |
exe_shell="${exe_shell:-`command -v "zsh" `}" | |
script="$0" | |
# | |
# Quote incoming arguments for shell expansion | |
# | |
args="" | |
for arg in "$@" | |
do | |
# True bourne sh doesn't know ${a//b/c} and <<< | |
case "${arg}" in | |
*\'*) | |
# Use cat instead of echo to avoid possible echo -n | |
# problems. Escape single quotes in string. | |
arg="`cat <<EOF | sed -e s/\'/\'\\\"\'\\\"\'/g | |
${arg} | |
EOF | |
`" | |
;; | |
esac | |
if [ -z "${args}" ] | |
then | |
args="'${arg}'" | |
else | |
args="${args} '${arg}'" | |
fi | |
done | |
# | |
# bash/zsh will use arg after -c <arg> as $0, convenient! | |
# | |
exec "${exe_shell:-bash}" -c ". ${script} --no-auto-shell ${args}" "${script}" | |
fi | |
if [ ! -z "${BASH_VERSION}" ] | |
then | |
set +o posix | |
fi | |
else | |
shift # get rid of --no-auto-shell | |
fi | |
# https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable | |
r_trim_whitespace() | |
{ | |
RVAL="$*" | |
RVAL="${RVAL#"${RVAL%%[![:space:]]*}"}" | |
RVAL="${RVAL%"${RVAL##*[![:space:]]}"}" | |
} | |
# | |
# This script merely strips off leading and trailing spaces from | |
# the input. It may add an additional linefeed at the end if the input has | |
# none. | |
# | |
text="`cat "$@"`" | |
IFS=$'\n' | |
while read -r line | |
do | |
r_trim_whitespace "${line}" | |
printf "%s\n" "${RVAL}" | |
done <<< "${text}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment