Skip to content

Instantly share code, notes, and snippets.

@brunerd
Last active December 17, 2020 22:48
Show Gist options
  • Save brunerd/fc2507a3e138e615d84e8f322e6d7c3b to your computer and use it in GitHub Desktop.
Save brunerd/fc2507a3e138e615d84e8f322e6d7c3b to your computer and use it in GitHub Desktop.
this will crawl mostly system areas of macOS and attempt to parse any JSON found, it will produce a CSV as well as echo the result to the console
#!/bin/bash
#jsonReport-macOS (c) Joel Bruner 2020, MIT License
#this will crawl mostly system areas of macOS and attempt to parse JSON found
#it will produce a CSV as well as echo out to the console the result
#############
# VARIABLES #
#############
searchRoot=${1}
######################
# COMPUTED VARIABLES #
######################
myFolder=$(dirname "${0}")
dateStamp=$(date +"%Y-%m-%d_%H%M")
myCSV="${myFolder}/jsonReport-${dateStamp}.csv"
########
# MAIN #
########
read -p "Please enter a path to search [default=/]: " searchRoot
#default to /
searchRoot=${searchRoot:=/}
#remove any trailing / (or / if that is all)
searchRootPrefix=${searchRoot%/}
echo "Begin ($(date)). Finding all .json files in macOS System(ish) paths within: ${searchRoot}"
#unplug from external media and networks - despite being excluded, ALL paths are still crawled
#https://stackoverflow.com/questions/4210042/how-to-exclude-a-directory-in-find-command
#jsonFiles=$(find / -not \( -path /System/Volumes/\* \) -not \( -path /usr/local/\* \) -not \( -path /Users/\* \) -not \( -path /Applications/\* \) -not \( -path /Volumes/\* \) -not \( -path /dev/\* \) -name '*.json' 2>/dev/null)
jsonFiles=$(find "${searchRoot}" -not -path "${searchRootPrefix}/System/Volumes/*" -not -path "${searchRootPrefix}/usr/local/*" -not -path "${searchRootPrefix}/Users/*" -not -path "${searchRootPrefix}/Applications/*" -not -path "${searchRootPrefix}/Volumes/*" -not -path "${searchRootPrefix}/dev/*" -type f -name '*.json' 2>/dev/null)
#output a header for the CSV
echo "Status,Path,Error Output" | tee -a "${myCSV}"
IFS=$'\n'
for jsonFile in $jsonFiles; do
testResult=$(json_pp 2>&1 < "$jsonFile")
exitCode=$?
if [ "${exitCode}" -eq 0 ] ; then
echo "OK,${jsonFile}," | tee -a "${myCSV}"
else
echo "ERROR,${jsonFile},\"${testResult//\"/\"\"}\"" | tee -a "${myCSV}"
fi
done
echo "Finished ($(date)). Results written to: ${myCSV}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment