Week 05 Weekly Test Questions
Test Conditions
· These questions must be completed under self-administered exam-like conditions.
· You must time the test yourself and ensure youcomply with the conditions below.
· You may complete this test in CSE labs or elsewhere using your own machine.
· You may complete this test at any time before
· Week 7 Thursday 18:00:00
· Weekly tests are designed to act like a past paper - to give you an idea of how well you are progressing in the course, and whatyou need to work on. Many of the questions in weekly tests are from past final exams.
· Once the first hour has finished, you must submit all questions you've worked on.
· You should then take note of how far you got, which parts you didn't understand.
· You may choose then to keep working and submit test question anytime up to Week 7 Thursday 18:00:00
· However the maximum mark for any question you submit after the first hour will be 50%
Getting Started
Set up for the test bcreating a new directory called test05 and changing to this directory.
1 $ mkdir test05
2 $ cd test05
There are some provided files for this test which you can fetch with this command:
3 $ 2041 fetch test05
If you're not working at CSE, you can download the provided files as a zip fileor a tar file
WEEKLY TEST QUESTION:
Hello Files
Write a POSIX-compatible shell script, hello_files.sh which takes 2 arguments.
The first argument will be positive integer,n.
The second argument will be a string,name.
Your program should create n files.
The names of these files should be hello1.txt ..hello n.txt.
Each file should have the same contents, a single line: hello name
For example:
1 $ ls hello*.txt
2 ls: cannot access 'hello*.txt': No such file or directory
3 $ ./hello_files.sh 3 Andrew
4 $ ls hello*.txt
5 hello1.txt hello2.txt hello3.txt
6 $ wc hello*.txt
7 1 2 13 hello1.txt
8 1 2 13 hello2.txt
9 1 2 13 hello3.txt
10 3 6 39 total
11 $ cat hello1.txt
12 hello Andrew
13 $ cat hello2.txt
14 hello Andrew
15 $ cat hello3.txt
16 hello Andrew
17 $ ./hello_files.sh 100 Brittany
18 $ ls hello*.txt|wc -l
19 100
20 $ cat hello100.txt
21 hello Brittany
22 $ cat hello42.txt
23 hello Brittany
24 $ cat hello1.txt
25 hello Brittany
You can assume the files do not exist already.
WEEKLY TEST QUESTION:
Print the file with most lines
We what to know which of a set of files has the most lines.
Write a POSIX-compatible shell script most_lines.shwhich given one of more filenames as argument, prints which file has themost lines.
For example
1 $ seq 1 5 >five_lines.txt
2 $ cat five_lines.txt
3 1
4 2
5 3
6 4
7 5
8 $ seq 1 10 >ten_lines.txt
9 $ seq 1 100 >hundred_lines.txt
10 $ ./most_lines.sh ten_lines.txt hundred_lines.txt five_lines.txt
11 hundred_lines.txt
WEEKLY TEST QUESTION:
List Identical Files in Shell
Write a POSIX-compatible shell script,ls_identical.sh
which takes the pathnames of 2 directories as argument.
It should print in alphabetical order the names of all files which occur in both directories,
and have exactly the same contents in bothdirectories.
Files must have the same name in both directories and the same contents for their name to be printed.
Do not print the names of files with same contents but different names in both directories.
For example:
1 $unzip directory.zip
2 Archive: directory.zip
3 creating: directory1/
4 creating: directory2/
5 $ls_identical.sh directory1 directory2
6 empty.txt
7 same.txt