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

Examples

This section contains examples of Scripting Basics 1. Most of the answers are at the end of the section, with some comments. Some of them require you to actually test the solutions on your own. These examples are not meant to replace the Exercises. Do the exercise sets!

1. Given two integers xxx and yyy, write a command to 

a. output their difference
b. set the variable sum to their sum

2. (this question was moved to module 3)

3. You have the following shell script, named ss1:

#!/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?

4. Which of the following variable names are illegal?

4runnum.bernum_ber_4runcomma,s
typ4metypOverylongvariablenameALLCAPStechno-crat

5. Alexi found the following line in the beginning of a shell script:

TESTDIR=${TESTDIR:-$PWD}

Explain to him what it means.

You should answer (and explain) the questions above before looking at the solutions below!

Solutions

1a. There are several ways to do this:

1b. Either

3a. 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.

3b. Only read permission is necessary to source the file using the dot operator.

3c. Read and execute permission is necessary to run the file as a shell script.

3d. Type in the file and run it each of the four ways. Then  read the notes below and they should make sense:

3e. 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.) 

4. 4run num.ber comma,s and techo-crat  are illegal. A variable name may include letters, numbers and underscores, but it cannot start with a number.

5. The shell script is ensuring that the variable TESTDIR has a value. If it was set and exported, the shell script will use the value. If it was not set (or not exported), the shell script will set TESTDIR to the current directory. Note that, in this case the syntax TESTDIR=${TESTDIR:=$PWD} is no different.

Prev This page was made entirely with free software on linux:  
Kompozer
and Openoffice.org    
Next

Copyright 2011 Greg Boyd - All Rights Reserved.

Document made with Kompozer