From 8584170426a7c06fb998992e9fdde659c1b418cb Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Fri, 9 Sep 2016 13:52:11 -0700 Subject: [PATCH] add isdiff --- isdiff | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 isdiff diff --git a/isdiff b/isdiff new file mode 100755 index 0000000..cb6f85f --- /dev/null +++ b/isdiff @@ -0,0 +1,70 @@ +#!/bin/bash +# Copyright (C) 2014 Ian Kelling + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +isdiff() { + local help="Usage: isdiff [--help|-h] [-v] [-x] FILE1 FILE2 +Test if FILE1 and FILE2 have the same contents + +Returns 0 for same, 1 for diff, 2 for error. + +-v verbose +-r use most reliable checksum available, ie. sha instead of md5" + + local v x y reliable + while true; do + if [[ $1 == --help ]] || [[ $1 == -[^-]* && $1 == *h* ]]; then + echo "$help" + return + elif [[ $1 == -[^-]* && $1 == *v* ]]; then + v=true + shift + elif [[ $1 == -[^-]* && $1 == *r* ]]; then + reliable=true + shift + else + break + fi + done + + x=($(type -p sha512sum) $(type -p sha256sum) $(type -p sha1sum) $(type -p md5sum)) + if [[ $reliable ]]; then + sumbinary=$x + else + sumbinary=${x[${#x[@]}-1]} + fi + + [[ $sumbinary ]] || { + echo "Need 1 of sha512sum sha256sum sha1sum or md5sum for sum() function" + return 1 + } + if [[ $# != 2 ]]; then + [[ $v ]] && echo Error: need 2 arguments. + return 2 + fi + if [[ $v ]]; then + x=$($sumbinary < "$1" ) || return 2 + y=$($sumbinary < "$2" ) || return 2 + else + { x=$($sumbinary < "$1" ) ;} 2>/dev/null || return 2 + { y=$($sumbinary < "$2" ) ;} 2>/dev/null || return 2 + fi + if [[ $x == "$y" ]]; then + return 1 # same + else + [[ $v ]] && echo "$1" "$2" different. + return 0 + fi +} +isdiff "$@" -- 2.30.2