Introducing the Shell

Overview

Teaching: 5 min
Exercises: 0 min
Questions
  • What is a command shell and why would I use one?

Objectives
  • Explain how the shell relates to the keyboard, the screen, the operating system, and users’ programs.

  • Explain when and why command-line interfaces should be used instead of graphical interfaces.

Background

The Shell

$

So let’s try our first command, which will list the contents of the current directory:

$ ls
Desktop     Downloads   Movies      Pictures
Documents   Library     Music       Public

Command not found

If the shell can’t find a program whose name is the command you typed, it will print an error message such as:

$ ks
ks: command not found

Usually this means that you have mis-typed the command.

Is it difficult?

Flexibility and automation

Nelle’s Pipeline: A Typical Problem

Nelle Nemo, a marine biologist, has just returned from a six-month survey of the North Pacific Gyre, where she has been sampling gelatinous marine life in the Great Pacific Garbage Patch. She has 1520 samples in all and now needs to:

  1. Run each sample through an assay machine that will measure the relative abundance of 300 different proteins. The machine’s output for a single sample is a file with one line for each protein.
  2. Calculate statistics for each of the proteins separately using a program her supervisor wrote called goostats.
  3. Write up results. Her supervisor would really like her to do this by the end of the month so that her paper can appear in an upcoming special issue of Aquatic Goo Letters.

It takes her about two weeks to run her samples by the assay machine. Now she has the daunting task of analysing her results by running ‘goostats’. The bad news is that if she has to run goostats by hand using a GUI, she’ll have to select a file using an open file dialog 1520 times. At 30 seconds per sample, the whole process will take more than 12 hours (and that’s assuming the best-case scenario where she is ready to select the next file as soon as the previous sample analysis has finished). This zero-breaks always-ready scenario is only achievable by a machine so it would likely take much longer than 12 hours, not to mention that the chances of her selecting all of those files correctly are practically zero. Missing that paper deadline is looking increasingly likely.

The next few lessons will explore what she should do instead. More specifically, they explain how she can use a command shell to run the goostats program, using loops to automate the repetitive steps e.g. entering file names, so that her computer can work 24 hours a day while she writes her paper.

As a bonus, once she has put a processing pipeline together, she will be able to use it again whenever she collects more data.

Key Points

  • A shell is a program whose primary purpose is to read commands and run other programs.

  • The shell’s main advantages are its high action-to-keystroke ratio, its support for automating repetitive tasks, and its capacity to access networked machines.

  • The shell’s main disadvantages are its primarily textual nature and how cryptic its commands and operation can be.