Jar Finder:
Have you ever had to look for a class in your classpath? Here's a script, my favorite script of all, that accomplishes that task.
#!/bin/sh
for JAR_FILE in `find $1 -name \*.jar`
do
echo $JAR_FILE
jar tvf $JAR_FILE | grep $2
done
To execute this script execute:
jgrep /home/jgathings/ String
What this does is search for the class "String" in all jars in /home/jgathings/ path.
This is not a one-liner, but it is a useful script. Here's the one-liner
find . -name \*.jar | awk '{print "echo "$1"; jar tvf "$1"| grep ApacheConf.class"}' | sh
A bit confusing? Well, let's break it down. First, note that the one-liner is composed of several tokens delimeted by the unix pipe. The first token uses 'find' to find all jars. The second token uses awk to construct a dynamic command. This token is semicolon delimeted. First AWK prints the name of the jar including it's path. The second AWK token uses jar to list the items in the jar. The output of this token is piped to grep which checks to see if the specified class is in the jar. This example searches for ApacheConf.class. Next the AWK command is closed with the }. Finally the entire command is piped to the shell with 'sh' and the output is printed to the screen. The output could be appended to a file with the > operator. Here's an output snippet:
./server/lib/tomcat-util.jar
./server/lib/catalina-storeconfig.jar
./server/lib/servlets-invoker.jar
./server/lib/tomcat-ajp.jar
12497 Mon Jan 28 13:36:16 GMT-05:00 2008 org/apache/jk/config/ApacheConfig.class
./server/lib/catalina-ant-jmx.jar
./server/lib/tomcat-coyote.jar
./server/lib/catalina-cluster.jar
./server/lib/tomcat-apr.jar
./bin/bootstrap.jar
James Gathings
Tuesday, January 13, 2009
One Line Unix Scripts
There are many positive aspects of one line unix scripts. One line unix scripts are quite similar to good, witty conversation. They tease your brain a bit because one liner's are similar to a puzzle. You also get great satisfaction when you solve the puzzle. Unix offers a plethora of one-line tools to play. You have AWK, SED, Bash, etc. at your fingertips. What follows are a few of my favorite scripts.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment