sections in this module | City College of San Francisco - CS160B Unix/Linux Shell Scripting Module: Intro |
module list |
This section contains examples of the Intro module. Most of the answers are at the end of the section, with some comments. These examples are not meant to replace the Exercises. Do the exercise sets!
Example 1:
You have the following shell script, named ss1: (It can be found in examples-shotts/intro )
commands | cd export answer answer="answer:" . ss1 echo $answer pwd | cd unset answer answer="answer:" . ss1 echo $answer pwd | cd export answer answer="answer:" ./ss1 echo $answer pwd | cd unset answer answer="answer:" ./ss1 echo $answer pwd |
output |
e. Would there have been any difference in the results if ss1 had the line export answer as its last line?
Example 2:
What do each of the following commands do to the standard output and standard error of xxx?
Example 3:
You have a command xxx that outputs both to standard output and to standard error. Write a command to
send standard output of xxx to the file output and the standard error to the file errors.
send both standard output and standard error of xxx to the file output
send standard output of xxx to the file output and throw away the error messages.
Write a pipelined command with these attributes:
Example 1:
1a. The description 'shell script' or 'commands file' refers to how you run the file of commands, not to the file itself, although a file of commands that is meant to be source-d (using the dot operator) is usually referred to as a commands file, and a file of commands that is meant to be executed directly is usually referred to as a shell script.
1b. Only read permission is necessary to source the file using the dot operator.
1c. Read and execute permission is necessary to run the file as a shell script.
1d. Type in the file and run it each of the four ways. Then read the notes below and they should make sense:
1e.
no. the export command only affects local variables. Once a variable is
in the environment, it is in the environment for that process and all
of its child (and its child's children ... ) proecesses. Since a
variable that is exported is copied to a child process (such as a shell
script), it can only be used to initialize the variables of a child
process. A child's variables can never affect the parent. (Note that
the terminology 'child' only affects files run as shell scripts, where
there are two sets of variables: one for the parent and one for the
child. Files run using the dot operator (sourced) run their commands in
the current shell, so there is only one set of variables.)
Example 2:
We assume that both standard error and standard output go to the monitor by default when xxx is run.
Example 3:
Let's go through these one at a time:
1. send standard output of xxx to the file output and the standard error to the file errors. This one is easy:
xxx > output 2> errors
2. send both standard output and standard error of xxx to the file output
xxx > output 2>&1 OR
xxx 2> output >&2
3. send standard output of xxx to the file output and throw away the error messages.
xxx > output 2> /dev/null
Write a pipelined command with these attributes:
xxx 2> xxx.errors | tee xxx.out | yyy > yyy.out 2>&1
Prev | This page was made entirely with free software on Linux: Kompozer and LibreOffice |
Next |