sections in this module City College of San Francisco - CS160B
Unix/Linux Shell Scripting
Module: Intro
module list

Examples

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 )

#!/bin/bash
answer="$answer yes"
echo "$answer"
cd /tmp
a. Is it a commands file or a shell script?
b. What minimum permissions are needed for ss1 to run it as follows: . ss1
c. What minimum permissions are needed for ss1 to run it as follows: ./ss1
d. In the table below, there are four sequences of commands involving ss1. Fill in the output of each sequence:
commandscd
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?

  1. xxx > xxx.out
  2. xxx 2> xxx.errs
  3. xxx > xxx.out 2>xxx.errs
  4. xxx > xxx.out 2>&1
  5. xxx 2>/dev/null
  6. xxx 2>&1 > xxx.out
  7. xxx | more
  8. xxx 2>&1 | more
  9. xxx | tee xxx.out

Example 3:

You have a command xxx that outputs both to standard output and to standard error. Write a command to

  1. send standard output of xxx to the file output and the standard error to the file errors.

  2. send both standard output and standard error of xxx to the file output

  3. send standard output of xxx to the file output and throw away the error messages.

  4. Write a pipelined command with these attributes:

Answers

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.

  1. saves the standard output of xxx in the file xxx.out.  standard error output still goes to the monitor.
  2. saves the standard error of xxx in the file xxx.errs. standard output still goes to the monitor
  3. saves the standard output of xxx in the file xxx.out. The standard error of xxx is saved in the file xxx.errs
  4. saves both standard output and standard error in the file xxx.out. The file should have the output in the same order that it would have appeared on the monitor if no output was redirected.
  5. throws away the standard error of xxx. The standard output still goes to the monitor.
  6. this saves the standard output of xxx in xxx.out. The standard error is not redirected. This is because redirection operators are order-dependent, and at the time the 2>&1 operator is processed, standard output is still going to the monitor.
  7. the standard output of xxx is sent through the pager more. standard error still goes to the monitor without being paged.
  8. here, both standard output and standard error are sent through the pager more
  9. standard output is both saved in the file xxx.out and sent to the monitor. standard error just goes to the monitor.

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

  1. 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

Copyright 2016 Greg Boyd - All Rights Reserved.

Document made with Kompozer