Skip to content
devtools-coursePublic

About

Devtools. Task bash

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

Repository files navigation

Task Bash

Branch: bash
Fenrir ID: bash
Points: 100

Your solution must be located at bash:

[folder] ...
├─ [folder] bash
│  ├─ [file text] column.sh
│  ├─ [file text] line-counter.sh
│  ├─ [file text] prod.sh
│  ├─ [file text] stackd.sh
│  ├─ [file text] sym-diff.sh
│  └─ [file text] wrapper.sh
└─ [folder] .git

Dot product (5 points)

File: prod.sh

You have to calculate the dot product of two vectors.

The input (stdin) consists of two lines (two vectors). Both vectors must contain only integers, a space character is used as a separator.

Example

// Stdin:
// 1 2 3 4
// 1 1 1 1
$ ./prod.sh
10

Error handling

When exiting due to an error, you must provide a description. The description must be clear and concise.

Non-numeric symbols

Exit code: 11

Mismatched number of elements

Exit code: 10

Questions

  1. How to read a line from stdin and split it?

  2. How to test whether a string is a number?

Directory stack (10 points)

File: stackd.sh

You have to reimplement pushd-popd-dirs utils in bash. The script must provide a function stackd which can be used to replace pushd-popd-dirs.

You CANNOT use any of those commands! To switch directories use cd.

Synopsis

stackd CMD

CMD:
    init          Initialise the directory stack with PWD as the first element

    list          Display the entire stack (dirs).
                  Example: 'top middle bottom'

    pop           Pop a dir from the stack and switch to the new top directory.
                  The second directory must be displayed (popd)

    push DIR      Push DIR onto the stack and switch to it (pushd)

Error handling

When exiting due to an error, you must provide a description. The description must be clear and concise.

Invalid number of arguments
  • push: exit code 11

  • pop: exit code 21

  • list: exit code 31

  • init: exit code 41

Invalid CMD

Exit code: 6

DIR in push is not a directory

Exit code: 12

Not enough elements to pop

Exit code: 22

The directory does not exist when pop

Exit code: 23

Questions

  1. Why do we use different exit codes?

  2. How to store the stack? You must choose the most optimal way!

  3. Why is your script executed with source? Try executing a script, which runs cd directly, and see the PWD afterwards. Why does it happen?

Column (15 points)

File: column.sh

You have to reimplement column util in bash.

You CANNOT use column in your implementation!

Synopsis

column.sh [-s SEPARATOR]

OPTIONS:
    -s SEPARATOR      The character used to split the rows.
                      The default value is ' ' (a space)

The semantics of your script must be the same as that of column -s SEPARATOR -t from utils-linux, except for the last column: it must not be followed by spaces.

Example

// Stdin:
// aq,s,fp
// e,rtu,t
$ ./column.sh -s ','
aq  s    fp
e   rtu  t

Error handling

When exiting due to an error, you must provide a description. The description must be clear and concise.

Invalid option

Exit code: 1

Missing the value for -s

Exit code: 2

Non-matching number of elements

Exit code: 3

Questions

  1. How to print something to stderr?

  2. How to split data by a separator without reading the string char-by-char?

  3. How to parse the options with default values?

Symmetric difference (15 points)

File: sym-diff.sh

You have to provide a function sym-diff that takes two arguments (the names of two arrays) and calculates their symmetric difference.

Each array represents a set of numbers so its element uniqueness is guaranteed.

The result (stdout) must be sorted in descending order.

Important
You cannot access arrays elements directly, use other commands that manipulate the data to achieve the goal. You are allowed only to use ${_[@]} at the beginning of your function to retrieve the data.
Tip
The task can be solved with a single pipeline (…​ | …​)

Example

ar=(1 2)
ra=(1 3)

val=()
while read -r line; do val+=("$line"); done < <(sym-diff ar ra)

val is expected to be equal to (3 2)

Error handling

No error handling is required

Questions

  1. How to find all unique numbers? How to remove all other numbers?

  2. How to sort numbers?

Wrapper (25 points)

File: wrapper.sh

Note
Spoiler alert! You are going to find and study some new information yourself during this task.

Create a custom wrapper script which can be used as a shebang.

What your wrapper must do

Handle SIGTERM signal

Print No no no, mr fish! to stdout and log SIGTERM detected in WARN mode. You must prevent the script from termination.

Track the inner script execution
  • Log each command that is executed in DEBUG mode

  • When the command is echo, it must be logged in ERROR mode and the wrapper must prevent echo from being executed.

Logging

Log format: MODE TIMESTAMP MESSAGE

  • MODE - find some information about log modes yourself

  • TIMESTAMP - the timestamp of the log. Format: YYYY-MM-DD HH:MM:SS

All logs must be written to the file $LOG_FILE (passed as an env variable)

Example

Inner script:

#! wrapper.sh

echo 3 4 5 6
ls -l | grep total | cut -f1 -d' '

stdout: total

stderr:

$LOG_FILE:

ERROR 2026-02-26 11:14:51 echo 3 4 5 6
DEBUG 2026-02-26 11:14:51 ls -l
DEBUG 2026-02-26 11:14:51 grep total
DEBUG 2026-02-26 11:14:51 cut -f1 -d' '
Caution
Tests might fail if you run them at 23:59:_ (The date might change)
Caution
Use fenrir rund if your system’s language is not English!
Tip
Read about special signals in bash for tracking the execution.
Tip
You might need to use set and shopt commands to set up the execution.

Questions

  1. How to handle signals in bash?

  2. How to get current datetime in bash?

Line counter (30 points)

File: line-counter.sh

You have to implement a script which calculates the LOC metric of a project. (LOC is Lines Of Code)

Synopsis

line-counter.sh PROJECT_ROOT [OPTIONS]...

OPTIONS:
    -i, --include FILE      Calculate LOC only for the files whose extensions are
                            declared in FILE. FILE must follow the CSV format

    -e, --exclude FILE      Exclude files whose extension are declared in FILE.
                            FILE must follow the CSV format

When both options are used, -e excludes files from the set selected by -i.

Since the CSV format allows different separators, your script must automatically detect which one is used. It is guaranteed that the extensions consist of alphanumeric characters and maybe dots or spaces.

Example

$ ./line-counter.sh /tmp/testdir -i a.csv
12

a.csv:

cpp,cc,cxx
hpp,hh,hxx

Error handling

No error handling is required

Questions

  1. What is the CSV format?

  2. How to get a file extension?

  3. How to safely pass data in a pipeline (…​ | …​)?

  4. Why is \n important at the end of a file?

The project is licensed under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

© 2026-present Iwan Kalinin <koefic.cien@gmail.com>

About

Devtools. Task bash

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

Contributors

Languages