"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "scripts/lreplace.in" between
littleutils-1.2.3.tar.lz and littleutils-1.2.4.tar.lz

About: littleutils are a collection of small and simple utilities (rename files, search for duplicate files, ...).

lreplace.in  (littleutils-1.2.3.tar.lz):lreplace.in  (littleutils-1.2.4.tar.lz)
#! PROGBASH #! PROGBASH
# vim: set filetype=bash: # vim: set filetype=bash:
# lreplace: replace one string with another in a file # lreplace: replace one string with another in a file
# Copyright (C) 2004-2019 by Brian Lindholm. This file is part of the # Copyright (C) 2004-2021 by Brian Lindholm. This file is part of the
# littleutils utility set. # littleutils utility set.
# #
# The lreplace utility is free software; you can redistribute it and/or modify # The lreplace utility is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the Free # it under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later version. # Software Foundation; either version 3, or (at your option) any later version.
# #
# The lreplace utility is distributed in the hope that it will be useful, but # The lreplace utility is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. # more details.
# #
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# the littleutils. If not, see <https://www.gnu.org/licenses/>. # the littleutils. If not, see <https://www.gnu.org/licenses/>.
# get a valid temporary directory and set up traps
TMPWILD=`tempname -w lreplace_$$` || exit 99
trap 'rm -f ${TMPWILD} ; exit 1' 1 2 3 13 15
trap 'rm -f ${TMPWILD} ; exit 0' 0
# get command-line options # get command-line options
DELIMITER='#' DELIMITER='#'
INSTRING='' INSTRING=''
INSTRINGSET=n INSTRINGSET='n'
OUTSTRING='' OUTSTRING=''
OUTSTRINGSET=n OUTSTRINGSET='n'
VERBOSITY=1 declare -i VERBOSITY=1
ZERO_IN=n ZERO_IN='n'
ZERO_OUT=n ZERO_OUT='n'
while getopts d:hi:o:qvzZ opts while getopts d:hi:o:qvzZ opts
do do
case $opts in case $opts in
d) DELIMITER=${OPTARG:0:1} ;; d) DELIMITER=${OPTARG:0:1} ;;
h) echo 'lreplace LU_VERSION' h) echo 'lreplace LU_VERSION'
echo 'usage: lreplace [-d delimiter] [-h(elp)] -i INSTRING -o OUTSTRING' echo 'usage: lreplace [-d delimiter] [-h(elp)] -i INSTRING -o OUTSTRING'
echo ' [-q(uiet)] [-v(erbose)] [-z(ero_length_input_proces sed)]' echo ' [-q(uiet)] [-v(erbose)] [-z(ero_length_input_proces sed)]'
echo ' [-Z(ero_length_output_permitted)] filename ...' echo ' [-Z(ero_length_output_permitted)] filename ...'
exit 0 ;; exit 0 ;;
i) INSTRING=${OPTARG} i) INSTRING=$OPTARG
INSTRINGSET=y ;; INSTRINGSET='y' ;;
o) OUTSTRING=${OPTARG} o) OUTSTRING=$OPTARG
OUTSTRINGSET=y ;; OUTSTRINGSET='y' ;;
q) VERBOSITY=$((${VERBOSITY}-1)) ;; q) VERBOSITY=$((${VERBOSITY}-1)) ;;
v) VERBOSITY=$((${VERBOSITY}+1)) ;; v) VERBOSITY=$((${VERBOSITY}+1)) ;;
z) ZERO_IN=y ;; z) ZERO_IN='y' ;;
Z) ZERO_OUT=y ;; Z) ZERO_OUT='y' ;;
*) echo 'lreplace LU_VERSION' *) echo 'lreplace LU_VERSION'
echo 'usage: lreplace [-d delimiter] [-h(elp)] -i INSTRING -o OUTSTRING' echo 'usage: lreplace [-d delimiter] [-h(elp)] -i INSTRING -o OUTSTRING'
echo ' [-q(uiet)] [-v(erbose)] [-z(ero_length_input_proces sed)]' echo ' [-q(uiet)] [-v(erbose)] [-z(ero_length_input_proces sed)]'
echo ' [-Z(ero_length_output_permitted)] filename ...' echo ' [-Z(ero_length_output_permitted)] filename ...'
exit 1 ;; exit 1 ;;
esac esac
done done
shift `expr ${OPTIND} - 1` shift $((${OPTIND}-1))
# make sure we have meaningful input # make sure we have meaningful input
if [ "$INSTRINGSET" = 'n' -o "$OUTSTRINGSET" = 'n' ]; then if [ "$INSTRINGSET" = 'n' -o "$OUTSTRINGSET" = 'n' ]; then
echo 'lreplace error: both input string and output string must be specified' echo 'lreplace error: both input string and output string must be specified'
echo 'usage: lreplace [-d delimiter] -i INSTRING -o OUTSTRING [-h(elp)] filena me ...' echo 'usage: lreplace [-d delimiter] -i INSTRING -o OUTSTRING [-h(elp)] filena me ...'
exit 1 exit 1
fi fi
if [ "X$INSTRING" = 'X' -a "$ZERO_IN" = 'n' ]; then if [ "X$INSTRING" = 'X' -a "$ZERO_IN" = 'n' ]; then
echo 'lreplace error: the input string to be replaced must be of non-zero leng th' echo 'lreplace error: the input string to be replaced must be of non-zero leng th'
exit 1 exit 1
fi fi
# set up traps
trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
# run through the files # run through the files
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
# make sure we can read and modify file # make sure we can read and modify file
if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
echo "lreplace warning: $1 is not a writeable non-directory file" echo "lreplace warning: $1 is not a writeable non-directory file"
shift; continue shift; continue
fi fi
# skip zero-length files unless explicitly requested # skip zero-length files unless explicitly requested
if [ "$ZERO_IN" = 'n' -a ! -s "$1" ]; then if [ "$ZERO_IN" = 'n' -a ! -s "$1" ]; then
echo "lreplace message: length of $1 is zero; skipping..." echo "lreplace message: length of $1 is zero; skipping..."
shift; continue shift; continue
fi fi
# run through sed # run through sed
TMPFILE=`tempname lreplace_$$` || exit 99 TMPFILE=$(tempname lreplace_$$) || exit 99
sed -e "s${DELIMITER}${INSTRING}${DELIMITER}${OUTSTRING}${DELIMITER}g" "$1" > ${TMPFILE} sed -e "s${DELIMITER}${INSTRING}${DELIMITER}${OUTSTRING}${DELIMITER}g" "$1" > ${TMPFILE}
# reject zero-length output unless explicitly requested # reject zero-length output unless explicitly requested
if [ "$ZERO_OUT" = 'n' -a ! -s ${TMPFILE} ]; then if [ "$ZERO_OUT" = 'n' -a ! -s $TMPFILE ]; then
echo "lreplace warning: new length for $1 would be zero; skipping..." echo "lreplace warning: new length for $1 would be zero; skipping..."
rm -f ${TMPFILE} rm -f $TMPFILE
shift; continue shift; continue
fi fi
# replace if changes occurred # replace if changes occurred
cmp -s "$1" ${TMPFILE} cmp -s "$1" $TMPFILE
if [ $? -eq 1 ]; then if [ $? -eq 1 ]; then
cp ${TMPFILE} "$1" cp $TMPFILE "$1"
if [ "$VERBOSITY" -gt 0 ]; then if [ $VERBOSITY -gt 0 ]; then
echo "$1: text replaced" echo "$1: text replaced"
fi fi
elif [ "$VERBOSITY" -gt 1 ]; then elif [ $VERBOSITY -gt 1 ]; then
echo "$1: unchanged" echo "$1: unchanged"
fi fi
# clean up afterwards # clean up afterwards
rm -f ${TMPFILE} rm -f $TMPFILE
shift shift
done done
rm -f ${TMPWILD}
 End of changes. 16 change blocks. 
26 lines changed or deleted 24 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)