Created
September 28, 2018 18:26
-
-
Save KingMob/995cfa3f53b19d65421dd2cdfed5d154 to your computer and use it in GitHub Desktop.
Bash script to search a Java classpath for files with a regex
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/bash | |
set -euo pipefail | |
IFS=$'\n\t' | |
# Use like: | |
# LCP=$(lein classpath) # in case you need to rerun it and don't want to wait on Leiningen | |
# search-classpath.sh $LCP tools.reader | |
# search-classpath.sh $LCP Reader.class | |
# ...etc | |
function print_matches() { | |
if [ -n "$2" ]; then | |
echo "Found in: ${1}:" | |
for match in $2; do | |
if [ $# -gt 2 ] && [ -n "$3" ]; then | |
output="${match#${3}}" | |
output="${output#/}" | |
else | |
output="${match}" | |
fi | |
printf '\t%s\n' "${output}" | |
done | |
fi | |
} | |
for P in $(echo "${1}" | tr ':' '\n'); do | |
if [[ "${P}" =~ \.jar$ ]]; then | |
JARPATHS=$(jar tf "${P}" | grep -i "${2}" || true) | |
print_matches "${P}" "${JARPATHS}" | |
elif [ -d "$P" ]; then | |
FINDPATHS=$(find "${P}" -iname "*${2}*" || true) | |
print_matches "${P}/" "${FINDPATHS}" "${P}" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment