CSCI 151 - Lab 7
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
- Create an account for these machines with the help of Dr. Goadrich and the Linux Lab Manager.
- Putty SSH (if Windows machine)
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 in the CSCI lab via SSH. This could be via the Putty program linked above for Windows,
or through the Terminal program on Mac OS. Details on this will be given in lab.
At the prompt:
- Type "ls" - What does it do?
- Type "ls -l" - What is the difference?
- Type "which python" - What does it say?
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:
- Type "ls -l test.py" - Examine the funny letters in the column.
- Type "./test.py" - What happens?
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:
- Type "chmod a+x test.py"
- Type "ls -l test.py" - Compare with your earlier ls command.
- Type "./test.py" - What happens now?
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:
- Type "./test.py a b c d e" - What happens?
Step 5 - Arranging Files
At the prompt:
- Type "mkdir pythonCode"
- Type "ls" - What do you see?
At the prompt:
- Type "mv test.py pythonCode"
- Type "ls"
- Type "ls pythonCode"
- Type "cd pythonCode"
- Type "ls"
- Type "pwd"
- Type "cd .."
- Type "ls"
- Type "pwd" - What just happened? Where did your file go?
Step 6 - Java files
At the prompt:
- Type "mkdir javaCode"
- Type "cd javaCode"
- Type "nano Test.java"
Input the following
public class Test {
public static void main(String[] argv) {
for (String arg: argv) {
System.out.println(arg);
}
}
}
At the prompt:
- Type "javac Test.java"
- Type "ls"
- Type "java Test a b c d e"
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:
- Type "javac Cat.java"
- Type "java Cat Cat.java"
- Type "cat Cat.java"
Discuss what you think Cat does.
Step 8 - Head
At the prompt:
- Type "head -10 Cat.java" - What does "head" do?
- Type "cp Cat.java Head.java"
- Type "ls"
- Type "nano Head.java"
Modify Head.java as follows, to emulate the "head" command above, that prints
out a limited number of lines in the file:
- It should take two command-line arguments.
- The first argument is a number
- The second argument is the filename
At the prompt:
- Type "javac Head.java"
- Type "java Head"
- Type "java Head Head.java
- Type "java Head 10 Head.java
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:
- Type "javac Dump.java"
- Type "java Dump test.out"
- Type "java Cat test.out"
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.
Record your file in the report.
Step 11 - More Commands
Here are a few more useful commands.
At the prompt:
- Type "grep println Dump.java" - What does grep seem to do?
- Type "ls | grep java" - What is the effect of using the vertical bar ("pipe") symbol?
- Type "ls > dir.out"
- Type "cat dir.out" - What is the effect of using the ">" symbol?
- Type "grep java < dir.out" - What is the effect of using the "<" symbol?
What to Hand In
Submit your document detailing the answers to the questions found in the
steps above.
Grading
- To earn a D, finish Step 1, 2 and 3
- To earn a C, finish the above and Step 4 and 5.
- To earn a B, finish the above and Step 6 and 7.
- To earn a A, finish the above and Step 8, 9 and 10.
- To earn a 100, finish the above and Step 11.
© Gabe Ferrer, Mark Goadrich, Hendrix College