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

Topic

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

Exercises

1.
Create a file script.sh which prints “Hello world!” to stdout. Solution.
2.
Run script.sh 1) as an executable file 2) as an argument to bash binary.
3.
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?
4.
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.
5.
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.
6.
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.
7.
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.
8.

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.

Solution.
9.

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
10

$ cat csv_file.csv
family_name,first_name,age,points,email
Doe,Joe,22,1,joe_doe@some_mail.com
Fog,Willy,38,8,ab@some_mail.com
Zdepa,Pepa,10,1,pepa@some_mail.com
Solution.