Difference between revisions of "BASH Scripting"

From PHYSpedia
Jump to: navigation, search
(Pipes (the case for stdin))
(Pipes (the case for stdin))
Line 17: Line 17:
 
   3 2
 
   3 2
 
   2 3
 
   2 3
The -k 2 option tells sort to sort the data based on column 2. -g tells it to sort based on numerical value
+
The -k 2 option tells sort to sort the data based on column 2. -g tells it to sort based on numerical value, rather than text (ascii) value.
 +
 
 +
Pipes simply connect the standard output of one command to the standard input of another command. In fact, pipes are completely separate from the commands, i.e. you can use pipes with programs you write. Since pipes work with stdin and stdout, you can write a program that reads from stdin and writes to stdout and use your programs with pipes. For example, consider a simple C++ program that reads numbers from standard input and computes their average,
 +
#include <iostream>
 +
 +
int main()
 +
{
 +
  double x, sum;
 +
  int N;
 +
 +
  sum = 0;
 +
  N  = 0;
 +
  while( std::cin>>x )
 +
  {
 +
    sum += x;
 +
    N++;
 +
  }
 +
 +
  std::cout<<sum / N;
 +
}

Revision as of 20:30, 6 September 2011

Scripting refers to the practice of writing "scripts" to perform tasks on a machine. A shell script is a file that contains a set of command that well be executed as if they were typed in at the command line. Shell scripts make use of the wide range of command line tools that already exist for processing text and data. Scripts have been used by system administrators for years to automate repetitive task, but scripting has many applications in computational physics and high performance computing.

Pipes (the case for stdin)

One of the most powerful features of Unix, and therefore Linux, is pipes. Pipes direct the standard output of one program to the standard input of another program. So, for exmaple, the cat command prints the contents of a file to standard output

$ cat file.txt
  1 4
  2 3
  3 2

In this example, the file named file.txt contains three lines. The cat command prints these lines to standard output. Now, the grep command reads lines from standard input, and prints lines that match a pattern to standard output. So, if we wanted to see only the lines in file.txt that contain the number 3, we can pipe the standard output of the cat command to grep

$ cat file.txt | grep "3"
  2 3
  3 2

Here, we have two separate commands to build a single output. There is no limit to the number of pipes we can chain together (or, if there is, this limit is much larger than you will need). We could take the output of the last command and pipe it through sort to show the lines in descending order on column two.

$ cat file.txt | grep "3" | sort -g -k 2
  3 2
  2 3

The -k 2 option tells sort to sort the data based on column 2. -g tells it to sort based on numerical value, rather than text (ascii) value.

Pipes simply connect the standard output of one command to the standard input of another command. In fact, pipes are completely separate from the commands, i.e. you can use pipes with programs you write. Since pipes work with stdin and stdout, you can write a program that reads from stdin and writes to stdout and use your programs with pipes. For example, consider a simple C++ program that reads numbers from standard input and computes their average,

#include <iostream>

int main()
{
  double x, sum;
  int N;

  sum = 0;
  N   = 0;
  while( std::cin>>x )
  {
    sum += x;
    N++;
  }

  std::cout<<sum / N;
}