Table Of Contents

Previous topic

Script / Program Organization

Next topic

Logic and Decisions

This Page

Exercise 1: Organization

When learning to program, 80% of all work is intelligent copying, 10% is learning general programming conventions, and 10% is learning specific details of a programming language or an environment. The biggest difficulty in learning to program is that the last 10%, specific details, is often required first.

In this exercise, use the 80% copying approach to create a script and add features incrementally to achieve a final result. Hopefully this approach will delay that final 10%.

Create a First Script

Tip

Use dolphin or other file viewer to display your home folder, and create or open your Projects folder. Then create a folder inside Projects, maybe called linux-scripts, where you can store your work.

Make a text file

On the Linux desktop, open a file viewer window (Dolphin), and check the menu option Control ‣ Panels ‣ Terminal to include the terminal console in the display. Create a text file from the console command line, and edit the file using Kate:

touch hello-world.sh
chmod u+x hello-world.sh
kate hello-world.sh

Example Script Layout

Warm up the Xerox by highlighting and copying the following code outline, which can become the basis of scripts written throughout these exercises, or download the script from here, naming it :file”hello-world.sh.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#! /bin/sh
# Describe program's principal objective
# 
# Initialize variables
# 
# Assign parameters ($n) to named variables
# 
# User execution confirmation (location varies)
# 
# Declare repeated code functions
# 
# !! MAIN SECTION !!
# 
# Validity tests (setting exit status)
# 
# Main code, if Valid
# 
# Exit section: echo results, log errors
# 

Hello World

Based on script samples shown previously, edit your script to display “Hello World.” Save your work, and test run the script from the command line.

Tip

  1. Scripts are text files, but using the filename extension .sh helps to identify script files to the user.
  2. Instead of using chmod, right-click the file icon in dolphin and check Properties ‣ Permissions ‣ Is executable.

Hint

  1. Sidebar examples from the previous page demonstrate commands to use.

  2. All Unixes include on-line resources to provide reference documentation:

    Resource Example Description
    help command --help Built-in command documentation
    man man command Manual page(s) for command
    info info command or, Info page(s) for command

Adding Sections

Assign a variable

Declare a variable named DISPLAY by assigning it the value World. In the echo statement which displays Hello World, replace the text World with this new variable. Test the script by executing it in the terminal:

./hello-world.sh

Tip

The ./ is required in front of the script name, so that the command interpreter knows the path to the script.

Edit the hello-world.sh script to include all four versions of the echo statement as follows:

echo 'Hello DISPLAY'  ; # full quoting
echo "hello DISPLAY"  ; # partial quoting
echo 'Hello $DISPLAY' ; # $-operator for evaluation
echo "Hello $DISPLAY" ; # $ evaluation in partial quotes

Execute script hello-world.sh. Which version of the echo statement worked correctly? Now place a # symbol at the beginning of each faulty statement to comment it. Execute the script again to see that only the desired result displays.

Tip

Learning comes from a process of evaluating alternatives, and seeing which one produces a desired result. In aphorisms, “We learn from our mistakes.” This guide will present working code examples, but incorrect code will also be used to help demonstrate correct programming conventions.

Read and assign an input

Based on examples from the previous page, add statements to evaluate input parameter 1 and, if it is not blank, assign the value of parameter 1 to the variable DISPLAY. Run the changed script with the following commands:

./hello-world.sh
./hello-world.sh Ebola

Now include the following four logic test statements preceding your then statement, so your code reads:

# Assign parameters ($n) to named variables
# if [! $1 = ''] && [-n $1]    ;
# if [ $1 != '' ] && [ -n $1 ]   ;
# if [ ! $1 == '' ] && [ -n $1 ]  ;
#if [ -n $1 ] && [ ! $1 = '' ]   ;
then DISPLAY=$1
fi

One at a time: uncomment a statement, test run the script, and note the result in the comments (either works or fails).

Warning

Unlike with quoting and variable evaluation, scripting permits several acceptable forms for simple logic expressions. However, complex tests may not work right with just any syntax. To verify results, logic must be tested.

User confirmation

Add statements to your script to: (1) prompt the user to run the script, and (2) process the user’s response.

Hint

On the previous page, look for an example of the read command for guidance.

Example Finished Program

A sample program for the Hello World scripting challenge can be viewed at: Example Program Outline.