Shell Scripting

Time for you to practice!

What is “Shell Scripting”?

  • In Linux, it is sometimes necessary to perform a number of complex shell commands in order to achieve an overall goal.
  • In the situation where we have to perform a sequence of complex or repetitive shell commands, this process can be very tedious and time consuming.
  • The interpretative nature of the shell in Linux provides us with an alternative that helps speeds up this process.

  • This facility is known as “Shell Scripting” or “Shell Programming” .

A script is a file containing shell commands that can interact with the user, repeat commands and make decisions.

To avail of this flexibility provided by “Shell Programming”, all we need to do is to:

  1. Place the shell commands (which we would normally type at the command prompt) into a file and
  2. then execute the file.

This is a self-paced tutorial sheet for you to complete.

Examples:

  1. Suppose we need to find out the number of logged-in users several times throughout the day: The Linux command would be as follows:
     $ who | wc –l
    Please note:
    • This is the lowercase letter " l ", not the number "1"
    • Do NOT type the $ sign - this is just shorthand for the command line prompt

Note that this command is actually a combination of two commands, connected by a pipe.

A pipe is used to connect the output of one command to the input of another command. The vertical bar symbol | is used to specify a pipe. In this example, the output of who is provided as input to the wc –l command. who lists the users currently logged on, one per line. wc –l counts the number of lines in its input.

So, to avoid having to re-type this command each time we wish to find out how many users are logged in, we will create a file called numusers.

Creating a Shell Scripting File

nano is a command-line text editor. gedit is a good graphical alternative

  1. We create the file
     $nano numusers
  2. We type in the shell command
     who | wc –l
  3. We save the file
     CTRL+O
  4. And exit the file
     CTRL+X
  5. To execute the commands contained in the file numusers, all you now have to do is type ./numusers at the command line
    $ ./numusers
    bash: ./numusers: Permission denied
    $

So what went wrong?

File Permissions and Re-directing output

Before you can execute a program this way, you must change the file’s permissions and make it executable. Use the chmod command to do this:

    $ chmod +x numusers
  1. So as the first part of this exercise, create and run the above shell script

    You can put any commands at all inside a file, make the file executable, and then execute its contents simply by typing it name to the shell. It’s that simple and that powerful.

  2. As an extension to the script above, use the shell script in conjunction with the file re-direction operator to redirect the output to a file called “howmany”; i.e.

     $ ./numusers > howmany
  3. To see what has been written to the howmany file, enter

      $ cat howmany

Example 2

For the next example suppose you want to write a shell program called stats that printed the date and time, the number of users logged in, and your current working directory. You know that the three command sequences you need to get this information are:

date, who | wc –l, and pwd.
$ nano stats
   Enter the lines:
   date
   who | wc –l
   pwd
$ chmod +x stats
$ ./stats
   Thu May 15 14:12:23 IST 2014
   3
   /home/jbloggs/
$

To make the output of this script a bit more user friendly we can add some text to the script, to appear at certain points of the programs execution. This functionality is achieved through the use of the shell command echo. Add the following lines to your stats program

$ nano stats
  echo The current time and date is:
  date
  echo
  echo The number of users on the system is: 
  who | wc –l
  echo
  echo Your current working directory is:
  pwd
$ ./stats
……….

COMMENTS

The Shell Programming language would not be complete without a # comment statement. A comment statement allows you to insert remarks or comments about your program without it having any effect on its execution. Whenever the shell encounters the special character # at the beginning of a word or line, it takes whatever characters following the # as comments and simply ignores them; e.g.

# Author: C Cahill
# Program Description:
# This program calculates the amount of CPU usage of each user ………

Comments are useful for documenting commands or sequences of commands whose purpose may not be obvious.

VARIABLES

Like virtually all programming languages, the shell allows you to store values into variables.
N.B. do not use spaces

variable_name=value

$ count=1

To assign the value /home/username/bin to the variable mybin, you simply write

$ mybin=/home/username/bin

NOTE: (replace username with your user name)

Two points are worth noting at this stage:

  1. Spaces are not permitted on either side of the equals sign (as mentioned above)

  2. Unlike most programming languages the shell has no concept of data types. Whenever you assign a value to a shell variable, no matter what it is the shell simply interprets that value as a string of characters. So when we stored the value 1 into the shell variable count above, the shell made no distinction whatsoever that an integer value was being stored in the variable.

Displaying the values of Variables

Using the echo command encountered above, we can display the value stored in a shell variable as follows:

echo $variable_name

The shell recognises the $ as a special character, if a valid variable name follows the $ the shell understands that it is to substitute the value in place of the variable name.

$ echo $count
$ echo $mybin

Program input

Using the read command allows us to get input form the user into a variable:

read variable_name
(note no $ sign before variable name)

$ read number
365    (entered by user)
$ echo $number
365    (output)

Exercises

Please complete the following file in your own time

The above file will introduce you to concepts that you should be familiar with:

  1. Arithmetic
  2. IF statements
  3. WHILE and FOR loops
  4. plus links to very useful online resources