B09 Lab week 2

It can be quite overwhelming to use a Unix-like computing environment (especially the text-mode command prompt) for the first time. This lab may help you start.

We begin with some tips for beginners. If you are already familiar with Linux, you may skip right to section The Exercises (but download and save the starter files first).

Viewers and Editors

To view a short text file: ‘cat filename

To view a long text file: ‘less filename’. Here are some of its keyboard commands:

action key
quit q
scroll Down, Up, PgDn, PgUp

Simple editor in a terminal: ‘nano filename’. It lists some commands at the bottom, using ^ to mean the Ctrl key, e.g., ^X means Ctrl-X and is how to exit.

Simple GUI editor on Ubuntu Desktop: ‘gedit filename’. You can also launch it by: Press the Windows key, enter “gedit”, then click the obvious icon.

In the long term, you should invest time in learning a sophisticated editor geared towards programming. Popular choices: VS Code, Vim, Emacs, Spacemacs, Atom, Sublime Text, Notepad++.

Survival Tips

Ctrl-C can abort most commands in terminals. Great for accidental wrong commands that hang and you need your command prompt back.

Most commands recognize the ‘--help’ argument and output a summary of how to use them, e.g., ‘ls --help’.

Most also have longer docs under the ‘man’ command (short for “manual”), e.g., ‘man ls’. The UI is similar to that of ‘less’.

Exercise: The “summary” from ‘ls --help’ is pretty long. Can you add ‘less’ and pipelining to display it one screenful at a time? Additional information you need: ‘less’ is happy to read from stdin if you don't provide a filename.

The Exercises

(Starter files are also available on mathlab in directory /courses/courses/cscb09s23/laialber/l02 )

These exercises introduce some Unix text-processing utilities and train you to pick out specific information from Unix-style documentation.

Each exercise tells you the program name to use and where to find its documentation. Your job is to find out from the documentation how to craft the arguments to achieve what the exercise requires. The textbook The Linux Command Line also helps. The first exercise is solved with explanation.

Each exercise specifies where to input from and output to, which can be files, and/or standard input (stdin), and/or standard output (stdout). As discussed in lecture, you do not always know that stdin and stdout mean the terminal, and it should not matter, and you already know how to test your solution against both terminal and file redirection.

(But reminders: When stdin is terminal and you enter input by hand, use Ctrl-D on its own line to signify end of input. File redirection for stdin uses the notation “< filename”. File redirection for stdout uses the notation “> filename”.)

This lab can take longer time than the tutorial session; don't feel bad if you have to continue after class. This is an exception.

0. ls

Use ls to list the non-dot names in the current directory, in order from oldest to newest.

Documentation: ‘ls --help’ or ‘man ls’ or link.

How I solved it: In any of those docs, I noticed:

“Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).”

That's perfect, I can get the current directory by doing nothing for the FILE part.

Now the options. I noticed:

-a, --all do not ignore entries starting with .

I do want to ignore dot files, so I should absolutely avoid that option.

Then I noticed:

-t sort by time, newest first

and I also noticed:

-r, --reverse reverse order while sorting

So if I use them both, I'll get oldest to newest.

My solution: ls -t -r

1. Dealing with a naughty filename

I have a file but it has a naughty filename: “--help”. Use mv to rename it to “help.txt”.

Documentation: In this case the most important hint is somewhere in the Common Options section.

2. cat

Use cat to send the concatenation of these inputs to standard output: the file cat-in-1.txt, then standard input, then the file cat-in-2.txt, in that order. (Verbatim, so for example don't add line numbers, don't display $ at end of each line, etc.)

Documentation: ‘cat --help’ or ‘man cat’ or link.

3. head

Use head to output everything except the last line of the file headtail-in.txt. Output to standard output.

Documentation: ‘head --help’ or ‘man head’ or link.

4. tail

Use tail to output the last line of the file headtail-in.txt. Output to standard output.

Documentation: ‘tail --help’ or ‘man tail’ or link.

5. wc

Use wc to output the number of lines in the file headtail-in.txt. Output to standard output. Don't output other counts, e.g., don't output the number of words.

Documentation: ‘wc --help’ or ‘man wc’ or link.

6. tr

Use tr just once to perform ROT13 decryption (Wikipedia entry). Input from standard input; output to standard output. Don't forget that English letters include both uppercase and lowercase.

Documentation: ‘tr --help’ or ‘man tr’ or link.

ROT13 is great for posting spoilers without spoiling. Here is an encrypted spoiler for the 2008 Iron Man movie: Veba Zna vf Gbal Fgnex.

(Food for thought, no need to hand in: Why does the same command do encryption too?)

7. tr but for deletion

Use tr just once to delete all occurences of the characters ‘2’ and ‘u’ from the input. Input from standard input; output to standard output.

Documentation: ‘tr --help’ or ‘man tr’ or link.

8. tee

Use tee to copy standard input to both standard output and the file tee-out.txt. (Overwrite tee-out.txt, do not append.)

Documentation: ‘tee --help’ or ‘man tee’ or link.

Trivia: This program is called “tee” because: Think of T-shaped pipes in plumbing, and “T” is pronounced as “tee”.

9. yes

Use a pipeline consisting of yes and head to output exactly 42 lines of “nonono” to standard output.

You must use a pipeline (the “|” thing as seen in the lecture), not the “<(yes ...)” feature you might find on the Internet. This exercise requires pipelining.

yes documentation: ‘yes --help’ or ‘man yes’ or link.

head documentation: ‘head --help’ or ‘man head’ or link.

10. comm

Use comm to output lines that appear in menu-new.txt but not in menu-old.txt. We assume that the two input files are already sorted.

Documentation: ‘comm --help’ or ‘man comm’ or link.

What to Submit

Please submit a single file utils.txt on MarkUs. The starter files include a copy that exemplifies the expected format, with blanks for you to fill in your solution.

Format: Each line begins with an exercise number (e.g., 0 for the ls exercise, 2 for the cat exercise) (optionally surrounded by spaces), then a colon (optionally surrounded by spaces), then your solution to that exercise (leave it blank if you can't solve it). For every exercise there is exactly one line. The lines should be in increasing order of exercise numbers. Please don't include multiple solutions.

This lab may be automarked or marked by humans.