Friday, August 17, 2007

Learning smart tools: Unix Shell

As a Java/J2EE person most of the time working in development probably deals with using IDE’s and less on command lines. But I will express my love for command line tools here since it makes things faster and useful.

IDE’s are good but has its share of limitations:

a) Processing log files when running application server: Log files generate different statements and may be you would like to know all instances when a particular event happened. Following Unix command will bring all lines which contains Exception instead of having to go thru the file looking for it.

> cat server.loggrep Exception

b) Want to automate the build: If we have used command line options for builds using tools like Ant - its just one step away in QA environment. No separate effort for QA

c) Multiple steps thru IDE are error prone: Ex: suppose before we start testing the application we would like to copy some files to the server. Every-time we do it manually or using features like linking folders is not preferred since human steps are error prone and the development directory gets fixed to a given location if we use linking.

d) Suppose before every check-in you want to test your code automatically: From Ant build scripts we can keep a target which will refresh the database, perform unit tests for all the service classes.

e) Single enter is easier than multiple clicks: when you have to get a job done in a UI using multiple clicks and if the same has been put in s script – all you have to do is press enter. Easy – isn’t it !!

As a command line interface Unix shells brings a lot of tools to perform jobs quickly:

a) Need to find which process is running in a given port.

>Fport grep 8080

Fport is a windows tool which tells you which process is running at different ports.

b) Need to copy a file to a Unix server running SSH server. No ftp server is running for security reasons.

Ø scp

(Win-scp is one utility which allows one to do so)

c) Schedule a job: crontab – very nice tools in Unix and for Windows there is ‘at’ command to do this job.
d) Few more tools which perform better file management and transfer and without having to download and configure additional tools are: Tar, zip etc.

The key features which makes it powerful over dos shells is its capability of using the output of one command as the input to other and which allows someone to perform an operation by using pipes and achieving a complex operation is a small script.


Editors in Unix:
Well probably I wont refer to Unix editors for editing the java files although its much quicker to work with it. People when gets used to it don’t wanna quit but for initial users its less intuitive than a Visual IDE. Plus IDE has support for debugging etc. The main thing is one does not have to take his hand out of the keyboard while working with IDE mean working with mouse and the keyboard and going back and forth between it. So simple editing I will still use VI but for java programming Intellij IDEA/Eclipse.

UnixShell in Windows:

For unix lovers there are several options to simulate unix shells in windows. Options are
a) Cygwin
b) MKS toolkit
c) Interix


Shell scripting:
Moment someone starts working on application maintenance the need to log file processing to understand and track-down a defect or outage becomes even more prominent. Shell scripts to add logic and the single line commands to programmatic option add a lot of value. The key file processing and parsing utilities are awk and sed.
There are scripting languages like Perl also helps to perform some of these operations really well.

To conclude:

What is engineering?

Engineering means that the truth is present – i.e. we know how to solve a problem and there are possibly many ways to do it. Engineering will means using the right tool for the right purpose and every engineer needs to know more than one tool to perform the job. The good engineer knows more tools – i.e. for a java engineer knowing just java is not good enough since its not going to solve all the problems. A single line of Unix command can take 500 lines of Java code. Knowing the tools and applying the right tool when you need it makes all the difference.

What is science ?

Science is about finding truth. Most of the times all we do is engineering thus should be in constant lookout for tools which makes our life easier.

References:

http://en.wikipedia.org/wiki/Sed
http://en.wikipedia.org/wiki/AWK_(programming_language)
http://en.wikipedia.org/wiki/Crontab

No comments: