Monday, April 30, 2012

Linux Interview Questions - Part 1

   This article contains a few  interview questions related to Unix or Linux Shell Scripting and command-line stuff. Some of them could be commonly asked, most of them are not so. For questions for which we have a detailed explanation, a link would be provided after the answer for detailed reference. The idea of this article is not just to provide an answer to a question, instead to know of the many different options which are present to solve a particular problem.

1. What is Shell Scripting ?
        Shell scripting, in Linux or Unix,  is programming with the shell using which you can automate your tasks. A shell is the command interpreter which is the interface between the User and the kernel. A shell script allows you to submit a set of commands to the kernel in a batch. In addition, the shell itself is very powerful with many properties on its own, be it for string manipulation or some basic programming stuff. 
 Ref: What is Shell Scripting?

Thursday, April 26, 2012

How to zero pad a number or a variable?

  In this article, we will see the different ways in which we can do zero padding of a number or a variable or in other words, to put leading zeros to a number. Let us assume a variable "x" whose value is 23, and try to zero pad it to a length of 4.

1. Using printf command:
This is the simplest of all, and efficient too since it uses an internal command printf. The format specifier 4d will align the variable towards the right by 4 digits. By putting 04d, the same number will be zero padded 4 places to the right.
$ x=23
$ printf "%04d\n" $x

Wednesday, April 25, 2012

What is umask?

  umask, as the man page says, stands for User file creation mask which is used for determining the default permission for a new file creation. The new file creation could either be a file creation through a normal process or a file copy. umask command is a shell built-in meaning it is an internal command.

  The three file permission attributes are read, write and execute. These 3 are mapped to octal values as shown below:
          read     - 4
         write     - 2
         execute - 1

Monday, April 23, 2012

find files by name or part of name or extension

   In one of our earlier articles on find, we saw about finding files depending on their modification time. In this article, we will see about the finding files using the filename or using a part of the file name or using the extension.

1. To find all the files ending with .c :
$ find . -name "*.c"
      The '.' indicates current directory. The default behavior of find makes this command to search in the current directory and also in all the sub-directories under the current directory for the files ending with .c.

Thursday, April 19, 2012

here-doc(EOF) issue in sqlplus or FTP

   In one of our articles, we had seen the different ways in which we can connect to sqlplus from Unix. The first way discussed was using the heredoc or the here-document method(EOF). Many times users encounter a weird issue while using the here-doc in sqlplus or FTP. The issue is no statements after the closing EOF get executed .

  Let us take an example of an update query inside sqlplus:
echo script started
sqlplus -s guru/unix11@XE <<EOF         
echo script ended 

Tuesday, April 17, 2012

What is CDPATH ?

    Has it happened to you wherein you logged in to an account or to an environment and simply printed the command, say "cd log" and your directory gets changed to some log directory. The interesting part is: The log directory is not there in the directory from where you fired the cd command. It was somewhere also, however the cd command switched to it correctly. The question now is:  If the "log" directory is not present in the location from where we are giving the "cd" command, how did we reach the log directory?  The answer to this question is most probably  the CDPATH environment variable would have been set.

1. What is CDPATH?
       CDPATH is an environment variable. It looks exactly like the PATH variable containing with many different paths concatenated using ':'.

Monday, April 16, 2012

Different ways to print the first line of a file

 In one of our earlier articles, we saw how to add a header or a trailer record to a file.  Let us see in this article how to print the first line or the header record of a file. Assuming a file with the sample contents as shown below:
$ cat file
1. The default command which comes to our mind is the head command. head with the option "-1" displays the first line.
$ head -1 file

Wednesday, April 11, 2012

cp - 10 different copy command examples

  cp command is used to copy files in Unix . There are some options in cp which add lot of value to this command. In this article, we will see about some of the rarely used options of the cp command which makes cp really powerful.

1. Copy a file
cp command has the arguments source file and destination file. Here, we are copying file1 to file2.
$ cp file1 file2
$ ls -l file*
-rw-r--r-- 1 guru users 163 Apr 11 15:36 file1
-rw-r--r-- 1 guru users 163 Apr 11 15:40 file2
   The same thing can also be achieved using the below command. It might look tricky if you are seeing this for the first time.

Monday, April 9, 2012

What is sourcing a file?

   UNIX users would have heard the term "sourcing a file" pretty frequently. What exactly do we mean by sourcing a file? Which files do we source? What is the use of it? Let us see in this article about this in detail.

1. What is sourcing a file? 
  Sourcing a file is nothing but executing the file, more importantly making the changes of the file applicable in the current shell itself, the shell which we are working in. Usually, when we execute a command or a script, a new sub-shell is created and the script is executed in the sub-shell. Due to this, if the script or the command intends to make any change to the current shell, it will not happen. In order to make the changes applicable in the current shell itself, we "source" the file.

Tuesday, April 3, 2012

Different ways to get the filename from absolute path

   In this article, we will see the different ways from which we can get the filename from an absolute path name. Assume a variable "y" which contains the absolute path. Let us see how to extract the filename alone from the absolute path.
$ echo $y

Monday, April 2, 2012

date & GNU date

  date command in UNIX is used more often to create file names, rather than to check the system date. For any of our process, if we are in need of a unique file name, then one of the best options is to have the date and time as part of the file name.

 Let us see the usage of date command: