I recently busy refactoring a history project, but due to objective conditions, no test cases are available, so I have to compare the result set of the old and new servers through human flesh. Consistent to judge right or wrong. Since laziness is the virtue of a programmer, I want to write a tool, and the result set is JSON, so I have jsondiff.sh.
The logic is very simple, nothing more than to get the result set on different servers through curl, and then diff, but there are a few points to note here: First, JSON is a line. Direct diff will lose meaning; secondly, Chinese characters in JSON will be encoded, which is not conducive to viewing; in addition, the order of fields in JSON is indifferent, so it is best to sort before diff. Explain that when formatting JSON data, I didn't use Bash, but instead used PHP The usage of "getopt" is worth noting. The related references are as follows: When using, a variety of tools are allowed. By default, "colordiff" will be used first. If necessary, vimdiff can be activated. "." Not long after this article was written, I found a good tool "jq" for parsing JSON from the command line. If you know it can save a lot of time, just practice writing the shell.
:#!/bin/bashRM=/bin/rmPHP=/usr/bin /phpCURL=/usr/bin/curlDIFF=/usr/bin/diffVIMDIFF=/usr/bin/vimdiffCOLORDIFF=/usr/bin/colordiffusage() { echo "Usage: $0 --uri=<URI> --old =<IP> --new=<IP>"}format() { $PHP -R ' function ksort_recursive(&$array) { if (!is_array($array)) { return; } ksort($ Array); foreach (array_keys($array) as $key) { ksort_recursive($array[$key]); } } $options = JSON_PRETTY_PRINT |
JSON_UNESCAPED_UNICODE; $array = json_decode($argn, true); ksort_recursive($array); echo json_encode($array, $options); '}request() { $CURL -s -H "Host: $1" " Http://$2$3"}eval set -- $( getopt -q -o "h" -l "host:,uri:,old:,new:,vim,help" -- "$@ ")while true; do case "$1" in --host) HOST=$2; shift 2;; --uri) URI=$2; shift 2;; --old) OLD=$2; shift 2;; --new) NEW=$2; shift 2;; --vim) VIM="Y"; shift 1;; -h|
--help) usage; exit 0;; --) break;; esacdoneif [[ -z "$URI" |
|
-z "$OLD" |
|
-z "$NEW" ]]; then usage exit 1fiif [[ -z "$HOST" ]]; then HOST="www.foobar.com"fiOLD_FILE=$(mktemp)NEW_FILE=$(mktemp)request "$HOST" "$OLD" "$URI" |
Format > $OLD_FILErequest "$HOST" "$NEW" "$URI" |
Format > $NEW_FILEif [[ "$VIM" == "Y" ]]; then $VIMDIFF $OLD_FILE $NEW_FILEelif [[ -x "$COLORDIFF" ]]; then $COLORDIFF -u $OLD_FILE $NEW_FILEelse $ DIFF -u $OLD_FILE $NEW_FILEfi$RM -f $OLD_FILE$RM -f $NEW_FILE
This article introduces the concept, role, and usage of LVM to help you understand LVM and to use LV
RedHat Linux provides firewall protection for increased system security. A firewall exists between y
The linux shell has its own set of flow control statements, including conditional statements (if),
Nginx starts with version 0.7.48 and supports Squid-like caching. This cache uses the URL and relate
Linux DDOS attack on Windows system
Solution to common problems in Linux system
7 urgent troubleshooting methods for Linux common faults
Linux system installation Eclipse method introduction
Whether the Linux system is hacked How to judge
Fedora configuration problem under Fedora Linux
Centos view port usage and enable port command
Tips for flexible use of expect scripts under Linux
Upgrade the server Have you considered these ten things?
How to solve the parameter error of Win8 system open disk?
Where is the win10 screenshot tool? How to use it?
Modify the command to make the Windows disk cleaning function more powerful
What happens to Win7 Gadgets that meet Win10?
Restrict disk access in Win8 systems
Win10 system prompts Windows update message prohibition method
Strong! Modify the icon of the video, music and other folders in the Windows 7 "Library"