sections in this module | City College of San Francisco - CS160B Unix/Linux Shell Scripting Module: Arguments |
module list |
This section contains examples of Scripting Basics 2. Most of the
answers are at the end of the section, with some comments. The shell
scripts that are provided for you may be found in the
examples-shotts/args directory in our public work area.
1. Write the following shell script, firstargs:
If the current directory has the following files:
$ ls -1
args
file1
one file
onery
and you have the following variable defined:
Consider the following runs of firstargs. For each, write the output of the shell script
./firstargs o* | ./firstargs $Name | ./firstargs $(ls o*) | ./firstargs "$Name" |
2. Change directory to the examples-shotts/args directory in our public directory. Examine the shell script args.bash. Then run it with the following command-line arguments:
3. Still in the class args directory, examine the shell script shiftargs.bash. Then run it with the following:
You should answer (and explain) the questions above before looking at the solutions below!
Solutions
1. Write the shell script and test it. After you examine the rest of the examples, the results should make sense.
2. a-c should be no surprises
d-e: When a wildcard is used to list items, each item name implicitly is enclosed in quotes, since all the processing remains in the shell. Thus, the names of files get assigned to the correct positional parameters. When command-substitution is used, the list of items goes to standard output. This is a simple data interface which loses any information about what the item names actually are. When the text is broken up using the standard technique (using delimiters), multi-word item names are broken into separate words.
The lesson here is to use a wildcard to list data items whenever you can.
3. b-c should be no surprises.
a. The shell script shiftargs only shows
the first six arguments. Since we gave it seven, the last was not shown
until after the shift. (Note that shiftargs had a $7 variable set - it just did not output it.)
Prev | This page was made entirely with free software on Linux: Kompozer and LibreOffice.org |
Next |