Chapter 11. Handling User Input
IN THIS CHAPTER
Using command line parameters
Working out your options
Getting input from users
So far you've seen how to write scripts that interact with data, variables, and files on the Linux system. Sometimes, you need to write a script that has to interact with the person running the script. The bash shell provides a few different methods for retrieving data from people, including command line parameters (data values added after the command), command line options (single-letter values that modify the behavior of the command), and reading input directly from the keyboard. This chapter discusses how to incorporate these different methods into your bash shell scripts to obtain data from the person running your script.
Command Line Parameters
The most basic method of passing data to your shell script is by using command line parameters. Command line parameters allow you to add data values to the command line when you execute the script:
$ ./addem 10 30
This example passes two command line parameters (10 and 30) to the script addem
. The script handles the command line parameters using special variables. The following sections describe how to use command line parameters in your bash shell scripts.
Reading parameters
The bash shell assigns special variables, called positional parameters, to all of the parameters entered in a command line. This even includes the name of the program the shell executes. The positional parameter variables are standard numbers, with ...
Get Linux® Command Line and Shell Scripting Bible now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.