Shell Scripting

From COMP15212 Wiki
Depends on Shell

Here is an example of Unix shell scripting with a view to showing how some other facilities can be exploited.

Examples

Example 1

 #!/bin/bash
 
 while read line1
 do
   read line2
   echo "$line1 $line2"
 done \
 | while read a b c d
   do
     echo "$d $c $b $a"
   done

This script runs two processes: the first reads two lines of input (the presence of the first controlling the while loop) and passes these, concatenated into a single line with a space separator, via a pipe to the other process. The second loop reads four temporary values into (environment) variables, then echoes these back to the stdout stream in reverse order.

The simple way to use this script would be to redirect the standard streams to files, thus: script <input >output

An example input file is included in here and is duplicated below for reference.

 1z      1y
 1x      1w
 2z      2y
 2x      2w
 3z      3y
 3x      3w
 4z      4y
 4x      4w
 5z      5y
 5x      5w
 6z      6y
 6x      6w
 7z      7y
 7x      7w
 8z      8y
 8x      8w
 9z      9y
 9x      9w
 10z     10y
 10x     10w