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

Answer to chapter 8 question 1

This is an exercise in arithmetic expansion only.

printf "Enter cm: "         # Prompt
read CM                     # Read number of cm
FEET=$(( $CM / 30 ))        # Feet is easy to calculate
CM=$(( $CM % 30 ))          # Replace CM by residual cm
                            #   above the previous feet
INCHES=$(( $CM * 12 / 30 )) # Convert residual cm
                            #   to inches

printf "%d cm is %d foot %d inches\n" $CM $FEET $INCHES

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck