Tutorial 5

In this tutorial, we are going to familiarize ourselves with shell programming. We will have some hands-on experience with ready to use scripts. We will also create some scripts ourselves as exercise. Use Vi, Emacs or Pico for editing the scripts. Login to one of the following Unix servers:

cs1.utdallas.edu

cs2.utdallas.edu

Use PuTTY or Xmanager for logging on as you have learnt in Tutorial 1.

For some basic constructs which we are going to use, you can consult the book for reference. The page numbers are given below:

read: page 353
exit: page 356
if: page 358
test: page 359
while: page 373
break: page 377

First example script: Hello World

In our first example, we will write a simple script which will print the string "Hello World!" to the terminal. Open your favorite Unix editor and type in the following script:

#!/bin/bash
# hello world shell script

echo Hello World!

 Everything on the right of a '#' is comment. The first line is a special comment, it is called the She-Bang line. It must reside in every shell script. It contains the path of the shell after a '!'. Save the file as "hello_world.sh" and then from the terminal assign it execute permission like this:

{cs1:~} chmod +x hello_world.sh

Run the script:

{cs1:~} hello_world.sh
Hello World!
{cs1:~}

Note that a new line is printed after the "Hello World!" string. How can you prevent echo from printing the new line?

Fibonacci number

Now we will run a script which calculates n-th Fibonacci number where n will be provided as input when prompted.

User input 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Output 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Here is the script:

#!/bin/bash
# Fibonacci number shell script
# It takes input one number and show Fibonacci number of that serial

# Prompts for an input
echo -n "Which fibonacci number do u want to see? "

# Read the number
read nSerial

# Initializes variables
a=0
b=1
count=2 # Used to control the number of iteration of the while loop
fibonacci_number=$a

# The loop to calculate the number
while [ $count -le $nSerial ]; do # Checks whether count is less than or equal to nSerial
        fibonacci_number=$[$a+$b]
        a=$b
        b=$fibonacci_number
        count=$[$count + 1]
done

# Print the output
echo "Fibonacci $nSerial = $fibonacci_number"

Type the script in the editor and save the file as fib.sh. Run it after assinging execute permission:

{cs1:~} chmod +x fib.sh
{cs1:~} fib.sh
Which fibonacci number do u want to see? 5
Fibonacci 5 = 5

Enter any number you wish when prompted.

Fibonacci number of multiple values

Now, we will write a script which will calculate n-th Fibonacci number for multiple n's. The script will prompt for an input until -1 is entered. After each input, it will show n-th Fibonacci number.

#!/bin/bash
# Fibonacci number shell script
# It takes input multiple numbers and show Fibonacci number of those serial
# It stops taking input when -1 is given

# Prompts for an input
echo -n "Which fibonacci number do u want to see? (Enter -1 to exit) "

# Read the number
read nSerial

# Loops until -1 is given as input
while [ $nSerial -ne -1 ]; do # Checks whether nSerial is not equal to -1
        # Initializes variables
        a=0
        b=1
        count=2 # Used to control the number of iteration of the while loop
        fibonacci_number=$a

        # The loop to calculate the number
        while [ $count -le $nSerial ]; do # Checks whether count is less than or equal to nSerial
                fibonacci_number=$[$a+$b]
                a=$b
                b=$fibonacci_number
                count=$[$count + 1]
        done

        # Print the output
        echo "Fibonacci $nSerial = $fibonacci_number"

        # Prompts for an input
        echo -n "Which fibonacci number do u want to see? (Enter -1 to exit) "

        # Read the number
        read nSerial
done

Type the script in editor and save as fib_m.sh. Run after assigning execute permission:

{cs1:~} chmod +x fib_m.sh
{cs1:~} fib_m.sh
Which fibonacci number do u want to see? (Enter -1 to exit) 3
Fibonacci 3 = 2
Which fibonacci number do u want to see? (Enter -1 to exit) 10
Fibonacci 10 = 55
Which fibonacci number do u want to see? (Enter -1 to exit) -1

Enter any number you wish. To exit, enter -1.

Fibonacci number of one value read from file

This script will work similarly to our first Fibonacci example except that it will take input from a file containing a number. The filename should be supplied by command line argument.

#!/bin/bash
# Fibonacci number shell script
# It takes input one number from a file and shows Fibonacci number of that serial

# Checks whether user provided one file name.
# If the check fails, it prints and error message and exits
# with an error code of 1
if [ $# -ne 1 ]; then
        echo $0: Please provide a file name
        exit 1
fi

# Initialize variables
a=0
b=1
count=2 # Used to control the number of iteration of the while loop
fibonacci_number=$a

# Reads the file to variable end, filename is stored in command line
# argument $1
read end < $1

# The loop to calculate the number
while [ $count -le $end ]; do # Checks whether count is less than or equal to end
        fibonacci_number=$[$a+$b]
        a=$b
        b=$fibonacci_number
        count=$[$count + 1]
done

# Print the output
echo "Fibonacci $end = $fibonacci_number"

Type the script in editor and save as fib_f.sh. Before running the script create a file containing a single number. Suppose, we create the file named fib.in. Run the script after assigning execute permission:

{cs1:~} echo 11 > fib.in
{cs1:~} chmod +x fib_f.sh
{cs1:~} fib_f.sh fib.in
Fibonacci 11 = 89

Fibonacci number of multiple values read from file

Now, we will write a script which will read multiple values from a file. The file should contain multiple values, each value in separate lines. The filename should be supplied by command line argument.

#!/bin/bash
# Fibonacci number shell script
# It takes input more than one number from a file each residing in a single line
# and shows Fibonacci number of that serial

# Checks whether user provided one file name.
# If the check fails, it prints and error message and exits
# with an error code of 1
if [ $# -ne 1 ]; then
        echo $0: Please provide a file name
        exit 1
fi

# Read value into "end" until end of file
while read end; do

        # Initialize variables
        a=0
        b=1
        count=2 # Used to control the number of iteration of the while loop
        fibonacci_number=$a

        # The loop to calculate the number
        while [ $count -le $end ]; do # Checks whether count is less than or equal to end
                fibonacci_number=$[$a+$b]
                a=$b
                b=$fibonacci_number
                count=$[$count + 1]
        done

        # Print the output
        echo "Fibonacci $end = $fibonacci_number"

done < $1 # Input from file $1

Type the script in editor and save as fib_m_f.sh. Here is a sample input file fib_m.in containing values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and 20. Right-click the link and save the file in appropriate location. Run the script after assigning execute permission:

{cs1:~} chmod +x fib_m_f.sh
{cs1:~} fib_m_f.sh fib_m.in
Fibonacci 0 = 0
Fibonacci 1 = 1
Fibonacci 2 = 1
Fibonacci 3 = 2
Fibonacci 4 = 3
Fibonacci 5 = 5
Fibonacci 6 = 8
Fibonacci 7 = 13
Fibonacci 8 = 21
Fibonacci 9 = 34
Fibonacci 10 = 55
Fibonacci 20 = 6765
 

Exercise

In the first Fibonacci script, add error check on the input for negative integers and real numbers as they are not defined in the series. If user inputs such number the output should be "not defined". For example:

Fibonacci -1 = not defined
Fibonacci -11 = not defined
Fibonacci 6.89 = not defined
Fibonacci -99.127 = not defined