Main index

Introducing UNIX and Linux


Introduction to shells

Overview
Why do we need a shell?
Shell syntax
      Types of shell command
      Simple commands
      Pipelines
      Grouping commands
      Exit status
      List commands
Arithmetic
      Operators and functions
Making decisions
      The 'test' statement
            Operators used by 'test'
      The 'if' statement
Loops
      'For' loops
      'While' and 'until' loops
Searching for files
      Arguments to 'find'
Formatted output
      Arguments to 'printf'
Passing information to scripts
      Scripts with arguments
      Parameter expansion
Summary
Exercises

Exercises

  1. List all regular files in your home directory whose size is less than 512 bytes.

    View solution

  2. What is the maximum length of a line in /usr/dict/words?

  3. Write a script called mcat which will be identical to cat with the following difference: if any file argument given to mcat either does not exist or is unreadable, mcat will not send any output to the standard output. The behaviour of cat is such that all its readable arguments are copied to standard output.

    View solution

  4. List the name of each regular file in the current directory (or any subdirectory of it), together with the first line of the file.

  5. List all users currently logged in to the system, in 4 columns of width 10 characters, so:

         chris        jo       sam    george
          pete       sue      dave      jane
          emma      bill
    

    View solution

  6. Write a script to prompt the user for two numbers, representing the width and height of a rectangle in cm, and display the area of the rectangle both in square metres and in square inches (1 inch = 2.54 cm).

  7. Write a script to take text input from standard input, and copy it to standard output with each line preceded by a line number in the same manner as cat -n. Do not use the command cat.

    View solution


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck