O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 20. Passing parameters to scripts

We have already seen how to pass parameters to scripts by using the special variables $1..$9. Using $# can tell us how many parameters have been passed. We also know how to construct a usage statement, which is when we need to inform the user on how to call a script or function with its proper calling parameters.

In this chapter we will cover:

  • shift;

  • getopts; and

  • shift and getopts examples.

To recap, here’s a framework of a script that handles the parameters start and stop. The script requires two parameters. If it does not get two parameters then a usage statement is produced. Notice we use the case statement to process the different parameters coming into the script.

					$ pg opt #!/bin/sh # opt usage() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required