CSC212 Lab 8 2014

From dftwiki3
Revision as of 14:22, 15 October 2014 by Thiebaut (talk | contribs) (Redirection)
Jump to: navigation, search

--D. Thiebaut (talk) 15:07, 15 October 2014 (EDT)



This lab is about programming shell scripts, using redirection, pipes, for-loops and various shell tools.


Connect to Linux


  • For this lab, we won't use Eclipse. We'll use the Terminal instead.
  • Connect to your 212a account on one of the Linux Mint machines, or, using your laptop, directly to beowulf2. If beowulf2 is unresponsive, you can connect from your laptop to any of these IP addresses, which belong to our Linux Mint machines in FH342 and FH345. In this case you should replace beowulf2.csc.smith.edu by a group of 4 numbers separated by dots. Note that this machine must be ON and have Linux booted and running before you can actually connect to it. You can connect to amachine even if somebody else is using it, or is already connected to it.

This section is only visible to computers located at Smith College


Redirection


  • Every command, every program in the Linux operating system reads data from the standard-input, and outputs information to the standard-output and the standard-error. By default the standard-input, or sdin for short, is attached to the keyboard.

By default, the standard-output (stdout for short) and the standard-error (stderr for short) are connected to the display.

  • Here is a short Java program that outputs to both stdout and stderr:


public class Demo1 {
	public static void main(String[] args) {
		System.out.println( "This goes to stdout" );
		System.err.println( "This goes to stderr" );
	}
}
Note the different streams used to pring: System.out and System.err.
  • Run the program in the Terminal.
  • Note the output, two very similar lines.


Redirecting stdout to a file.


  • Let's run the program, but using the redirection symbol > to send stdout to a file:


java Demo1  >  Demo1.output


  • Notice that the program still outputs one line on the screen. But only 1. To see the other line,

cat the file Demo1.output.

cat Demo1.output

You will see that the second line, the one directed to stdout has been captured in the file.


The purpose of stderr, is for program to send error messages to the terminal, and to have these messages displayed on the terminal, even when the output has been redirected to a file. We use output to stderr typically for error messages.


Redirecting outputs to an already-existing file


  • Try the command date, which is a typical Linux command:
date

  • Now capture the output of date to a file:
date  > Demo1.output
cat Demo1.output

You will have noticed that the redirection didn't work. You cannot redirect the output of a command to a file that already exists. If you want to force the redirection, add an ! after the >:
date >! Demo1.output
cat Demo1.output


Redirecting the output of several commands to the same file


  • What if we wanted to store both the date and the output of Demo1 to Demo1.output? In this case we do not want the second output to erase the file. Linux uses >> to indicate redirection/append-to-end-of file.
  • Let's try it:


date >! Demo1.output
java Demo1  >> Demo1.output

  • Verify (using cat) that Demo1.output now contains a time tag, and the output of java Demo1.