Note to the next instructor:
This assignment tacitly assumes that $COLUMNS is usually in your
environment. This is an error on my part. I assumed that $COLUMNS
was, by default, exported. However, it’s not.
One possible fix is to use a different variable: $WIDTH instead
of $COLUMNS. If $WIDTH isn’t set, use 80.
CS155 HW4: Rulers!                
Summary                
Alas, this assignment concerns neither Prime Minister Justin Trudeau of
Canada or Queen Jetsun Pema of Bhutan.
                
It it sometimes useful to be able to quickly count columns in a terminal
window. For this assignment, you will write a bash script called
ruler
which will display a ruler.
                
Arguments                
ruler
will take one optional command-line argument:
                
-
ruler
-
If no arguments are given, use the environment variable
COLUMNS
to determine the length of the ruler.
If COLUMNS
is not set, assume eighty columns.
-
ruler half
-
If the word “
half
” is given, display a ruler half as wide (round down)
as would be otherwise displayed, if no arguments were given.
-
ruler
n -
If a single numeric argument is given, display a ruler that wide.
Examples                
Here are some examples, where %
is my prompt:
% echo $COLUMNS
80
% ./ruler
12345678901234567890123456789012345678901234567890123456789012345678901234567890
1 2 3 4 5 6 7 8
% ./ruler half
1234567890123456789012345678901234567890
1 2 3 4
% ./ruler 27
123456789012345678901234567
1 2
Requirements                
- You may not change, add, or remove from the required output.
- This implies that extra messages are forbidden.
- Your program should emit a helpful error message if:
- too many arguments are given
- an argument < 1 is given
- a bad argument is given
- If your program emits an error message, it should not emit anything else.
For example, given the argument “
bonehead
”, it would be wrong for the
program to complain about that argument, and then go on
to display a ruler.
- In cases where multiple requirements are violated,
it doesn't matter which requirement you complain about.
Pick one, complain, and that should be all.
- This must be a pure
bash
script, and not use any other languages
such as perl or awk. You may use programs such as sed or grep.
- Don’t be concerned about leading zeroes in numbers. They are used by
bash to indicate octal (base 8). We will not test your program with
leading zeroes.
- If the requested length is wider than the terminal, it will look bad.
That’s ok.
- Ruler lengths ≥ 100 will not be tested.
- The first line of your script must be:
#! /bin/bash
- Your script must include a comment block after the first line that contains:
your name, course, assignment, and date.
Hints                
There’s a convenient mechanism to get a default value for a Bash variable.
Look for it if you’re interested.
                
How to submit your homework:                
Via web checkin, or:
                
~cs155/bin/checkin HW4 ruler
How to receive negative points:                
Turn in someone else’s work.