"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/lreplace.in" (28 Mar 2021, 3500 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 "lreplace.in": 1.2.3_vs_1.2.4.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # lreplace: replace one string with another in a file
    5 
    6 # Copyright (C) 2004-2021 by Brian Lindholm.  This file is part of the
    7 # littleutils utility set.
    8 #
    9 # The lreplace 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 lreplace 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 DELIMITER='#'
   23 INSTRING=''
   24 INSTRINGSET='n'
   25 OUTSTRING=''
   26 OUTSTRINGSET='n'
   27 declare -i VERBOSITY=1
   28 ZERO_IN='n'
   29 ZERO_OUT='n'
   30 while getopts d:hi:o:qvzZ opts
   31 do
   32   case $opts in
   33     d) DELIMITER=${OPTARG:0:1} ;;
   34     h) echo 'lreplace LU_VERSION'
   35        echo 'usage: lreplace [-d delimiter] [-h(elp)] -i INSTRING -o OUTSTRING'
   36        echo '                [-q(uiet)] [-v(erbose)] [-z(ero_length_input_processed)]'
   37        echo '                [-Z(ero_length_output_permitted)] filename ...'
   38        exit 0 ;;
   39     i) INSTRING=$OPTARG
   40        INSTRINGSET='y' ;;
   41     o) OUTSTRING=$OPTARG
   42        OUTSTRINGSET='y' ;;
   43     q) VERBOSITY=$((${VERBOSITY}-1)) ;;
   44     v) VERBOSITY=$((${VERBOSITY}+1)) ;;
   45     z) ZERO_IN='y' ;;
   46     Z) ZERO_OUT='y' ;;
   47     *) echo 'lreplace LU_VERSION'
   48        echo 'usage: lreplace [-d delimiter] [-h(elp)] -i INSTRING -o OUTSTRING'
   49        echo '                [-q(uiet)] [-v(erbose)] [-z(ero_length_input_processed)]'
   50        echo '                [-Z(ero_length_output_permitted)] filename ...'
   51        exit 1 ;;
   52   esac
   53 done
   54 shift $((${OPTIND}-1))
   55 
   56 # make sure we have meaningful input
   57 if [ "$INSTRINGSET" = 'n' -o "$OUTSTRINGSET" = 'n' ]; then
   58   echo 'lreplace error: both input string and output string must be specified'
   59   echo 'usage: lreplace [-d delimiter] -i INSTRING -o OUTSTRING [-h(elp)] filename ...'
   60   exit 1
   61 fi
   62 if [ "X$INSTRING" = 'X' -a "$ZERO_IN" = 'n' ]; then
   63   echo 'lreplace error: the input string to be replaced must be of non-zero length'
   64   exit 1
   65 fi
   66 
   67 # set up traps
   68 trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
   69 
   70 # run through the files
   71 while [ $# -gt 0 ]; do
   72 
   73   # make sure we can read and modify file
   74   if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
   75     echo "lreplace warning: $1 is not a writeable non-directory file"
   76     shift; continue
   77   fi
   78 
   79   # skip zero-length files unless explicitly requested
   80   if [ "$ZERO_IN" = 'n' -a ! -s "$1" ]; then
   81     echo "lreplace message: length of $1 is zero; skipping..."
   82     shift; continue
   83   fi
   84 
   85   # run through sed
   86   TMPFILE=$(tempname lreplace_$$) || exit 99
   87   sed -e "s${DELIMITER}${INSTRING}${DELIMITER}${OUTSTRING}${DELIMITER}g" "$1" > ${TMPFILE}
   88 
   89   # reject zero-length output unless explicitly requested
   90   if [ "$ZERO_OUT" = 'n' -a ! -s $TMPFILE ]; then
   91     echo "lreplace warning: new length for $1 would be zero; skipping..."
   92     rm -f $TMPFILE
   93     shift; continue
   94   fi
   95 
   96   # replace if changes occurred
   97   cmp -s "$1" $TMPFILE
   98   if [ $? -eq 1 ]; then
   99     cp $TMPFILE "$1"
  100     if [ $VERBOSITY -gt 0 ]; then
  101       echo "$1: text replaced"
  102     fi
  103   elif [ $VERBOSITY -gt 1 ]; then
  104     echo "$1: unchanged"
  105   fi
  106 
  107   # clean up afterwards
  108   rm -f $TMPFILE
  109   shift
  110 done