Other labs: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.

Přeložit do češtiny pomocí Google Translate ...

Lab #3 (Mar 2 – Mar 6)

Before class

  • What is the difference between a scripting language and a compiled language?
  • What is a regular expression (regex/regexp)? What is the difference between regex and a shell wildcard (glob)?
  • What different shells are there?
  • What is a variable in shell? How do you assign a value to the variable? How would you retrieve a value from the variable?
  • What is the alias command good for?
  • What is the content of $PS1 variable?
  • What is the variable $PATH used for?
  • What are the differences between running cat, ./cat and /bin/cat
  • What is shebang (also hashbang)?


  • Shell scripting.
  • Environment variables.
  • Shell customization and .bashrc.


Create a file script.sh which prints “Hello world!” to stdout. Solution.
Run script.sh 1) as an executable file 2) as an argument to bash binary.
Add proper shebang for bash. What happens if 1) bash is not installed on the system and 2) bash is installed in different path? What can we do to minimize this type of problems?
What happens and why if you change the shebang to #!/bin/echo? Try again running script.sh as an executable and as an argument to bash.
Check what set -euo pipefail does and why it can be useful? Give and try an example, where script with set -e will die as soon as an error is detected.
Write fact.sh with a function which computes factorial of given number. Create two variants: 1) Read input from stdin. 2) Read input from the first argument ($1). Solution.
Write max.sh which prints the maximum of given numbers. Write two variants: 1) Read numbers from stdin. 2) Read numbers from command line. Solution.

Write bar_plot.sh which prints horizontal bar plot. Input numbers indicate bar height. Decide what input option is more viable for you. Example:

$ ./bar_plots.sh 7 1 5
7: #######
1: #
5: #####

If the largest value is greater than 60 rescale the whole plot. The maximum number of # is 60.


Write csv_sum.sh that reads CSV file on stdin. Sum all the numbers in the given column. The column name is specified as a first argument. Example:

$ ./csv_sum.sh points < csv_file.csv

$ cat csv_file.csv