We have moved to www.dataGenX.net, Keep Learning with us.

Monday, July 02, 2012

Must know for an UNIX programmer- Some tips

This article gives an overview of commands which are very useful for executing complex tasks in simple manner in UNIX environment. This can provide as reference during critical situations

Multi line comments in Shell program

As such Shell does not provide any multi-line commenting feature. However, there is a workaround. To comment a block of lines, we need to enclose the statements to be commented by using : ‘and ’. Below is an example of the same.

$ '
This is a multi-line comment that does not include a single quote in the content.

However, the above syntax works only when you don't have a single quote in the content. In order to circumvent that problem one can use the HERE document for multi-line comment as given below:

This is a multi-line comment that
does not include a single quote in the content.

The literal COMMENT is just indicative and one can use any syntactically valid literal marking the start and end of comment.

Deleting blank lines from the file

The sed (viz. Stream Editor) command can be used to delete the empty lines from the text files.

sed -e '^$' testfile

Here it is assumed that testfile contains some empty lines which are being removed.

Making control characters visible in a script

When we open a file using a standard Unix editor like vi, the control characters present in the files are not visible and so, in several scenarios the output is not as expected. Again, the sed command can come handy in such scenarios.

sed -nl testfile

List all the child process of a given parent process ID

In Unix, a script can call many other scripts and hence a parent process can have many child processes. To know the order, in which the scripts are called and the set of child processes for a particular parent process, pstree command can be used to find out.

pstree -p 10001

where in the process ID of the parent process is 10001. This command helps in identifying all the forked child processes of any script.

Finding the IP address using the hostname

Quite often we are required to know the IP address of the machine that we need to work upon. Also, it could be other way round too i.e. finding the host name of the server whose IP address is known. The command nslookup comes to the rescue in such situations. Assume that one needs to find the IP address of machine with hostname 'testserver.in.ibm.com', then the command would look like:

nslookup testserver.in.ibm.com

Upon executing the above command, the nslookup consults the Domain Name Servers and fetches the IP addresses of the given hostname.

Changing the timestamp on a file to a past date

Whenever a file is created, is takes the time stamp of current system time. To change the timestamp of files to a past date, let’s say before 10 years, the touch command is a boon. This was very useful for my project to execute a scenario where we needed to delete particular kind of files which are one year old. These files were part of newly created project and apparently we didn’t have this kind of files in the server. We changed the timestamp of files to past date, executed test cases and completed testing.

Let us take an example where we would like to update the date of the file testfile to 14 Sep 2010 01:12:34, then the command would be as follows:

touch 20100914011234 testfile

where the second argument to the command represents the timestamp in the format <year><month><day><hour><minutes><seconds>.

As seen above in all commands that there are good utilities available on UNIX operatiing system which allow the user to do complex tasks with least difficulties. By using these commands we can certainly reduce the task time and reduce the delay during project deliverables.

njoy the simplicity.......
Atul Singh

No comments :

Post a Comment