Skip to content

Code Snippets

February 17, 2011

Listing out some code snippets:

1. Short 2 liner code which will give the highest number of an array.
public int guessWhat( int arr[] )
for( int i = 0, x= 0; i < arr.length; i++ )
x = x < arr[i] ? arr[i] : x;
return x;

2. Find the port occupied by the process or process-id.
netstat -nlp | grep $pid | awk ‘{print $4}’ | cut -f2 -d:

3. List all the ports occupied in a linux/solaris machine
netstat -nlp | grep tcp | awk {print $4} | cut -f2 -d:

4. jfind utility which will search for class file (s) from jar files in a given directory.
user@sqindia$ sh jfind directory-name class1 class2 ……

if [ ! -x ${jarDir} ] ; then echo “Directory or File $jarDir does not exit.”; exit 1; fi
shift 1;
tf=”`echo “$*” | sed -e ‘s/ /\\\\|/g’`”;
for i in `find $jarDir -name “*.jar” -print`
out=`jar -tvf $i | grep -w ${tf}* | awk ‘{print $8}’`;
if [ “$out” != “” ] ; then
echo “————————-“;
echo “Jar file: ‘${i}’ “;
out1=`echo $out | sed -e ‘s/ /\\\\n/g’`;
echo -e $out1;
echo “————————-“;
if [ “$c” = “0” ]; then
echo “The classname ‘$cls’ cannot be found.”;


[bala@cepheusvir16 ~]$ jfind $JAVA_HOME/jre/lib BootstrapServer StubEntry BadPaddingException
Jar file: ‘/usr/local/java/jre/lib/jce.jar’

Jar file: ‘/usr/local/java/jre/lib/rt.jar’

Jfind is a very cool utility which will help in searching for class file(s) from huge jar-file repository.

5. Automate interactive script using expect
#!/usr/local/bin/expect -f #
spawn ssh
expect “password:”
send xxxxx\n
put employees.lst
put student.lst
echo “Transfer complete”

7. On Solaris you can create a read-only version of any directory by adding something like this to /etc/auto_direct:
/var/rot -ro,fstype=lofs :/var/tmp
Then you can go to /var/rot to read what’s in /var/tmp, but you can’t write anything.


From → tech

  1. Just wish to say your article is as surprising. The clearness to your post is just excellent
    and i could assume you are knowledgeable in this subject.
    Fine with your permission allow me to take hold of your RSS feed to stay
    up to date with imminent post. Thank you a million and please continue the
    gratifying work.

  2. An intriguing discussion is worth comment. I believe that you should write more about this topic, it might not be a
    taboo subject but typically people don’t talk about such subjects. To the next! Best wishes!!

  3. If you’re setting up a new company you may already be considering engaging the services of a chartered accountant to help you keep books and sort your accounts. This allows them to earn money enough to make a living. Their education focuses on tax laws and makes them qualified to handle matters like the preparation of estate tax returns or facilitating cases filed at a tax court, although they may also know the general workings of doing business accounting.

  4. I have to thank you for the efforts you’ve put in writing this site. I really hope to view the same high-grade content from you in the future as well. In fact, your creative writing abilities has encouraged me to get my own website now 😉

  5. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three emails with the same comment.
    Is there any way you can remove me from that service?
    Appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: