Prev | Current Page 361 | Next

Michael McCallister

"openSUSE Linux Unleashed"

Process command line arguments and options supplied by the user
. Interactively query the user for data while the script is running
Command line arguments and options allow the user to enter data on the command line
when the shell script is executed. An option is a dash followed by a single letter. Options are
often used to control the behavior of a script, such as whether or not to produce an output.
A command line argument is a string value that can be a single text string, a number, or a
string with spaces enclosed in double quotes. Each argument or option is separated by
one or more spaces. The shell script assigns variables to each argument and option that is
specified. These variables are shown in Table 8.3.
TABLE 8.3 Shell Command Line Variables
Variable Description
$0 The name of the shell script
$1 The value of the first argument (continuing for $2, $3, and so on)
$# The number of command line arguments specified
$* A string containing all of the command line arguments
$- A string containing all of the options specified
Advanced Shell Programming 155
8
These argument values can be used anywhere in the script you would use a normal variable:
#!/bin/bash
# testing command line paramters
# enter your name, then two numbers on the command line
echo Hello $1,
result=$[$2 * $3]
echo The answer to your problem is $result
When running the program, you just include the arguments on the command line (in the
proper order):
rich@testing:~> .


Pages:
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373