CSCI 151 - Lab 5
Command-Line Programming


Overview

In this lab, we will learn to write and edit programs in Java and Python using the command line and the Linux operating system.

Setup

  1. Find the Linux Lab in room 316 on the third floor of MCReynolds.
  2. Create an account for these machines with the help of Dr. Goadrich and the Linux Lab Manager.

Description

Follow the steps of this lab, and record your answers to the questions you encounter along the way in a separate document. You will turn in this document as a record of your completion of this lab.

Step 1 - Looking around

Log onto a Unix/Linux machine.

Open a terminal window.

At the prompt:

Step 2 - Editing a file

Next, we will write a program using a text editor.

At the prompt:

This will open a window for you to type. Input the following, replacing the WHICHPYTHON word with the result of your above "which python" command.

#!WHICHPYTHON

print("This is only a test")

Hit Control-X to exit (and say yes to the save question).

Step 3 - Running a program

At the prompt: We have written a program, but it is just a text file. To be able to run it, we need to alter its settings.

At the prompt:

Step 4 - Command-Line Arguments

At the prompt: Revise your program to be as shown here

#!WHICHPYTHON
import sys

for arg in sys.argv:
    print arg

Close and save your program.

At the prompt:

Step 5 - Arranging Files

At the prompt: At the prompt:

Step 6 - Java files

At the prompt: Input the following

public class Test {
        public static void main(String[] argv) {
                for (String arg: argv) {
                        System.out.println(arg);
                }
        }
}

At the prompt:

Compare and contrast the two programs in Python and Java.

Step 7 - Cat

At the prompt:

Next, enter the following program:

import java.util.Scanner;
import java.io.*;

public class Cat {
	public static void main(String[] argv) throws FileNotFoundException {
		if (argv.length == 0) {
			System.out.println("Usage: java Cat filename");
			System.exit(1);
		}
		File f = new File(argv[0]);
		Scanner s = new Scanner(f);
		while (s.hasNextLine()) {
			System.out.println(s.nextLine());
		}
		s.close();
	}
}

After saving the file, go back to the command line.

At the prompt:

Discuss what you think Cat does.

Step 8 - Head

At the prompt: Modify Head.java as follows, to emulate the "head" command above, that prints out a limited number of lines in the file: At the prompt: Record the output from each of the above three commands.

Step 9 - Dump

At the prompt: Enter the following code:

import java.io.*;

public class Dump {
	public static void main(String[] argv) throws IOException {
		if (argv.length == 0) {
			System.out.println("java Dump filename");
			System.exit(1);
		}
		File f = new File(argv[0]);
		PrintWriter fileOut = new PrintWriter(f);
		fileOut.println("This is a test.");
		fileOut.println("This is only a test.");
		fileOut.close();
	}
}

At the prompt:

Step 10 - Copy

Now write and test program called Copy.java.

It should take two arguments: a source file and destination file.

It should do the same thing as the "cp" command.

Step 11 - More Commands

Here are a few more useful commands.

At the prompt:

What to Hand In

Submit your document detailing the answers to the questions found in the steps above.

Grading


© Mark Goadrich, Hendrix College