0% found this document useful (0 votes)
40 views

Chapter 9. Shell Scripting Basics

Uploaded by

AC Atelier
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Chapter 9. Shell Scripting Basics

Uploaded by

AC Atelier
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

CHAPTER 9

SHELL SCRIPTING
BASICS
TOPICS
01 WHAT IS A SHELL

02 WHAT IS A SHELL SCRIPT

03 WRITING A SHELL SCRIPT

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

There should be no space on either side of the


equals (=) sign.
Always make sure variable names are descriptive.
READING VARIABLE VALUE
To read the value of a variable, precede its name
with a $ sign.
var=Hello
anothervar=Anna

echo $var $anothervar


# this will print "Hello Anna"
READING VARIABLE VALUE
When assigning complex values to variables, you need to
make use of quotes.
Single quotes will treat every character literally.
Double quotes will allow you to do substitution.
READING VARIABLE VALUE
COMMAND SUBSTITUTION
Command substitution allows you to take the output of
a command (what would normally be printed to the
screen) and save it as the value of a variable.
To do this, put the command within brackets, preceded
by a $ sign.

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.

The read command reads from the standard input


(keyboard or file). By default, it reads until a newline is
received.
USER INPUT
The input is stored in one or more variables given as
arguments:
read var
If more than one variable is given, the first word (the
input up to the first space or tab) is assigned to the
first variable, the second word is assigned to the second
variable, and so on, with any leftover words assigned to
the last one.
READ USER INPUT EXAMPLE
echo "What are your favorite colors?:
read colors
echo "Your favorite colors are $colors."
READ USER INPUT EXAMPLE
echo "What are your favorite colors?:
read col1 col2 col3
echo "Your favorite colors are $col1, $col2, and $col3."
ARITHMETIC OPERATIONS
The most common way to use arithmetic expressions is
in arithmetic expansion. Arithmetic expansion evaluates
math expressions and returns the result.
The syntax is:
$((expression))
For example, the command echo $(( 3 + 4 * (5 - 1) ))
prints 19.
ARITHMETIC OPERATIONS
Examples:
Add two numbers and echo the result:
echo $((2+3)) #prints 5
Multiply two numbers and echo the result:
echo $((5*3)) #prints 15
Divide two numbers:
echo $((20/5)) #prints 4
ARITHMETIC EXPANSION

You might also like