Sunday, June 22, 2014

Shell Programming


  • A shell is a program that acts as the interface between the user and the linux system, enabling us to enter commands for the operating system.


  • It resembles the linux command terminal but it is much more powerful, eg. input and output can be directed, data can be piped between simultaneous programs. 
  •  In Linux the standard shell that is invariably installed is "bash"( Bourne- Again Shell)./bin/sh is the location of the default binary of bash shell
  •   To know the bash version
To direct the output of a command to a file use > operator
 
  • test.txt command will contain all the files and folders present in the current directory

    •   ps command gives the list of all running processes.
    • To append the output of a command to an existing file we use >> operator
 
 
  • To discard the output of a command execution redirect it to /dev/null (universal linux bit bucket).
  • Standard error uses file descriptor 2 and if we use 2>errormsg.txt then all the error messages will goto errormsg.txt instead of getting redirected to standard output as default.
  • $ command >output.txt  2>errormsg.txt
  • If we want to store both the output and error messages into the same file then we use $ command >common.txt  2>&1
  • If we want to discard both output as well as error then we use
 $command > /dev/null 2>&1 


  • uniq command - uniq filters out adjacent, matching lines from input file INPUT, writing the filtered data to output file OUTPUT.
    If INPUT is not specified, uniq reads from the standard input.

    If OUTPUT is not specified, uniq writes to the standard output.

Examples


Let's say we have an eight-line text file, myfile.txt, which contains the following text:

This is a line.
This is a line.
This is a line.
 
This is also a line.
This is also a line.
 
This is also also a line.

...Here are several ways to run uniq on this file, and the output it creates:

uniq myfile.txt

This is a line.
 
This is also a line.
 
This is also also a line.
 
Displays all unique lines 
 

uniq -c myfile.txt

      3 This is a line.
      1  
      2 This is also a line.
      1  
      1 This is also also a line.
 
Displays the count of each lines .


uniq -d myfile.txt

This is a line. 
This is also a line. 
 
 
Show only duplicated lines. 

uniq -u myfile.txt

This is also also a line.
 
Show only unique lines.
 

 
 
 
 
more command 
 
more is a filter for paging through text one screen at a time. It does not provide as many options or enhancements as less, but is nevertheless quite useful and simple to use. 


Examples

more +3 myfile.txt
Display the contents of file myfile.txt, beginning at line 3.

more +/"hope" myfile.txt
Display the contents of file myfile.txt, beginning at the first line containing the string "hope". 

ls | more
List the contents of the current directory with ls, using more to display the list one screen at a time.

 
less command


less is a program similar to more, but it has many more features. less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi.

Examples

less file.txt
Views the file file.txt.
less -N file.txt
Views the file file.txt, displaying a line number at the beginning of each line.
 
 
PIPES
 
They are used to pass output of one command execution to another command and avoids writing multiple lines.
 
ex. cat mydata.txt | sort | uniq > mydata.txt
 
 
echo 

displays a line of text.
 

Options

-n do not output the trailing newline.
-e enable interpretation of backslash escapes.
 

For any clarifications or if you have any suggestion please post comments below.

No comments:

Post a Comment