Shell Components   «Prev  Next»
Lesson 9

Components of a Shell Program

This module introduced you to the programming concepts that are used in creating a shell script. If you have programmed before, these concepts may be familiar to you, though their application in shell scripts may differ from other situations. Now that you understand these concepts, you are ready to dive into creating your first real shell script.

Terms and concepts

This module introduced you to the following terms and concepts:
  1. Built-in commands
  2. External commands
  3. Managing input and output in a script
  4. A shell script as a command line macro
  5. Tests within if/then/else statements
  6. Variables in shell scripts
  7. Control and looping structures
  8. Regular expressions

Linux Shell Scripting
Basically, a shell script is a text file with Unix commands in it.
Shell scripts usually begin with a #! and a shell name

Example:
#!/bin/sh

If the shell script does not begin with #!/bin/sh, the user's current shell environment will be used
Any Unix command can go in a shell script: Commands are executed in order or in the flow determined by control statements.
Different shells have different control structures
  1. The #! line is very important
  2. We will write shell scripts with the Bourne shell (sh)

Why write shell scripts?

  1. To avoid repetition: If you do a sequence of steps with standard Unix commands over and over, why not condense those commands and enable repetition with just one command?
  2. To automate difficult tasks: Many commands have subtle and difficult options that you do not want to figure out or remember every time.

It is possible to create shell scripts that are far more interactive and involving than the regular
  1. Press 1 to continue,
  2. Press 2 to exit
type menu systems that are usually passed off as interactive shell scripts. The shell can do much more than is regularly exploited, and although the shell is far from being the perfect language for game development, I hope this recipe will inspire more creative uses of shell scripts and particularly the newer features available in the bash shell (such as arrays) and in the GNU environment (like sleep being able to perform sub-second sleeps, without which this game is much less fun to play).