Shell Scripts
Part One Exercise (10 Marks)
Create an interactive script called divtest that tests whether a user-specified number is divisible by two user-specified factors. The script will require that the two factors be specified by the user as integer command-line arguments. The script will prompt the user for the integer to test. To help clarify the requirements, consider the following example. Suppose 10 and 3 are the factors to be used in the test, and 21 is the integer to be tested. The user would run the command with the following command line,
$ divtest 10 3
Then the script would prompt the user for the integer to test and the user would then provide the number 21 followed by pressing the <enter> key. Here is the way the script would run to completion (what the user would type is shown in bold):
$ divtest 10 3
Enter an integer to test: 21
21 is divisible by 3.
$
To give you a feel for the way the script should run under all conditions, here are 3 other sample runs (again, what the user would type is shown in bold):
$ divtest 7 5
Enter an integer to test: 70
70 is divisible by 7.
70 is divisible by 5.
$
$ divtest 10 12
Enter an integer to test: 33
33 is not divisible by 10 or 12.
$
$ divtest 10 12
Enter an integer to test: 70
70 is divisible by 10.
$
Your script should also handle the situation where the user forgets to specify the factors on the command line as follows:
$ divtest
Usage error: two, integer command-line arguments required.
Try again.
Exercise Two (10 marks)
Create a script (that is not interactive) called drawBox that outputs a box of characters with dimensions specified by the user on the command line. When the user runs the command she/he would provide 4 command-line arguments as follows:
• The first argument is an integer specifying the width of the box to be drawn.
• The second argument is an integer specifying the height of the box to be drawn.
• The third argument is a letter that is used to fill the odd numbered lines.
• The fourth argument is a letter that is used to fill the even numbered lines.
The sample runs below demonstrate how your script should run (what the user would type is shown in bold):
$ drawBox
Error: Invalid number of arguments
- Enter 4 arguments on the command line.
- enter 2 integers followed by two letters.
$ drawBox 6 3 X O
XXXXXX
OOOOOO
XXXXXX
$ drawBox 6 3 O X
OOOOOO
XXXXXX
OOOOOO
$ drawBox 20 7 O X
OOOOOOOOOOOOOOOOOOOO
XXXXXXXXXXXXXXXXXXXX
OOOOOOOOOOOOOOOOOOOO
XXXXXXXXXXXXXXXXXXXX
OOOOOOOOOOOOOOOOOOOO
XXXXXXXXXXXXXXXXXXXX
OOOOOOOOOOOOOOOOOOOO
$ drawBox 10 1 O X
OOOOOOOOOO
$ drawBox 10 8 V M
VVVVVVVVVV
MMMMMMMMMM
VVVVVVVVVV
MMMMMMMMMM
VVVVVVVVVV
MMMMMMMMMM
VVVVVVVVVV
MMMMMMMMMM
$ drawBox 20 9 o w
oooooooooooooooooooo
wwwwwwwwwwwwwwwwwwww
oooooooooooooooooooo
wwwwwwwwwwwwwwwwwwww
oooooooooooooooooooo
wwwwwwwwwwwwwwwwwwww
oooooooooooooooooooo
wwwwwwwwwwwwwwwwwwww
oooooooooooooooooooo
$