Here is another script I wrote for work and I thought it will be interesting enough to share:
Say you want to check which JAR files (or ZIP files for that matter, as Java ARchive files are just ZIP files with a different extension) contain files that contain some text.
grep is the obvious answer, but how to grep files in JARs?
A simple loop can go over the archives you are interested in, then
unzip -c can extract the content of files to the standard output so that
grep can be used on them:
pattern="smtp" for jar in /usr/share/java/*.jar; do for file in $(unzip -l $jar | perl -nle 'split /\s+/ and print $_[-1];'); do unzip -c $jar $file | grep -qi "$pattern" && ( rpm -qf $jar 2>&1 >/dev/null && \ rpm -qf $jar || \ echo "orphan jar: $jar" ); done done | sort -u
This could also be written as single line in the terminal, obviously.
My version of the loop’s internal code looks at matching files and prints out the RPM they belong to or proclaim them to be orphaned files.
An alternative loop code might simply print the matching output and the file that contained it, a-la
output="$(unzip -c $jar $file | grep -i "$pattern")" [ -n "$output" ] && echo "$jar, $file: $output"
Although there is an obvious bug in that last code example – see if you can figure it out.