Shell Scripting

Shell basics · Files · Users · Permissions

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.

Be careful of the Basics:

  1. Linux IS case sensitive so be careful with working with filenames and with commands
  2. Become familiar with naming conventions i.e. no spaces
  3. In Linux, the shell is a program that interprets commands & acts as an intermediary between the user and the inner workings of the OS.

We will use the Ubuntu Distribution

There are some basic exercises that we will complete from today's handout now.

Manual

There is a very comprehensive manual included which can be used simply by the man command, i.e.

man ls

which will return with the manual page for the ls command.

Examples of some shell scripting commands:

  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

    $ ls -l numusers

This will show the permissions of the file numusers

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

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

    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

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

      $ cat howmany

ASIDE: A practical use of the Octal numbering system

    $ ls -l numusers

will show the amended file permissions that you've set for the file numusers.

For all files, read, write and execute permissions can be granted on the file for three categories, namely user, group and others.

Preceeding the permissions, when using the ls -l command, is the fIle type:

  • " - " means a file
  • " d " means a directory

For example:

 $ chmod 755 numusers

will assign read, write and execute permission to the owner, and just read and execute permission to everyone else (group and others), on the file called numusers

Octal 755 (base8) converts to Binary 111101101 (base2).

Splitting this binary number above that has 9 digits into three groups for each of the three categories of file user, where each 1 meaning turning on for the respective permission, gives the following file permissions:

111 101 101 (base2)

owner 111 All read write execute permissions (read=1, write=1, exe=1)

group 101 Only read and execute permissions (read=1, write=0, exe=1)

others 101 Only read and execute permissions (read=1, write=0, exe=1)

TIP

We could shorten this command again to

 $ chmod +x numusers

Example 2

For the next example suppose you want to write a shell program called “stats” that prints 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

Save your file and exit

Run the script

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.

  • Can you think why a program might benefit from comments?

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:

# 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.

To store a value in a shell variable, you simply write the name of the variable followed immediately by the equals sign =, followed immediately by the value you wish the variable to contain

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. Remember, spaces are not permitted on either side of the equals sign

  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:

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