"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/notrail.in" (28 Mar 2021, 1987 Bytes) of package /linux/privat/littleutils-1.2.4.tar.lz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "notrail.in": 1.2.3_vs_1.2.4.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # notrail: Removes all trailing spaces from text files
    5 
    6 # Copyright (C) 2004-2021 by Brian Lindholm.  This file is part of the
    7 # littleutils utility set.
    8 #
    9 # The notrail utility is free software; you can redistribute it and/or modify
   10 # it under the terms of the GNU General Public License as published by the Free
   11 # Software Foundation; either version 3, or (at your option) any later version.
   12 #
   13 # The notrail utility is distributed in the hope that it will be useful, but
   14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   15 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   16 # more details.
   17 #
   18 # You should have received a copy of the GNU General Public License along with
   19 # the littleutils.  If not, see <https://www.gnu.org/licenses/>.
   20 
   21 # get command-line options
   22 declare -i VERBOSITY=1
   23 while getopts hqv opts
   24 do
   25   case $opts in
   26     h) echo 'notrail LU_VERSION'
   27        echo 'usage: notrail [-h(elp)] [-q(uiet)] [-v(erbose)] filename ...'
   28        exit 0 ;;
   29     q) VERBOSITY=$((${VERBOSITY}-1)) ;;
   30     v) VERBOSITY=$((${VERBOSITY}+1)) ;;
   31     *) echo 'notrail LU_VERSION'
   32        echo 'usage: notrail [-h(elp)] [-q(uiet)] [-v(erbose)] filename ...'
   33        exit 1 ;;
   34   esac
   35 done
   36 shift $((${OPTIND}-1))
   37 
   38 # set up traps
   39 trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
   40 
   41 # run through the files
   42 while [ $# -gt 0 ]; do
   43 
   44   # make sure we can read and modify file
   45   if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
   46     echo "notrail warning: $1 is not a writeable non-directory file"
   47     shift; continue
   48   fi
   49 
   50   # run through sed
   51   TMPFILE=$(tempname notrail_$$) || exit 99
   52   sed -e 's/ *$//' "$1" > $TMPFILE
   53 
   54   # replace if changes occurred
   55   cmp -s "$1" $TMPFILE
   56   if [ $? -eq 1 ]; then
   57     cp $TMPFILE "$1"
   58     if [ $VERBOSITY -gt 0 ]; then
   59       echo "$1: trailing space removed"
   60     fi
   61   elif [ $VERBOSITY -gt 1 ]; then
   62     echo "$1: unchanged"
   63   fi
   64 
   65   # clean up afterwards
   66   rm -f $TMPFILE
   67   shift
   68 
   69 done