CS29206 Systems Programming Laboratory, Spring 2022-2023
CS29206 Systems Programming Laboratory, Spring 2022-2023
Class Test 2
13–April–2023 03:00pm–04:00pm Maximum marks: 60
1. (a) Consider a text file input.txt, containing alphanumeric text along with special characters. Write a single grep
command which matches one (or both) of the following two kinds of strings.
(i) Strings starting with a lower-case letter at the beginning of the line, followed by any number of alphanumeric
characters, and ending with a lower-case vowel (not necessarily at the end of the line),
(ii) Strings starting with an upper-case letter (not necessarily at the beginning of a line), followed by any number of
characters (alphanumeric or special), and ending with a lower-case letter at the end of the line. (5)
#!/usr/bin/gawk -f
BEGIN {
status = "Failure"
getline S < "-"
compare(S)
print status
}
— Page 1 of 4 —
2. (a) Consider entries in a telephone directory with filename directory.txt as follows.
+123-334-889-778
+880-1855-456-907
+9-7777-38644-808
+123-443-998-887
Write a gawk command (not a script) which takes directory.txt as a command-line argument, and the prints
only the country codes in all the lines, as shown below. The same country code may be printed multiple times.
+123
+880
+9
+123 (3)
In the code given below, the associative array state[] is accessed against every state found in the dossier file. If you choose, you
can set that entry to anything like 0 or 1 or "found".
#!/usr/bin/gawk -f
BEGIN { FS = "," }
— Page 2 of 4 —
3. (a) Write a sequence of bash commands in a script, (b) What will be printed by the following bash code (4 × 5)
that reads (from the terminal) two parts of the user’s snippet? Write your answer below the snippet.
name (may contain spaces) in variables firstname
declare -ai P=(2 3 5 7)
and lastname, and then sets and prints a variable
P[5]=11
fullname. Write your answer below the following
echo ${P[3]}
sample I/O. The quotes shown should be printed.
echo ${P[@]}
First name: Foo Bar echo ${!P[@]}
Last name: Basu Roy Chowdhury echo ${#P[@]}
Full name: "Foo Bar Basu Roy Chowdhury"
(c) Suppose that the bash variable pattern stores a regular expression. You want to search for this regular
expression in a file myfile.txt. You can use the following command:
grep -e "$pattern" myfile.txt
Two other methods for the same search are sketched below. The first method uses pipe |, and the second method
uses string redirection <<<. Fill in the blanks (write nowhere else) to complete the commands of these alternative
methods. In each blank, use a standard Unix command to print the entire file myfile.txt to stdout.
(d) What will be printed by the following bash code (e) What will be printed by the following bash code
snippet? Write your answer below the snippet. snippet? Write your answer below the snippet.
x=15; y=25 function f () {
function Fxy () { echo echo Hello, World!
echo "x = $x, y = $y" }
} g=‘f‘
function F () { echo "$g"
Fxy echo ’$g’
y=30; local x=10 y=20 echo ‘$g‘
Fxy ‘echo $g‘
}
F
Fxy
— Page 3 of 4 —
4. Write an executable bash script to do the following task. The script uses a directory name dir. If that name is
supplied by the user as the first command-line parameter, dir is set to that parameter, otherwise dir is set to the
current directory. The script then checks whether dir is a directory and has read permission. If not, it exits with some
error status. Otherwise, it proceeds to create a file myfiles.zip in the current directory as outlined below (assume
that you have permission to write in the current directory). The script checks whether myfiles.zip is already
present in the current directory, and if that is the case, the script deletes the file. After that, the zip file is created using
the following command, where file1, file2, file3, . . . are all of the regular files in dir having read permission.
zip myfiles.zip file1 file2 file3 ...
Before invoking the command, the script makes a listing of all the files in dir, and identifies (and stores) the names of
all the regular files in dir with read permission. If there is no such file, the zip command is not invoked. Otherwise,
myfiles.zip is created using the above command. Write the executable bash script below to perform this task.
Note that you should call zip only once (provided that there are file(s) in dir to zip). You must not incrementally
add file(s) to the zip archive. (10)
#!/bin/bash
— Page 4 of 4 —