|
| 1 | +#! /bin/sh |
| 2 | +# |
| 3 | +# Copyright (c) 2021 Nat! - Mulle kybernetiK |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# Neither the name of Mulle kybernetiK nor the names of its contributors |
| 17 | +# may be used to endorse or promote products derived from this software |
| 18 | +# without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 24 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | +# POSSIBILITY OF SUCH DAMAGE. |
| 31 | +# |
| 32 | +[ "${TRACE}" = 'YES' ] && set -x && : "$0" "$@" |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +#### |
| 37 | +# |
| 38 | +# Prelude to be placed at top of each script. Rerun this script either in |
| 39 | +# bash or zsh, if not already running in either (which can happen!) |
| 40 | +# Allows script to run on systems that either have bash (linux) or |
| 41 | +# zsh (macOS) only by default. |
| 42 | + |
| 43 | +if [ "$1" != --no-auto-shell ] |
| 44 | +then |
| 45 | + if [ -z "${BASH_VERSION}" -a -z "${ZSH_VERSION}" ] |
| 46 | + then |
| 47 | + exe_shell="`command -v "bash" `" |
| 48 | + exe_shell="${exe_shell:-`command -v "zsh" `}" |
| 49 | + |
| 50 | + script="$0" |
| 51 | + |
| 52 | + # |
| 53 | + # Quote incoming arguments for shell expansion |
| 54 | + # |
| 55 | + args="" |
| 56 | + for arg in "$@" |
| 57 | + do |
| 58 | + # True bourne sh doesn't know ${a//b/c} and <<< |
| 59 | + case "${arg}" in |
| 60 | + *\'*) |
| 61 | + # Use cat instead of echo to avoid possible echo -n |
| 62 | + # problems. Escape single quotes in string. |
| 63 | + arg="`cat <<EOF | sed -e s/\'/\'\\\"\'\\\"\'/g |
| 64 | +${arg} |
| 65 | +EOF |
| 66 | +`" |
| 67 | + ;; |
| 68 | + esac |
| 69 | + if [ -z "${args}" ] |
| 70 | + then |
| 71 | + args="'${arg}'" |
| 72 | + else |
| 73 | + args="${args} '${arg}'" |
| 74 | + fi |
| 75 | + done |
| 76 | + |
| 77 | + # |
| 78 | + # bash/zsh will use arg after -c <arg> as $0, convenient! |
| 79 | + # |
| 80 | + |
| 81 | + exec "${exe_shell:-bash}" -c ". ${script} --no-auto-shell ${args}" "${script}" |
| 82 | + fi |
| 83 | + if [ ! -z "${BASH_VERSION}" ] |
| 84 | + then |
| 85 | + set +o posix |
| 86 | + fi |
| 87 | +else |
| 88 | + shift # get rid of --no-auto-shell |
| 89 | +fi |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +# https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable |
| 94 | +r_trim_whitespace() |
| 95 | +{ |
| 96 | + RVAL="$*" |
| 97 | + RVAL="${RVAL#"${RVAL%%[![:space:]]*}"}" |
| 98 | + RVAL="${RVAL%"${RVAL##*[![:space:]]}"}" |
| 99 | +} |
| 100 | + |
| 101 | +# |
| 102 | +# This script merely strips off leading and trailing spaces from |
| 103 | +# the input. It may add an additional linefeed at the end if the input has |
| 104 | +# none. |
| 105 | +# |
| 106 | +text="`cat "$@"`" |
| 107 | + |
| 108 | +IFS=$'\n' |
| 109 | +while read -r line |
| 110 | +do |
| 111 | + r_trim_whitespace "${line}" |
| 112 | + printf "%s\n" "${RVAL}" |
| 113 | +done <<< "${text}" |
| 114 | + |
0 commit comments