contrib/pacdiff : rework and cleanup
I initially only wanted to add a -l/--locate option to use locate instead of find, which should have been easy. Then I thought I would try to support filename with whitespace while I was at it, and this was a bit more complex. The safest ways seem to be the following ones : http://mywiki.wooledge.org/BashFAQ/020 Then I received a lot of suggestions on #bash about how to improve the script, which I tried to address. Signed-off-by: Xavier Chantry <shiningxc@gmail.com> [Dan: fix grouping of find arguments] Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
14c1a4423e
commit
dce7aa8569
1 changed files with 58 additions and 27 deletions
|
@ -1,5 +1,5 @@
|
||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
# pacdiff : a simple pacnew/pacorig/pacsave updater for /etc/
|
# pacdiff : a simple pacnew/pacorig/pacsave updater
|
||||||
#
|
#
|
||||||
# Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
|
# Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
|
||||||
#
|
#
|
||||||
|
@ -17,29 +17,60 @@
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
# Original http://phraktured.net/config/bin/pacdiff
|
|
||||||
|
|
||||||
diffprog=${DIFFPROG:-vimdiff}
|
diffprog=${DIFFPROG:-vimdiff}
|
||||||
for x in $(find /etc/ -name "*.pacnew" -o -name "*.pacorig" -o -name "*.pacsave")
|
locate=0
|
||||||
do
|
|
||||||
echo "File: ${x%.pac*}"
|
usage() {
|
||||||
chk="$(cmp $x ${x%.pac*})"
|
echo "pacdiff : a simple pacnew/pacorig/pacsave updater"
|
||||||
if [ -z "${chk}" ]; then
|
echo "Usage : pacdiff [-l]"
|
||||||
|
echo "The -l/--locate flag makes pacdiff use locate rather than find"
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd() {
|
||||||
|
if [ $locate -eq 1 ]; then
|
||||||
|
locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave
|
||||||
|
else
|
||||||
|
find /etc/ \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave \) -print0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ $# -gt 0 ]; then
|
||||||
|
case $1 in
|
||||||
|
-l|--locate)
|
||||||
|
locate=1;;
|
||||||
|
*)
|
||||||
|
usage; exit 0;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# see http://mywiki.wooledge.org/BashFAQ/020
|
||||||
|
while IFS= read -u 3 -r -d '' pacfile; do
|
||||||
|
file="${pacfile%.pac*}"
|
||||||
|
echo "File: $file"
|
||||||
|
if [ ! -f "$file" ]; then
|
||||||
|
echo " $file does not exist"
|
||||||
|
rm -i "$pacfile"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
check="$(cmp "$pacfile" "$file")"
|
||||||
|
if [ -z "${check}" ]; then
|
||||||
echo " Files are identical, removing..."
|
echo " Files are identical, removing..."
|
||||||
rm $x
|
rm "$pacfile"
|
||||||
else
|
else
|
||||||
echo -n " File differences found. (V)iew, (S)kip, (R)emove: [v/s/r] "
|
echo -n " File differences found. (V)iew, (S)kip, (R)emove: [v/s/r] "
|
||||||
read c
|
while read c; do
|
||||||
c="$(echo $c| tr A-Z a-z)" #tolower
|
case $c in
|
||||||
if [ "$c" = "r" ]; then
|
r|R) rm "$pacfile"; break ;;
|
||||||
rm $x
|
v|V)
|
||||||
elif [ "$c" = "s" ]; then
|
$diffprog "$pacfile" "$file"
|
||||||
continue
|
rm -i "$pacfile"; break ;;
|
||||||
else
|
s|S) break ;;
|
||||||
[ "$c" = "n" -o "$c" = "N" ] || $diffprog $x ${x%.pac*}
|
*) echo -n " Invalid answer. Try again: [v/s/r] "; continue ;;
|
||||||
echo -n " Remove file? [Y/n] "
|
esac
|
||||||
read c
|
done
|
||||||
[ "$c" = "n" -o "$c" = "N" ] || rm $x
|
|
||||||
fi
|
fi
|
||||||
fi
|
done 3< <(cmd)
|
||||||
done
|
|
||||||
|
exit 0
|
||||||
|
|
||||||
|
# vim: set ts=2 sw=2 noet:
|
||||||
|
|
Loading…
Add table
Reference in a new issue