Due: 5/8/2025 11:59pm
Exercise 2: Shell Tools and Scripting
In this exercise, you will develop a few essential bash scripts, which will help you to (semi-)automate testing of your kernel code and streamline your workflow of development, testing, and debugging. Before get started, please watch this lecture video and read the lecture node in The Missing Semester website.
Primer
- Re-familiarize yourself with Bash shortcuts to boost productivity: https://gist.github.com/memphys/2225770
Question 1 [7 points]
Write bash functions marco
and polo
that do the following. Whenever you execute marco
the current working directory should be saved in some manner, then when you execute polo
, no matter what directory you are in, polo
should cd
you back to the directory where you executed marco
. For ease of debugging you can write the code in a file marco.sh
and (re)load the definitions to your shell by executing source marco.sh
.
Upload a tarball of macro.sh
, polo.sh
, and the screenshot of demonstrating how your macro
and polo
work.
Question 2 [7 points]
Say you have a command that fails rarely. In order to debug it you need to capture its output but it can be time consuming to get a failure run. Write a bash script, names mytest.sh
, that runs the following script, named mycode.sh
until it fails and captures its standard output and error streams to files and prints everything at the end.
Upload a tarball of mytest.sh
, mycode.sh
, and the screenshot of demonstrating how mytest.sh
works.
#!/usr/bin/env bash
n=$(( RANDOM % 100 ))
if [[ n -eq 42 ]]; then
echo "Something went wrong"
>&2 echo "The error was using magic numbers"
exit 1
fi
echo "Everything went according to plan"
Question 3 [7 points]
find
’s -exec
can be very powerful for performing operations over the files we are searching for. However, what if we want to do something with all the files, like creating a zip file? As you have seen so far commands will take input from both arguments and STDIN
. When piping commands, we are connecting STDOUT
to STDIN
, but some commands like tar
take inputs from arguments. To bridge this disconnect there’s the xargs
command which will execute a command using STDIN
as arguments. For example ls | xargs rm
will delete the files in the current directory.
Your take is to write a command that recursively finds all assembly files (*.S
) in the Linux kernel source code directory and make a tarball with them. You should use find
, tar
, and xargs
and your command should work even if the files have spaces (hint: check -d
flag for xargs
). Please your tarball of *.S
files and your command script, named tarS.sh
, in a tarball. This tarball should have the tarball of *.S
and tarS.sh
.