Thursday, August 30, 2012

getopts - How to pass command line options to shell script?

   In an earlier article, we discussed how to pass command line arguments to shell script and access them using positional parameters. In this, we will see how to use the getopts command to pass command line options to shell scripts. Command line options are the options or switches passed to a command. For example, -l, -r, -t are some examples of the command line options passed to the ls command.

Types:
  Command line options can be classified into 3 types:
  1. Options which does not take arguments:
    • ls -l    #no arg for -l
  2. Options which take arguments:
    • cut -d","  file #arg "," for -d
  3. Options + Additional command line arguments:
    • ls -l file1 file2 file3 
Syntax:
The syntax of the getopts command is:
 getopts optstring name
-> optstring - the string which contains the list of options expected in the command line
-> name - the variable name which is used to read the command line options one by one.

Tuesday, August 28, 2012

5 examples to extract substring in bash / ksh93 shell

Sub-string is nothing but extracting a part of a string. In an earlier article, we discussed the various ways of how to extract a sub-string from every line in a file. In this article, we will see how to extract sub-string exclusively in bash / ksh93 and its options. Moreover, this substring extraction is an internal command, and hence it is very good from performance perspective:

 The syntax of the sub-string extraction is:
${variable:offset:length}
where variable is the variable containing the string,
          offset specifies the position from where to start extracting the string,
          length specifies the length of characters to be extracted from the offset.

 Let us consider a variable containing a string "Solaris"
$ x="Solaris"

Monday, August 27, 2012

Insert new line or text after every n lines in Linux

In one of our earlier articles, we discussed how to insert a line before or after a pattern. In this article, we will see how to insert a line after every n lines in a file.

Let us consider a file. The requirement is to insert a line with text "LINE" afte every 2 lines:
$ cat file
Unix
Linux
Solaris
AIX
Linux

Tuesday, August 21, 2012

What is the use of sticky bit in Linux?

    In an earlier article, we discussed the purpose of SUID, an advanced file permision. In the same lines, Sticky bit is also an advanced file permission used in Unix/Linux or its flavors. This feature was originally meant for regular files and directories, though is now used only in directories in many Unix flavors. This permission, when applied on a directory, allows users to maintain 'their' files in directories which are shared with everybody.  

Thursday, August 16, 2012

15 examples of sort command in Linux

sort command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ascii. Using options in sort command, it can also be used to sort numerically. Let us discuss it with some examples:

File with Ascii data:
 Let us consider a file with the following contents:
$ cat file
Unix
Linux
Solaris
AIX
Linux
HPUX

1. sort simply sorts the file in alphabetical order:
$ sort file
AIX
HPUX
Linux
Linux
Solaris
Unix
All records are sorted alphabetically.

Monday, August 13, 2012

7 examples to print few lines before a pattern match in Linux

In this article, we will discuss the different ways for how to print a few lines before a pattern match in log files. You will do well to know how to print a few lines AFTER a pattern match.

 Let us consider a sample file as below. The requirement is to print 2 lines before the pattern 'Linux':
$ cat file
Unix
AIX
Solaris
Linux
SCO
1. grep will do it for you if your grep is a GNU grep.
$ grep -B2 Linux file
AIX
Solaris
Linux
The option '-B' will give n lines before the pattern. Hence '-B2 Linux' gives 2 lines before the pattern 'Linux' including the line matching the pattern.

Monday, August 6, 2012

awk - Passing awk variables to shell

In one of our earlier articles, we discussed how to access or pass shell variables to awk. In this, we will see how to access the awk variables in shell? Or How to access awk variables as shell variables ?  Let us see the different ways in which we can achieve this.

 Let us consider a file with the sample contents as below:
$ cat file
Linux 20
Solaris 30
HPUX 40
1. Access the value of the entry "Solaris" in a shell variable, say x:
$ x=`awk '/Solaris/{a=$2;print a}' file`
$ echo $x
30
This approach is fine as long as we want to access only one value. What if we have to access multiple values in shell?

Thursday, August 2, 2012

Move files and directories : Inodes Part 3

  In our earlier inode articles, we discussed about the inode basics and inode relations with the soft links and hard links and their differences. In this, we will discuss about the changes in inodes during the move(mv) of a file or directory.
  
1.  Does the mv command rename a file or move a file?
      It actually does both, depending on the context. If the source and target are both files, it does rename. If the source is a file and the target is a directory, it does move. However, once we understand the internal working of the mv command with respect to inodes, we will realize both are same.