Created
October 2, 2020 03:28
-
-
Save Kreyren/c83ec2fa52dc6e001c753643c7c6c1c1 to your computer and use it in GitHub Desktop.
Quick PoC script for file content comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# shellcheck shell=sh # Written to be POSIX compatible | |
# Created by Jacob Hrbek identified using a GPG identifier assigned to an electronic mail <kreyren@rixotstudio.cz> according to keyserver <https://keys.openpgp.org/> assuming said keyserver not hijacked | |
#set -e # Exit on false | |
###! Quick assertion wrapper | |
die() { printf 'FATAL: %s\n' "$2"; exit "$1" ;} | |
###! This is a dumb Proof-of-concept for implementation of showing a difference of two files for Sumit | |
###! Expects two paths to the file provided as an argument that it will try to compare | |
###! This is a haste implementation and should not be used in any system | |
compareLines() { | |
firstFile="$1" | |
secondFile="$2" | |
# Set the counter so that $((lines+1)) works | |
lines=0 | |
# Process each line in file supplied in the first argument | |
## NOTICE: Do not use for loop to avoid pitfall https://mywiki.wooledge.org/BashPitfalls#pf1 | |
for lines in 1 2; do | |
# Show the line that is being compared | |
content="$(ex +${lines}p -scq "$firstFile")" | |
compared_content="$(ex +${lines}p -scq "$secondFile")" | |
# Compare the lines | |
if [ "$content" != "$compared_content" ]; then | |
# Output that the file is different | |
cat <<-OUTPUT | |
Line '$lines' is different in file '$1' comparing to the same line in file '$2' | |
>>> $content | |
>>> $compared_content | |
OUTPUT | |
elif [ "$content" = "$compared_content" ]; then | |
# Output that the file is same | |
cat <<-OUTPUT | |
Line '$lines' is same in file '$1' comparing to the same line in file '$2' | |
>>> $content | |
>>> $compared_content | |
OUTPUT | |
else | |
die 255 "Unexpected trap, DAMN YOU SUN AND YOUR COSMIC RAYS! .. or developer is incompetent" | |
fi | |
done | |
} | |
# Invoke | |
compareLines "$(pwd)/A.md" "$(pwd)/B.md" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment