Chapter 4. Creating and Calling Subroutines

This chapter shows how to create and call subroutines in a script.

The topics covered in this chapter are as follows:

  • Show some simple subroutines.
  • Show more advanced routines.
  • Mention return codes again and how they work in scripts.

In the previous chapters we have seen mostly simple scripts that were not very complicated. Scripts can actually do a whole lot more which we are about to see.

First, let's start with a selection of simple but powerful scripts. These are mainly shown to give the reader an idea of just what can be done quickly with a script.

Clearing the screen

The tput clear terminal command can be used to clear the current command-line session. You could type tput clear all the time, but wouldn't just cls be nicer?

Here's a simple script that clears the current screen:

Chapter 4 - Script 1

#!/bin/sh
#
# 5/8/2017
#
tput clear

Notice that this was so simple I didn't even bother to include a Usage message or return code. Remember, to make this a command on your system do this:

  • cd $HOME/bin
  • create/edit a file named cls
  • copy and paste the preceding code into this file
  • save the file
  • run chmod 755 cls

You can now type cls from any terminal (under that user) and your screen will clear. Try it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset