Project 0a: 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 only cover several topics, but highly recommend you to play with it as much as possible.

"For the things we have to learn before we can do them, we learn by doing them."                                                 ― Aristotle, The Nicomachean Ethics

Overview

The project contains two subparts and two bonus problems. In part 1, you will write shell scripts to accomplish simple tasks. Understanding some basic shell commands (cd, cp, ...) and concepts (redirection, pipe ...) might be helpful. In part 2, you will practise with linux C programming environment (gcc compiler, gdb debugger, text editors). Finally, you can try some advanced text processing tools in bonus problems.

Readings

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 foo

The script first builds a new directory foo in the current directory (the one contains s1.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

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. Furthermore, - All file names in output should start with letter "b" (other files should be excluded) - The file output is sorted by the file name field (in order of alphabet, from small to large) - The file output is read only for other users

Here is an example output

bacman root -rwxr-xr-x
badblocks 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

We have a C program set_operation.c, which contains several implementation bugs. You will first try to compile/run the program, then detect and correct the bugs using the gdb debugger.

A correct (bug-free) output should be:

---------------------------------
----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

Bonus 1

Using awk to print the 10th line of a file. For example, assume that a file has the following content:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

Your script should output the tenth line, which is:

Line 10

Bonus 2

Given a text file, transpose its content. The file is in "column format", which contains a bunch of rows, each row has a fixed number of columns, and the columns are separated by the '   ' character. For example, if a file has the following content:

name age
alice 21
ryan 30

Your script should output:

name alice ryan
age 21 30

Hints

Part 1

Part 2

Hand In

General Advice

Try to use and get familiar with the shell commands first. Make good use of "man", "help" and "--help".

Start small, and get things working incrementally. For example, you can try to write some sub-functions in your script first, then test them before you combine them together. For debugging, you could build several test cases, and see why the running process of the program is different from your expectation.