# Project 0: Linux Warm Up

## Objective

This project is to help you getting familiar with Linux, which is, in fact, the operating system in the course title Operating System Labs. We will only cover two topics here, but we suggest that you play with it as much as you can. It is really fun and exciting. Although you will be stuck with this black annoying system at some point (almost surely), you can definitely learn something from this great software.

## Overview

We first give you two simple tasks, and you will try to accomplish them with a few lines of shell scripts. Understanding some basic commands (cd, cp, ...) and concepts (redirection, pipe ...) is important. In the second part, you will practise with the C programming environment (gcc compiler, gdb debugger), which will be your best friends in the rest of the semester.

OSTEP Lab Tutorial

## Program Specifications

### Part 1

You need to write two shell scripts, both of them should be executable in a Linux shell.

• s1.sh:

% ./s1.sh foo

The script first builds a new directory foo in the current directory (the one contains sh1.sh), then creates two files: "name.txt" containing your name; "stno.txt" containing your student number. Finally, it makes a copy of "name.txt" and "stno.txt" in the same directory of foo.

• s2.sh:

% ./s2.sh
It generates a text file output. Each row of output represents a file in directory /bin, and contains three fields which are name, owner and permission of that file. The fields should be separated by white spaces. We further require that
• All file names in output should start with letter "b" (other file names should be excluded)
• The file output is sorted by the file name field (in order of alphabet, from small to large)
• File output is read only for other users

The following is an example of output

bacman root -rwxr-xr-x
baobab root -rwxr-xr-x
base64 root -rwxr-xr-x
basename root -rwxr-xr-x
bash root -rwxr-xr-x
bashbug root -r-xr-xr-x
bayes.rb root -rwxr-xr-x
bbox root -rwxr-xr-x

### Part 2

You need to compile/run a C program set_operation.c. The program contains several simple bugs, and you are going to detect and correct them using the gdb debugger.

Here are some details of the set_operation.c:

• It is designed for doing linked list operation to compute $$(A-B)\cup(B-A)$$, where $$A$$ and $$B$$ are two sets input by users.
• The logic is very simple:
• copy $$A$$ to a temporary set $$A_2$$
• compute $$A=A-B$$ and $$B=B-A_2$$
• union $$A$$ and $$B$$
• There are two sub-functions: output and check
• "output" is used to output all elements in a linked list
• "check" is used to check if an integer belongs to a linked list

The program after being corrected should run as follows:

---------------------------------
----Computing (A-B)union(B-A)----
---------------------------------
----input the number of elements of A: 3
1-th element: 3
2-th element: 4
3-th element: 5
----input the number of elements of B: 2
1-th element: 1
2-th element: 4
---- elements of (A-B)union(B-A) ----
1-th element: 3
2-th element: 5
3-th element: 1

## Hints

### Part 1

• In the first script, you can use echo and redirection to output some texts into a file.
• In the second script, you may use xarg (between find and ls) or pipe.
• For processing the text file, you can write a loop to read each line in and process them one by one. But, using awk maybe more convenient.
• To sort the data , you may use the command sort.

When you need some details of a command, use man. Output some informations properly when scripts are running. For example, for the second script, show the number of the instances of the file when it is created.

### Part 2

• Set some breakpoints, especially at some important statements(for example, loop or logical control)
• Display some value of variables, such as the loop variables or the number in the linked list which is pointed at by the pointer currently.

## Hand In

• two script files named s1.sh and s2.sh.
• source file after debugging and a lab report contains three parts:
• A simple introduction of your project
• Summarize the commands and their usages, which were used in compiling and debugging.
• How you debugged the program (where are your breakpoints located, which variables you've watched, etc.) and the bugs you found.