Introduction To Bash Script

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Lab 6: Introduction to Bash

Scripts

Operating System Concepts 9th Edition


What is Bash Script?

A Bash script is a plain text file which contains a series of commands. These
commands are a mixture of commands we would normally type ourselves on the
command line (such as ls or cp for example) and commands we could type on the
command line but generally wouldn't (you'll discover these over the next few pages).

An important point to remember though is:


Anything we can run normally on the command line can be put into a script and
it will do exactly the same thing. Similarly, anything we can put into a script can
also be run normally on the command line and it will do exactly the same thing.

Operating System Concepts 9th Edition


How do we run them?

Running a Bash script is fairly easy. Another term you may come across
is executing the script (which means the same thing). Before we can execute a
script it must have the execute permission set (for safety reasons this permission is
generally not set by default).

If you forget to grant this permission before running the script you'll just get an
error message telling you as such and no harm will be done.

Operating System Concepts 9th Edition


How do we run them?

Operating System Concepts 9th Edition


How do we run them?

Why the ./

You've possibly noticed that when we run a normal command (such as ls) we just
type its name but when running the script above we put a ./ in front of it.

When you just type a name on the command line Bash tries to find it in a series of
directories stored in a variable called $PATH. We can see the current value of this
variable using the command echo.

The directories are separated by " : "

Operating System Concepts 9th Edition


How do we run them?

Bash only looks in those specific directories and doesn't consider sub
directories or your current directory. It will look through those directories in order
and execute the first instance of the program or script that it finds.

The $PATH variable is an individual user variable so each user on a system may
set it to suit themselves.

This is done for a few different reasons.


• It allows us to have several different versions of a program installed. We can
control which one gets executed based on where it sits in our $PATH.

Operating System Concepts 9th Edition


How do we run them?
• It allows for convenience. If I set the following path in my $PATH variable

/home/smahmud74/bin

• This allows me to put my own scripts and programs there and then I can use
them no matter where I am in the system by just typing their name. I could
even create a script with the same name as a program (to act as a wrapper) if I
wanted slightly different behaviour.

• It increases safety - For example a malicious user could create a script


called ls which actually deletes everything in your home directory. You
wouldn't want to inadvertently run that script. But as long as it's not in your
$PATH that won't happen.

Operating System Concepts 9th Edition


How do we run them?
• If a program or script is not in one of the directories in your $PATH then you
can run it by telling Bash where it should look to find it. You do so by
including either an absolute or relative path in front of the program or script
name.

• You'll remember that dot ( . ) is actually a reference to your current directory.


Assuming this script is in my home directory I could also have run it by using
an absolute path.

Operating System Concepts 9th Edition


How to Change your Path
Adding a new directory in the $PATH variable.

Copy your script in that directory.

Execute your script from anywhere.

Operating System Concepts 9th Edition


The Shebang
The Shebang (#!)

#!/bin/bash
This is the first line of the script above. The hash exclamation mark ( #! )
character sequence is referred to as the Shebang. Following it is the path to the
interpreter (or program) that should be used to run (or interpret) the rest of the
lines in the text file. (For Bash scripts it will be the path to Bash, but there are
many other types of scripts and they each have their own interpreter.)

Formatting is important here. The shebang must be on the very first line of the
file (line 2 won't do, even if the first line is blank). There must also be no spaces
before the # or between the ! and the path to the interpreter.

Operating System Concepts 9th Edition


Formatting
Formatting

As we saw above, formatting for the shebang was important (ie no spaces, must be
on first line). There are many areas in Bash scripts where formatting is important.
Typically it involves spaces and either the presence or absence of a space can be the
difference between the command working or not.

I'll point these out as we encounter them. Also get in the habit of being mindful of the
presence or absence of spaces when looking at code.

Operating System Concepts 9th Edition


Variables
A variable is a temporary store for a piece of information. There are two actions we
may perform for variables:

• Setting a value for a variable.


• Reading the value for a variable.

Variables may have their value set in a few different ways:


• The most common are to set the value directly and
• for its value to be set as the result of processing by a command or program.

To read the variable we then place its name (preceded by a $ sign) anywhere in the
script we would like. Before Bash interprets (or runs) every line of our script it first
checks to see if any variable names are present. For every variable it has identified,
it replaces the variable name with its value. Then it runs that line of code and begins
the process again on the next line.

Operating System Concepts 9th Edition


Command line arguments
Command line arguments are commonly used and easy to work with so they are a
good place to start.

When we run a program on the command line you would be familiar with supplying
arguments after it to control its behaviour.

For instance we could run the command ls -l /etc. -l and /etc are both command line
arguments to the command ls.

We can do similar with our bash scripts. To do this we use the variables $1 to
represent the first command line argument, $2 to represent the second command
line argument and so on. These are automatically set by the system when we run our
script so all we need to do is refer to them.

Operating System Concepts 9th Edition


Command line arguments

Operating System Concepts 9th Edition


Setting Our Own Variables
There are a few ways in which variables may be set (such as part of the execution of a
command) but the basic form follows this pattern:

This is one of those areas where formatting is important. Note there is no space on
either side of the equals ( = ) sign. We also leave off the $ sign from the beginning of
the variable name when setting it.

Variable names may be uppercase or lowercase or a mixture of both but Bash is a


case sensitive environment so whenever you refer to a variable you must be
consistent in your use of uppercase and lowercase letters.

Operating System Concepts 9th Edition


Setting Our Own Variables

Variables can be useful for making


our scripts easier to manage.
Maybe our script is going to run
several commands, several of
which will refer to a particular
directory. Rather than type that
directory out each time we can set
it once in a variable then refer to
that variable.

Operating System Concepts 9th Edition


Setting Our Own Variables

Quotes:
In the example above we kept things nice and simple. The variables only had
to store a single word. When we want variables to store more complex values
however, we need to make use of quotes. This is because under normal
circumstances Bash uses a space to determine separate items.

Because commands work exactly the same on the command line as in a script
it can sometimes be easier to experiment on the command line.

Operating System Concepts 9th Edition


Setting Our Own Variables

When we enclose our content in quotes we are indicating to Bash that the
contents should be considered as a single item. You may use single quotes
( ' ) or double quotes ( " ).

• Single quotes will treat every character literally.


• Double quotes will allow you to do substitution (that is include variables
within the setting of the value).

Operating System Concepts 9th Edition


Command Substitution
Command substitution allows us to take the output of a command or program
(what would normally be printed to the screen) and save it as the value of a
variable. To do this we place it within brackets, preceded by a $ sign.

Command substitution is nice and simple if the output of the command is a


single word or line. If the output goes over several lines then the newlines are
simply removed and all the output ends up on a single line.

Operating System Concepts 9th Edition

You might also like