BASH Scripting

From PHYSpedia
Revision as of 16:42, 6 September 2011 by Cclark (talk | contribs) (Pipes (the case for stdin))

Jump to: navigation, search

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 2
  2 3
  3 4

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 4