Computer Lab Week 10 - UNIX 4


Home


** This tutorial contains Assessable Tutorial D **

 

1. Log onto and UNIX Workstation

2. Catchup

If you haven't finished your previous Unix tutorials do them now.

3. Change directory to your PE tutorials directory.

4. Creating a Java Program

Use the gedit editor to type the following program. The name of the file must be AddTwoNumbers.java. Specify the name of the file in the command.

Java code

5. Making Changes

Use gedit to add the following lines to the start of the program:

// Name: AddTwoNumbers.java
// Purpose: Input two numbers and display their sum
// Author: Tim Whitfort

Use gedit to change the line to display the sum, from:

    System.out.println(sum);

to:

    System.out.println("The sum is = " + sum);

6. Compile the program

To compile the program type the following command in a terminal window:

    javac AddTwoNumbers.java

If you have any syntax errors they will be displayed on the screen.

Use gedit to edit the program. Look at the listing above and try and fix the errors.

If you can't fix the problem see your tutor for help. You are not expected to know Java, so don't spend too much time before you get help.

7. Run the program

To run the program type the following command:

    java AddTwoNumbers

8. Unix Commands

You are required to demonstrate your understanding of some commonly used Unix commands.

Create a text file called q8Commands.txt. Each line of the file is to contain a command number and the UNIX command required to do the commands described below:

The start of the file q8Commands.txt should contain:

1. cd ~
2. pwd
3. ls
etc (you complete the rest)

Commands Required

Command
Number
Command Description
1 Ensure your current directory is your home directory
2 Display the name of your present working directory
3 List the files in your home directory
4 Change directory to your Programming Environment directory (this should already have been created)
5 Change directory to your tutorials directory (this should already have been created. If it doesn't exist create it))
6 Display the name of the present working directory
7 List the names of all the files in the current directory
8 Create a new directory called 'tut20' in the tutorials directory
9 Change directory to your new tut20 directory
10 Display the name of your present working directory
11 Copy the AddTwoNumbers.java file created earlier into the current directory
12 Print the above file to itlab3 (this won't work. write down what the command would be anyway)
13 Compile AddTwoNumbers.java
14 Run AddTwoNumbers
15 Copy all files from the /Users/cjcope/Public to your current directory using a relative reference to cjcope's public directory
16 Rename lecture2.txt to lecture3.txt
17 Display the permissions on the files in the current directory
18 Delete the other read permissions (mode) from all of the txt files in your current directory
19 Delete lecture3.txt
20 Display the permissions on the files in the current directory
21 Change directory to the parent directory (tutorials)
22 Move Hello.java from the tut20 directory to the current directory
23 Display the names of the files in the current directory
24 Display the name of the present working directory
25 Delete your tut20 directory (This may require a few of commands - be careful). Make sure the your q8Commands.txt file isn't in the directory before deleting it
26 Display the names of the files in the current directory

Print the file q8Commands.txt and hand it in stapled to a title page for assessable tutorial D. Alternatively you can show your tutor your commands in the Lab session in week 11.


9. Grep

Use grep to:

  1. find all occurrences of sum in AddTwoNumbers.java
  2. final all occurrences of int in AddTwoNumbers.java
  3. final all occurrences of int in AddTwoNumbers.java using a case insensitive comparison. Why are there more occurrences of int?
  4. count the occurrences of System in AddTwoNumbers.java
  5. find all occurrences of System in all java files in your tutorials directory
  6. find all occurrences of int num in all java files in your tutorials directory. What characters do you need to put around a search string with spaces in it?

10. Redirecting Output

  1. List all of the files in your home directory showing permissions, file size etc
  2. Rerun the same command, this time redirecting output to a file
  3. Display the contents of the file
  4. Use grep to find all occurrences of the word and in the file Unix.txt in tjwhitfort's Public directory
  5. Rerun the same command, this time piping the output to less.

11. Investigate Unix Further

If you have time investigate Unix further.

12. Work on your assignment

13. Log off


Assessable Tutorial D

Due Date: Computer Lab Week 11
Marks: 2%

Section 8 (Unix Commands) above is Assessable Tutorial D. Hand in a title page as specified in Assessable Tutorial A, and a printout of the file q8Commands.txt. Alternatively you can show your tutor your commands in Computer Lab week 11.


Written by Tim Whitfort.