Lewis Sharpe


Unix Shell Scripting - Interactive Lab Session

F27PX - Praxis (2018/2019)

Week 5, October 2018

Instructor: Lewis Sharpe


Total exercise time: 60-90 minutes


Segment 0 - Session Aims, Objectives, and Logisitics:

What you should know already?

You should have learnt basic linux usage already in previous session with Dr Hans-Wolfgang Loidl here.


Session Aim:

To allow students to understand the concept, purpose and importance of basic shell scripting using bash for a Unix based operating system.


Session learning outcomes:

1. What is a shell variable and how to define them,

2. How to use bash control structures effectively (if, for, conditionals, etc.), and how to define a function in your script

3. How to use text processing utility commands effectively (sed, awk, grep, etc),

4. How to create a simple game in a shell script.

5. How to output coloured output from a shell script

6. How to create a simple text based animation from a shell script


Find a Partner:

Ideally, do the exercises as paired programming exercises. Work in pairs will allow you to communicate understanding of fresh new knowledge.


Segment 1 - Rudimentary tasks:

Task 1 (laying the foundations: basic and intermediate shell scripting exercises):

Duration: 15 minutes (per levelled material)

Paris


Select starting exercise based on your experience level.

No shell scripting experience:

Click here for basic introductory shell scripting exercises for beginners (Credit: Hans-Wolfgang Loidl).

This will allow you to implement examples in bash of the following, hello world example, shell variable example, arithmetic expressions, regular expressions, overview of bash control structure and implementing shell functions.

Some past shell scripting experience:

Click here for intermediate shell scripting exercises for users with some experience of scripting (Credit: Hans-Wolfgang Loidl).

This section will allow you to create shell script arguments, using text processing utility commands and continous development of line-counting shell script.


Segment 2 - Intermediate tasks:

Task 2 (let there be colour!: colourful shell script):

Duration: 15-20 minutes

color


Click here to download the source code we will be using for task 2.

Save this file as colour.sh, set execute permission on that file by typing chmod a+x colour.sh and then execute the file like this: ./colour.sh.


This script outputs all the colours to the screen. Look at the code and try to understand it, then, try the following:

1. Try adding new colour coordinations of text and foreground to the program - the colour codes will be easy to find with a quick google search.

2. Modify one line of text so half is one colour and half is another.



Task 3 (let it snow, let it snow, let it snow! creating a text-based snow animation using a shell script):

Duration: 15-20 minutes

Paris


Click here to download the source code we will be using for task 3.

Save this file as snow.sh, set execute permission on that file by typing chmod a+x colour.sh and then execute the file like this: ./snow.sh.


Read the code and try to understand it, then, try the following:

1. How are the assignment of snowflakes determined? Is it pre-defined or random?,

2. Try to increase the rate of flow of snowflake falling,

3. Modify the code so different shaped snowflake are falling with the '*' snowflake symbols. Try the '#' symbol.

4. What is the purpose of the use of the sleep function towards the end of the bash program?



Segment 3 - Advanced tasks:

Task 4 (3 in a row, we have a winner!: tic tac toe game using a shell script):

Duration: 15-20 minutes

Paris


Click here to download the source code we will be using for task 4 (Credit).

Save this file as game.sh, set execute permission on that file by typing chmod a+x game.sh and then execute the file like this: ./game.sh.


Read the code and try to understand it, then, try the following:

1. Try to increase the board size.

2. Could the board representation be improved?

3. Could the AI be improved?

4. What types of specific bash control structures have been used, and would you have coded it differently?



Task 5 (change what you see for the better: modify your prompt on the command line):

Duration: 10 minutes

Paris

Click here to view task 5. (Credit: Hans-Wolfgang Loidl).

This is an advanced exercise, modify your prompt on the command line, so that it shows the user-name, the host-name, as well as the available disk space and disk utilisation on the current partition.


Segment 4 - Further Reading and Learning:

Further Reading list available: here - detailed textbook on Linux, shell and much more.

Where you might encounter shell scripting in future learning within your degree programme at Heriot-Watt University?

F27CX/F27CS - Introduction to Computer Systems,

F28HS - Hardware-Software Interface,

F21DP - Parallel and Distributed Technology,

F21CN - Computer Network Security,

F28DA - Data Structures and Algorithms.


Segment 5 - Learning Outcomes Checklist:

Finished tasks. Have you achieved the learning outcomes?

1. What is a shell variable and how to define them.


2. How to use bash control structures effectively (if, for, conditionals, etc.), and how to define a function in your script.


3. How to use text processing utility commands effectively (sed, awk, grep, etc).


4. How to create a simple game in a shell script.


5. How output coloured output from a shell script.


6. How to create a simple text based animation from a shell script.



IMPORTANT NOTE-

COMPILING AND RUNNING THE EXAMPLE SHELL SCRIPT FILES:

1) download files: into an approriate directory space

2) to compile: chmod a+x (filename).sh

3) to execute: ./(filename).sh

(make sure the file is trying to compile and/or execute
is the directory space that you are currently in)
.