KEMBAR78
BASH Scripting | PDF | Control Flow | Scripting Language
0% found this document useful (0 votes)
42 views25 pages

BASH Scripting

The document outlines an introduction to scripting, specifically focusing on Bash scripting, including session guidelines, prerequisites, and topics covered. Key concepts include system variables, exporting variables, decision-making with if/else statements, and loops, particularly while loops. The session also provides resources for further learning and concludes with a Q&A segment.

Uploaded by

junaidahmed2198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views25 pages

BASH Scripting

The document outlines an introduction to scripting, specifically focusing on Bash scripting, including session guidelines, prerequisites, and topics covered. Key concepts include system variables, exporting variables, decision-making with if/else statements, and loops, particularly while loops. The session also provides resources for further learning and concludes with a Q&A segment.

Uploaded by

junaidahmed2198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Introduction to Scripting

By: Divya Shukla


⥤ Session Guidelines and pre-requisites

● Please be ready with your Linux/Ubuntu terminal before the session so


that you can try hands-on
● Please create a GitHub account and install git in your system.
● Last 30 mins of the session would be dedicated to doubt clearing for the
current session and past sessions
● Keep the non-technical questions for the end of the session
● Prefer asking any assignment related question at the end of the session
or in the doubt-classes

2
What we covered in last class:
● Introduction to Bash Scripting
● Sample script
● Variables
● Command Line Arguments

3
What we will cover today?
● System Variables
● Exporting the variables
● User input Decision Making
● Loops :While Loops
● Writing the shell script to host the website

4
Compiler and interpreter
A compiler is a software program that
translates the entire source code of a
high-level programming language into
machine code or an intermediate code all
at once, generating a standalone
executable file.
In contrast, an interpreter reads and
executes source code line by line,
translating and executing it in real-time
without generating a separate compiled
output, making it suitable for languages
like Python and JavaScript.

5
6
7
Scripting
Scripting refers to the process of writing
and executing scripts, which are
sequences of instructions or commands
written in a scripting language. These
scripts are typically used to automate
tasks, perform specific actions, or
manipulate data within a computer
program or system. Scripting languages
are often interpreted rather than
compiled, making them more accessible
and flexible for tasks that don't require the
same level of performance as compiled
languages.
8
Bash Scripting
Bash scripting refers to the process of
writing and running scripts using the Bash
(Bourne Again SHell) command-line
interpreter on Unix-like operating systems,
including Linux and macOS. Bash is a
widely used shell in the Unix and Linux
ecosystem, and it provides a powerful
environment for interacting with the
system, automating tasks, and
performing various system administration
tasks.

9
]Variable
Variables are symbolic names used to
represent data in computer programs.
They store values that can change
during the execution of a program,
allowing for dynamic storage and
manipulation of information. These
values can be of various types, such as
numbers, text, or more complex data
structures. Variables are crucial for
storing and managing data, enabling
programs to perform calculations,
make decisions, and respond to user
input effectively.

10
Sample Bash script

#!/bin/bash
# This is a simple Bash script that prints a greeting
echo "Hello, World!"

11
Scripting
Continued….

12
System Variables

● System variables are also known as


environment variables.
● These are special variables that store
information about the system or the
environment in which your script is running.
● These variables can be accessed and
manipulated to gather information or
configure your script's behavior based on the
current environment.

13
Common System Variables

● $HOME: This variable stores the path to the


current user's home directory.

● $USER or $LOGNAME: These variables store


the name of the currently logged-in user.

● $PATH: The PATH variable contains a list of


directories where executable files are located.
When you enter a command in the terminal,
the system searches for that command in the
directories listed in the PATH variable.

14
Common System Variables

● $PWD: It holds the present working


directory, which is the directory your script
or terminal session is currently in.
● $SHELL: This variable contains the path to
the default shell for the current user.
● $TERM: It specifies the terminal type or
emulation.
● $LANG: This variable defines the default
language and localization settings for the
user.

15
Common System Variables
● $HOSTNAME: It stores the hostname of
the system.
● $OSTYPE: This variable indicates the
type of operating system.
● $UID: It contains the user ID of the
current user.
● $EUID: This variable stores the effective
user ID of the current user.
● $GROUPS: It lists the groups the user
belongs to.

16
Common System Variables

Usage:

echo "Home directory: $HOME"


echo "Current user: $USER"
echo "Current working directory: $PWD"

17
Exporting Variables in BASH

Exporting variables in a Bash script means making


a local variable available to child processes,
including other scripts or commands launched from
your script. When you export a variable, it becomes
part of the environment for the child processes.
This is useful when you want to pass information
from your script to subprocesses or make
environment variables accessible to other parts of
your system.
To export a variable in Bash, you use the export
keyword followed by the variable name.

export VARIABLE_NAME="variable_value"

18
If/Else in BASH

● In Bash scripting, decision making involves


using conditional statements like `if`, `elif`
(else if), and `case` to control the flow of your
script based on certain conditions.
● With `if` statements, you can execute code
when a condition is true and use `else` to
handle the false case.
● `elif` lets you check multiple conditions in
sequence.
● The `case` statement is handy for comparing a
variable against different values or patterns.

19
Loops in BASH

In Bash scripting, loops are used to repeatedly


execute a block of code as long as a certain
condition is true or for a specified number of
iterations. Bash supports several types of loops,
including the for loop, while loop, and until loop,
which serve different purposes and are used in
various scenarios.

20
While loop in BASH

A while loop in Bash is a control structure that


allows you to repeatedly execute a block of code
as long as a specified condition remains true. It
provides a way to create loops that continue
executing until a particular condition is met.
Here's the basic syntax of a while loop in Bash:

while [ condition ]
do
# Code to execute while the condition is true
done

21
While loop in BASH

A while loop in Bash is a control structure that


allows you to repeatedly execute a block of code
as long as a specified condition remains true. It
provides a way to create loops that continue
executing until a particular condition is met.
Here's the basic syntax of a while loop in Bash:

while [ condition ]
do
# Code to execute while the condition is true
done

22
Lets learn to host a website using BASH

23
Resources

● BASH script tutorial

● BASH beginner's guide

● https://exercism.org/tracks/bash - Practice bash for free

● https://mywiki.wooledge.org/BashGuide bash guide

24
Let’s answer your
questions now!

Thanks for attending the lecture :)

25

You might also like