Main index

Introducing UNIX and Linux


More on shells

Overview
Simple arithmetic
      Arithmetic expansion
            Operators for arithmetic expansion
      The 'expr' command
Pattern matching
      Patterns
            Examples of patterns
      The case statement
Entering and leaving the shell
More about scripts with options
Symbolic links
Setting up terminals
Conventions used in UNIX file systems
Summary
Exercises

Exercises

  1. Write a script cm2ftin which uses arithmetic expansion to convert from centimetres to feet and inches, rounded down to the nearest whole number of inches. Input should be a whole number of centimetres, and you may assume 1 foot is 30 cm.

    cm2ftin
    Enter cm: 42
    42 cm is 1 foot 5 inches

    View solution

  2. Repeat exercise 1 using expr instead of arithmetic expansion.

  3. Write a script to read a single argument, representing the name of a text file, and display the average number of characters per line (excluding Newline characters) to two decimal places. Make sure that the script can handle the cases when it is called with the wrong number of arguments and when it cannot access the required file. Hint: use read and wc.

    View solution

  4. Write a script called area to take two numerical arguments, representing the base length and height of a right-angled triangle, plus one or two options -a and -h (meaning area and help). With option -a, the area of the triangle should be displayed on standard output preceded by the message Area is, and with option -h a short help message should be displayed. With no options, there should be no output; any other option should be ignored, except that a warning message should be output on standard error.

  5. Write a script called hello to display one of Good morning, Good afternoon or Good evening depending on the time of day. You should use the output of date and pattern matching.

    View solution

  6. Write a script called saytime to display the current time in words.

  7. Write a script called drawsquare to take as argument a single number, between 3 and 15 inclusive, and draw on the standard output a square, using the symbols + (plus), - (hyphen) and | (vertical bar), so:

    drawsquare 4
    +--+
    |  |
    |  |
    +--+

    If drawsquare is presented without arguments, with more than 1 argument, or with a single argument that is not a number between 3 and 15, it should display an error message and exit with status 1.

    View solution

  8. Write a script called drawcube to take as argument a single number, between 3 and 15 inclusive, and draw on the standard output a cube, using the symbols + (plus), - (hyphen), / (slash) and | (vertical bar), so:

    drawcube 4
       +--+
      /  /|
     /  / |
    +--+  +
    |  | /
    |  |/
    +--+

    If drawcube is presented without arguments, with more than 1 argument, or with a single argument that is not a number between 3 and 15, it should output an error message and exit with status 1.

  9. Write a script called eurhello to display a greeting in one of several languages. With option -e, or with no options, eurhello should use the greeting Hello, with option -f it should use the French Bonjour, and with option -g it should use the German Guten Tag. It should also allow an option -G, which takes an argument, allowing an arbitrary greeting. Following any options, an argument, which is a string representing the name of the person to whom the greeting is addressed, is required:

    eurhello Chris
    Hello Chris
    eurhello -f "Monsieur le President"
    Bonjour Monsieur le President
    eurhello -G "Hi there" Sam
    Hi there Sam

    If several of the three possible options are given as arguments to the script, the last (rightmost) one takes precedence.

    View solution


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck