COSC 224
CHUKA UNIVERSITY
UNIVERSITY EXAMINATIONS
EXAMINATION FOR THE AWARD OF
DEGREE OF BACHELOR OF SCIENCE IN COMPUTER SCIENCE
COSC 224: UNIX OS AND PROGRAMMING
STREAMS: Y2S2
TIME: 2 HOURS
DAY/DATE: TUESDAY 6 /07/ 2021 2.30 PM – 4.30 PM
INSTRUCTIONS:
Answer Question One and any other two questions.
Question One (Compulsory) (30 marks)
(a) Explain what the commands shown below will do when executed.
(i) cat /etc/passwd | sort (2 marks)
(ii) sudo adduser fred (2 marks)
(b) Write Unix commands for doing the following: (4 marks)
Capture at least five names of cities in Kenya from the keyboard. This list should be
combined with the list present in a file “[Link]”. From the combined list sort the city
names in ascending order and store the sorted list in the file “[Link]”.
(c) Write a shell script to rename all file names in the current working directory that end with .txt
to end with .c (4 marks)
(d) Write a perl script to search all usernames in the home directory that begin with letter c sort
them and write them in a file “[Link]”. (4 marks)
(e) Explain the two modes of the Vi editor in Unix. Show how a user is able to switch from one
mode to another. (4 marks)
Page 1 of 4
COSC 224
(f) State the role of the following commands in UNIX. Use an example to illustrate the
commands usage. (4 marks)
(i) cd
(ii) sudo passwd fred
(iii) ls
(iv) adduser
(g) Explain the three types of file permissions in UNIX. Give at least one example of file
permission display on how they are arranged in UNIX. (6 marks)
Question Two (20 marks)
(a) Write a Unix perl script to find all the C program files in the directory
“/usr/fred/programfiles”and move them to the directory “/usr/fred/cstuff”. (6 marks)
(b) Write a shell script for searching files in a given directory to find which files contain the
string “unix revised server IPs”. (6 marks)
(c) Assuming that a file’s current permissions are r-xr-xr--, using both relative and absolute
permissions, specify the chmod expression required to change them to:
(8
marks)
(i) rw-r--r-x
(ii) rwxrw-r--
(iii) ---r--r--
(iv) --x----w-
Question Three (20 marks)
(a) Explain the difference between the commands cd *backup and cd ~/backup. Is it possible for
both commands to work? (4 marks)
(b) Write a perl program that populates an array named weekday from the string
SunMonTueWedThuFriSat, and then prints each day in uppercase. (8 marks)
(c) Write a shell script that prompts for a string and then lists all the files with that string value
from the directory /home/users/config (8 marks)
Page 2 of 4
COSC 224
Question Four [20 marks]
(a) What does the command “ $who | sort –logfile > newfile” do? (4 marks)
(b) Write a perl script for creating a backup for every file in the current working directory.
Every backup file shall be named filename_backup where “filename” is the file name that
you are backing up. (8 marks)
(c) Write a perl program that prompts a user to repeatedly enter an integer number. When the
user enters 0, the program should print the average and the total. The program should
capture all the numbers entered and their total in a file “[Link]”.
(8
marks)
Question Five (20 marks)
(a) Identify any FOUR syntactical errors in the shell program shown below: (4 marks)
1. #!/bin/sh
2. prunning=yes
3. echo "Enter value of index [value 1, 2, or 3]:"
4. read index
5. while $prunning = yes ; do
6. $index + 1
7. sleep $index
8. if [ $index => 20 ] ; then
9. break
10. done # endwhile
11. eched "Job done"
(b) Explain what happens when a user is added to a Unix system in terms of files/directories
that have to be created or modified. Give examples and explanations of the commands that
are used to make these changes (e.g. chown, chgrp, chmod).
(8 marks)
(c) Explain the purpose of each of the following commands, and how to use them, giving
examples: (8 marks)
(i) find
(ii) grep
(iii) file
(iv) cp
(v) chmod
(vi) who
(vii) ftp
(viii) mkdir
Page 3 of 4
COSC 224
…………………………………………………………………………………………………………………………………………………
Page 4 of 4