Intro to Unix lab (summer 2018)

  • Time and place: Wednesday 12:20, SU2 
  • Consultations arranged via email (varis(at)ufal.mff.cuni.cz)

Course Requirements

To get a credit, you need to complete the homeworks:

  • You'll need at least 2/3 of all points that will awarded from homeworks. There will be a set of homeworks together worth 3 points per lab.
  • The deadline for submission will usually be one week from the homework announcement. Solutions submitted after the deadline will receive only half of the points (see "Homework submission" for more details).
  • If you aren't able to get enough points but still manage to get at least 1/3 of the required points from homeworks alone you can still get the credit by submitting additional homework (after consultation).
  • Cheating is not tolerated! Plagiarism will be awarded by zero points, repeated plagiarism by failing the lab.

The attendance is not mandatory, however, it is recommended. There will be hints to the homeworks, anwers to questions about the lecture content, etc.

For information specific to the Unix lab in Rotunda, check this link.

For interesting links related to Unix, I recommend checking out course pages of Petr Kučera or Ondřej Plátek.

Homework submission

Each week there will be a homework containing one or more tasks. A solution for each task can earn you points.

The solutions to the homeworks can be submitted via SIS (Study Group Roster / Studijní mezivýsledky). You must submit a .zip archive containing the solutions, each solution being in a separate file. Deadline for submission is a day before the next practicals. You can also send me a solution after deadline via email, however, you can only earn half of the points this way.

The solutions (if not stated otherwise) should be compatible with standard POSIX shell (/bin/sh). Check common differences between shell extensions here. I recommend you to always run "sh your_script.sh" to check whether your solution works correctly. You can also use the script checker.

Lab Content Summary

Feb 21

  • Information on lab requirements
  • Getting familiar with Unix eviromnent and commands.
  • Different types of shell, remote login,
  • Keyboard shortcuts: Ctrl-a, Ctrl-e, Ctrl-r, Ctrl-d, Ctrl-k, Ctrl-l ...
  • Commands: man, ls, cd, pwd, mkdir, cp, mv, rm, touch, cat, head, tail, touch
  • Cmd-line shortcuts: ~, !!
  • Redirection: >, >>, <, <<, |, /dev/null
  • Expansions: *, ?, [list], [!list]
  • Tasks for the lab (solutions)

Feb 28

Mar 07

Mar 14

Mar 21

Mar 28

Apr 04

Apr 11

Apr 18

Apr 25

May 2

May 9

May 23

Homeworks

Feb 28

HW01:

  1. (1p) Write a command that prints the second oldest file in a directory
  2. (1p) Create a file containing numbers from 1 to 100, one number on each line (use the 'seq' command), print only lines containing numbers from 24 to 42
  3. (1p) Count the number of hidden files/directories in the current directory (try your home directory) excluding "." and ".."

Mar 07

HW02:

  1. (1p) Write a command which sets a last modification time of a file to 9th November 2001, 11:55. If the file does not exist, do not create the file!
  2. (1p) Write a command that lists all directories (in the current directory) that have name containing a number but not starting with a number.
  3. (1p) Let's say that the solutions for this batch of homeworks are stored in files hw2-1.sh, hw2-2.sh. Write command, that sends me these solutions via email (Concatenated in the body of the email). (Note: Submit all your solutions the usual way via SIS, don't send me the email.)

Mar 14

HW03:

  1. (1p) Set your default file creation mode mask so that each file you create has automatically read/write permission for owner, is readable by its group, and is inaccessible by other users.
  2. (1p) Write a command that removes a read permission from other users for a directory and for its directory subtree (files and directories).
  3. (1p) Set a read/write/execute permissions for owner read/execute for group and read-only for other to a file, via octal numeric way (0,5p) and via symbolic way (0,5p).

Mar 21

HW04:

  1. (1p) Count the number of different login-shells that are listed in "/etc/passwd".
  2. (1p) Replace the GID values in "/etc/group" by the "-". Leave the rest unchanged.
  3. (1p) You have a file corpus-small.txt containing English sentences and their translations, each on separate line, each sentence pair separated by an empty line.
    Transform the file, so that on each line, it contains both the English sentence and its translation, separated by a tabulator (default paste field separator)
    The resulting file should look like this.

Mar 28

HW05:

  1. (1p) Using the files countrycodes_en.csv and kodyzemi_cz.csv, print the countries that have identical names in Czech and in English.
  2. (1p) Write a command that prints all files in the root directory tree that are executable by all users and are not owned by root.
  3. (1p) Sort the /etc/group file by the number of users in the group (list in the file) decreasingly. When the number of users is equal, sort the sublist by the group name.
    (Hint: If you replace each user by a special symbol, you can use the lenght of the resulting string to sort them alphabetically.)

Apr 04

HW06:

  1. (1p) Write a command which finds every file ending with ".txt" in the directory subtree and for each file creates a copy ending with ".txt.bak" located in the same directory as the original file.
  2. (1p) For every file in /usr/include with filename ending in ".h", print its file name and all lines in the file that start with "#include" (Use grep to gather the lines.).
  3. (1p) Write a command that deletes all softlinks from the /tmp directory subtree that do not point to an existing file. (hint: you can use "cat" on the file to detect whether it exists.)

Apr 11

HW07:

  1. (1p) Output a list of files from /usr/bin that are shell scripts (hint: use "file" command to get the file type; output of "file" is "filename: description"; shell scripts are described as "POSIX shell script").
  2. (1p) Create a script that takes an input text and separates each symbol by a whitespace (preserve the original whitespaces by a special character) (0,5p). Then create a script that reconstructs the original text (0,5p).
  3. (1p) Put a "#" symbol in front of every line from /etc/passwd, where UID=GID. (hint: grepping such lines can help you create the necessary regex).

Apr 18

HW08:

  1. (1p) Write a script in sed that modifies an input stream (containing only alphanumeric characters) by reversing the sequence of characters on each line (e.g. "abcdef" to "fedcba") .(Hint: use conditioned branching to modify whole line multiple times.)
  2. (1p) Write a shell script that takes n+1 parameters, first parameter is expr operator (escaped) the rest is sequence of numbers on which the operator is subsequentially applied (e.g. "./script.sh '+' 1 2 3" prints 6).
  3. (1p) Write a shell script that prints its parameters each on a separate line sorted alphabetically (ascending).

Apr 25

HW09:

(Make sure that your solutions also check the parameters (whether they are correct, whether there is correct number of parameters etc.). Solutions that do not check the parameters correctly will receive only half of the points.)

  1. (1p) Write a script which works as cp but with reversed order of parameters (the target is the first parameter).
  2. (2p) Write a script which expects three parameters (in this order): file_name, number 'n' and a character 'c'. The script then performs a circular right shift of 'n' fileds, the fields are separated by the 'c' (input is in the file specified by the first parameter). Output is written to STDOUT. Example

May 2

HW10:

  1. (1p) Write the script similar to the task 3) from the lab10 but for each target, that is older than some of its dependencies, print also, which dependencies are newer than the target in a format similar to input (target : z1 z2 .. zN)
  2. (2p) Write a script that takes two number parameters: month and year, passes them to the "cal" command and prints the output according to the following example.

May 9

HW11:

  1. (1p) Assume that the input lines contain only characters '(' and ')', write a sed script which checks whether each input line contains string of well paired brackets. The correct lines are replaced with 'ok' string, the wrong lines with 'wrong' string. (e.g. string '(())()((()))' would be 'ok', while string '(((())()' would be 'wrong').
  2. (2p) Write a sed script that takes an input containing paragraphs that are separated by an empty line (each paragraph can be on multiple lines) and outputs each paragraph to a single line (the empty lines are removed in the process).

May 23

HW12:

  1. (3p) Write an awk script which expects a file in the csv format at the input. This means that each line of the input line consists of several fields separated with a comma ",". The first line is the header line, it contains the column names. The first column of the subsequent lines contains a name, the rest of columns contain numbers. The script adds a new column called “Sum” (in the header line), which on each line contains the sum of the number fields.