Chapter 9. Shell Scripting Basics
Chapter 9. Shell Scripting Basics
SHELL SCRIPTING
BASICS
TOPICS
01 WHAT IS A SHELL
04 VARIABLES
05 USER INPUT
06 ARITHMETIC OPERATIONS
WHAT IS A SHELL
Shell is a program that accepts
commands from the standard input
device such as keyboard or from a
file, and translates it into a
computers’ native binary language
and passed to the kernel for
execution.
USING THE SHELL
To use the shell you simply type commands.
A command is a computer program, which is built to
perform a specific task.
Examples of commands include:
ls
clear
date
WHAT IS A SHELL SCRIPT
Normally shells are interactive. It will accept command
from you (via keyboard) and execute them.
However, if you store a sequence of commands to a text
file and tell the shell to execute the text file instead of
entering the commands via keyboard, that is known as a
shell program or shell script.
A shell script can be defined as - "a series of
command(s) stored in a plain text file".
WRITING A SHELL SCRIPT
To successfully write a shell script, you have to do three
things:
1. Write a script
2. Put it somewhere the shell can find it
3. Give the shell permission to execute it
1. WRITE A SCRIPT
To create a shell script, you use a text editor. Open a
text editor and type the code below:
#!/bin/bash
# My first script
echo Hello, World!
UNDERSTANDING THE CODE
1st Line - Shebang
The first line is a special type of comment called a
shebang or hash-bang. It tells the system which
interpreter to use to execute the file.
The characters #! MUST appear at the very beginning of
the first line; in other words, they must be the first two
bytes of the file for it to be recognized.
UNDERSTANDING THE CODE
2nd Line - Comments
The second line is a comment.
Comments begin with an octothorpe, or hash (#), at the
beginning of a word and continue until the end of the
line. The shell ignores them.
UNDERSTANDING THE CODE
3rd Line - Commands
The third line is a command line: the command itself and
two arguments.
The command, echo, prints its arguments separated by a
single space and terminated with a newline.
2. PUT IT SOMEWHERE
THE SHELL CAN FIND IT
Before you turn the code into a script, you need to make
two decisions: what you will call the file and where you
will put it.
The name should be unique (that is, it should not
conflict with any other commands) and descriptive.
You should also put the script where the shell can find
it.
2. PUT IT SOMEWHERE
THE SHELL CAN FIND IT
Selecting a Directory for the Script
The "/bin" directory contains commands that may be
used by both the system administrator and by users.
For user or personal scripts, you can create your own
"bin" directory inside your home directory, e.g.
/home/bin
2. PUT IT SOMEWHERE
THE SHELL CAN FIND IT
Add Directory to the PATH variable
When the shell is given the name of a command to
execute, it looks for that name in the directories listed
in the PATH variable.
To be able to run the script from any location in the file
system, it has to be added to the system PATH.
2. PUT IT SOMEWHERE
THE SHELL CAN FIND IT
Add Directory to the PATH variable
To add directories to the system PATH, use the following
command, where directory is the name of the directory
you want to add:
export PATH=$PATH:directory
$ export PATH=$PATH:/home/bin
3. GIVE THE SHELL PERMISSION
TO EXECUTE IT
Setting Permissions
To be able to run a script by its name only, you need to
give the shell permission to execute the script:
chmod u+x bin/hw
Now the command can be run using just its name:
$ hw
Hello, World!
VARIABLES
A variable is a container for temporary storage of data.
There are two actions you may perform on variables:
Assigning a value to a variable.
Reading the value of a variable.
VARIABLE ASSIGNMENT
The most basic form of assigning values to variables
follows this pattern:
variablename=value
today=$(date)
files=$(ls)
USER INPUT
Another way of assigning values to variables is by asking
for user input and getting that input with read.