Week 05 Tutorial Sample Answers
Week 05 Tutorial Sample Answers
1. The assignment specification doesn't fully explain the assignment - what can I do?
ANSWER:
You can also use the the reference implementation 2041 tigger to discover what your program is supposed to do
in any situation.
ANSWER:
Once you understand what you have to dom subset 0 is not that hard.
But note the marking scheme recognizes the difficulty of subsets 1 & 2.
ANSWER:
git init creates many files and sub-directories inside the directory .git
ANSWER:
5. What is the index in tigger (and git), and where does it get stored?
ANSWER:
Files get added to the repositoy via the index so its somethimes called a staging area.
You might create a directory .tigger/index/ and store the files there.
6. What is a commit in tigger (and git), and where does it get stored?
ANSWER:
ANSWER:
# ==============================================================================
# test00.sh
# Test the tigger-add command.
#
# Written by: Dylan Brotherston <[email protected]>
# Date: 2022-06-20
# For COMP2041/9044 Assignment 1
# ==============================================================================
PATH="$PATH:$(pwd)"
expected_output="$(mktemp)"
actual_output="$(mktemp)"
trap 'rm "$expected_output" "$actual_output" -rf "$test_dir"' INT HUP QUIT TERM EXIT
# Check that the file that has been commited hasn't been updated
# Check that the file that is in the staging area hasn't been updated
ANSWER:
Part of the assignment requirements is that you must submit intermediate versions of your code.
9. Write a shell script extract.sh that, when given one or more archive files as command line arguments, will use the correct
program to extract the files.
ANSWER:
#! /usr/bin/env dash
case $# in
0)
echo "Usage: $0 <file> [<file> ...]"
exit 2
;;
esac
status=0
case "$archive" in
*.tar.bz2) tar xjf "$archive" ;;
*.tar.gz) tar xzf "$archive" ;;
*.tar.xz) tar xJf "$archive" ;;
*.bz2) bunzip2 "$archive" ;;
*.rar) rar x "$archive" ;;
*.gz) gunzip "$archive" ;;
*.tar) tar xf "$archive" ;;
*.tbz2) tar xjf "$archive" ;;
*.tgz) tar xzf "$archive" ;;
*.zip) unzip "$archive" ;;
*.jar) unzip "$archive" ;;
*.Z) uncompress "$archive" ;;
*.7z) 7z x "$archive" ;;
*)
echo "$0: error: '$archive' cannot be extracted" >&2
status=1
;;
esac
done
exit $status
Write a shell script last.sh that, using shell case statments, finds the number of loggins that occurred from within UNSW.
(Look for connections to from the uniwide network)
ANSWER:
#! /usr/bin/env dash
counter=0
z0=0
z1=0
z2=0
z3=0
z4=0
z5=0
z6=0
z7=0
z8=0
z9=0
class=0
11. Write a shell function top_and_bottom that, given a file name, prints the file name, plus the first and last lines of the file.
$ . top-and-bottom.sh
$ top-and-bottom /usr/share/dict/british-english-insane
=================
/usr/share/dict/british-english-insane
-----------------
A
événements
=================
ANSWER:
#! /usr/bin/env dash
top_and_bottom() {
echo "================="
echo "$1"
echo "-----------------"
sed -n '1p;$p' "$1"
echo "================="
}
12. Write a shell function print_message that, given an optional exit status and a message:
ANSWER:
#! /usr/bin/dash
print_message() {
if [ $# -gt 1 ]; then
echo "$0: error: $2"
exit $1
else
echo "$0: warning: $1"
fi
}
13. Create a git repository called cs2041-Labs and add you week01 and week02 lab work.
ANSWER:
# `master` is the most common default branch name, but you can use any name
$ git config --global init.defaultBranch master
$ git branch -m master
$ cp ../week01/*_answers.txt ../week02/*_answers.txt .
$ git add .
Now create an SSH key so that you can connect to the CSE gitlab servers.
$ mkdir -p ~/.ssh
$ cd ~/.ssh
$ ssh-keygen -t ed25519
# follow the instructions given
$ cat id_ed25519.pub
# copy the public key
# Don't show anyone your private key
$ eval `ssh-agent`
$ ssh-add id_ed25519
Refresh the GitLab page and you should see your files.
14. There is a git repository located on the CSE gitlab servers at https://gitlab.cse.unsw.edu.au/cs2041/22t2-tut05
Clone this repository to your local machine.
ANSWER: