Thursday, March 29, 2012

10 good shell scripting practices

   Everybody working in UNIX can do a decent level of shell scripting. Depending on your expertise, the kind of commands you use, the way you look at a problem, the mode of arriving at a solution could be different. For people in their early stages of shell scripting, it is good to follow certain practices which will help you  in learning the art faster, cleaner and better. We discussed about the 5 important things to follow to  become good in shell scripting in one of our earlier articles. Along the same lines, the following are the 10 points in which we will discuss about the good practices:

1. Learn less, do more: You want to do some scripting in UNIX, be it in shell, perl or python. So, you took a book and started  studying. Some people study the entire thing first and start practising. This might be a good method for some, but I do not believe too much in this. Instead, just study the basic, the very basic with which you can start with something. Once done with the basic, start writing simple programs. Gradually, build your requirement. Go back and develop your program further. If you get stuck due to lack of knowledge, go back to the book. Read what you want. Come back and start developing. Build requirement further. Read a little more if stuck. Carry on. I believe in this way than to read the entire stuff first because how much ever we read, unless we start practicing, we cannot correlate lot of things, and it does not add much value to your study either. This learn-practice-stuck method helps me a lot.

Tuesday, March 27, 2012

sed - Replace or substitute file contents - Part 2

 In one of our earlier articles, we saw about Replace and substitute using sed. In continuation to it, we will see a few more frequent search and replace operations done on files using sed.

Let us consider a file with the following contents:
$ cat file

Monday, March 26, 2012

Internal vs External commands

  UNIX commands are classified into two types
  • Internal Commands - Ex: cd, source, fg
  • External Commands - Ex: ls, cat
Let us look at these in detail

Internal Command:
   Internal commands are something which is built into the shell. For the shell built in commands, the execution speed is really high. It is because no process needs to be spawned for executing it.    For example, when using the "cd" command, no process is created. The current directory simply gets changed on executing it.

External Command:
  External commands are not built into the shell. These are executables present in a separate file. When an external command has to be executed, a new process has to be spawned and the command gets executed. For example, when you execute the "cat" command, which usually is at /usr/bin, the executable /usr/bin/cat gets executed.

Monday, March 19, 2012

Join every 2 lines in a file

  In this article, we will see the different ways in which we can join every two lines in a file. We will join them using comma as the delimiter.
    Assume a file with the following contents as shown below. The file below shows stats for countries. The only issue in the file is: the stats are not directly against the country names, instead they are in the next line.
$ cat file

Wednesday, March 14, 2012

Soft Links & Hard Links - All about Inodes - Part 2

 In one of our earlier articles, we discussed about the inode structure and its properties under the title All about Inodes. In continuation to the same, in this article, we will see about the links in UNIX: hard links and soft links, and their relationship with Inodes. We will learn them in the way of questions and answers.

Tuesday, March 13, 2012

Different ways to capitalize the contents of a file

   In this article, we will see the different ways in which we can capitalize the contents of the file and also the other way around. In other words, this will tell how to convert the lowercase characters or string into upper case and upper case into lower case.

Let us take a sample file, "file1", with the following contents:
$ cat file1

Monday, March 12, 2012

Retrieve table names from Oracle queries

    Sometimes, you might have a requirement where you have a file full of Oracle sql queries, and you want to filter out the table names alone. In this article, we are going to look into a Perl script which is for this very purpose.  Given a file with sql queries which could be comprising of SELECT, UPDATE, INSERT or DELETE statements, the script will parse the sql query file and list out the table names alone.

Thursday, March 8, 2012

typeset - arithmetic operations

   typeset is one of those many beautiful commands which is highly underused. In simple words, typeset is the one which is used to declare variables. Interesting!! We do not usually declare variables in Shell, isn't it? Let us see in the article the use of typeset.
   typeset command has different options to use with, but we are going to discuss only the "-i" option in this article.

1. Assignment
$ typeset -i x 

Wednesday, March 7, 2012

Disk Usage and Free Space- Housekeeping

  File system and directories should be kept clean and tidy, else they can anytime messup. Say, you get a mail from your system admin saying the filesystem is full, and asking you to clean up the files. Or say your TL comes and stands next to you asking you to free some space in your account so as to get some free space.How do we do it?  In this article, we will see about these house keeping stuffs.

1. How do you find out the file system capacity or free space?

The df command lists all the filesystems in the system. Infact, df -h gives more readable output:
$ df -h
Filesystem   Size  Used   Avail Use%  Mounted on
/dev/dev1    97G   6.0G   86G   7%    /
/dev/dev2    97G   96G     1G   99%   /home

Thursday, March 1, 2012

15 different ways to display the contents of a file

  Looking at the file contents is one such activity which a UNIX developer might be doing in sleep also. In this article, we will see the umpteen different ways in which we can display or print the contents of a file.

Let us consider a file named "a", with the following  contents:
1. The first is the most common cat command:
$ cat a