0% found this document useful (0 votes)
22 views25 pages

Beginner's Guide to Bash Scripting

The document provides an overview of Bash scripting, covering key concepts such as variable declaration, data types, conditional statements, loops, functions, and file handling. It includes examples for printing to the terminal, using arrays, and processing command-line arguments. Additionally, it introduces tools like awk and sed for text processing within Bash scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views25 pages

Beginner's Guide to Bash Scripting

The document provides an overview of Bash scripting, covering key concepts such as variable declaration, data types, conditional statements, loops, functions, and file handling. It includes examples for printing to the terminal, using arrays, and processing command-line arguments. Additionally, it introduces tools like awk and sed for text processing within Bash scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Bash Scripting

Bash
 Bourne Again Shell.
 It is a scripting language.
 allowing users to write sequences of
commands in a file, known as a script.
• File Extension: .sh
For example:
[Link]
Hello World in Bash
• To print something to the
terminal in Bash, you can use
the echo command.
Variables
• variable is a container used to store data or
values
Variable Declaration
 my_number=42

 echo "The value of my_number is


$my_number"
data types
 Supports mainly three data types.

• string
• integer
• array
String
 String: Strings are sequences of characters.

 Example: name="John"
Integer
 Bash can handle integer arithmetic.

 Example: age=25
Array
 Arrays are used to store multiple values under a
single variable.

 Example: numbers=(10 20 30)


If Statements
 if statements are used to conditionally execute a
block of code based on the success or failure of a
test command
 Example:

 number=10
 # Simple if statement
 if [ $number -eq 10 ]; then
 echo "The number is 10."
 fi
if-else Statements
 if-else statement allows you to execute different
blocks of code based on whether a condition is true
or false.
 Example:

 number=5
 # If-else statement
 if [ $number -eq 10 ]; then
 echo "The number is 10."
 else
 echo "The number is not 10."
Loops
 Loops in programming are control structures that
allow you to repeatedly execute a block of code.

 Why Loops?

 Loops help you execute a block of code repeatedly,


saving you from writing the same code over and
over.
Two common types of loops
 for Loop

 while Loop
For loop
 The for loop is used to iterate over a sequence, such
as a range of numbers or elements in an array.

 Syntax:

for variable in list


do
# Code to be executed for each iteration
done
while loop
 The while loop in Bash is used to repeatedly execute
a block of code as long as a specified condition is
true
 Syntax:

while [ condition ]
do
# Code to be executed while the condition is true
done
Function
 In Bash, a function is a block of code that can be
defined and executed within a script. Functions
provide a way to organize and reuse code, making
scripts more modular and maintainable
 Syntax:

function function_name {
# Code to be executed
}
# Call the function
function_name
Input and output
 The read command allows you to interactively take
input from the user.
 The echo command is used to print text to the
terminal.

# Example script (input_script.sh)


echo "Enter your name:"
read username
echo "Hello, $username!"
working with files
• Creating a File
• Writing to a File
• Appending to a File
• Reading a File
• Copying a File
• Moving/Renaming a File
• Deleting a File
working with directory
• Creating a Directory
• Listing Contents of a Directory
• Changing Directory
• Copying a Directory
• Moving/Renaming a Directory
• Deleting a Directory
• Checking File Types
awk
 In Bash, awk is used as a command-line tool for
processing and analyzing text data.
• Syntax:

awk 'pattern { action }' filename

 pattern: Specifies a condition or regular expression to


match lines.
 action: Specifies the code to be executed for lines
that match the pattern.
sed
 sed stands for "stream editor," and it is a powerful
and versatile text-processing tool in Unix-like
operating systems. sed is designed for parsing and
transforming text streams on the command line or in
scripts. It allows you to perform basic text
manipulations, such as search and replace, insertion,
deletion, and more.

 Syntax:

sed 'command' filename


Command-Line Arguments
 command-line arguments are values that you pass to
a script or a command when you run it from the
terminal.
 Example:

#!/bin/bash
echo "Script Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "Total number of arguments: $#"
echo "All arguments: $@"
Array
 In Bash, arrays are used to store lists of values.
 Elements are accessed using their index, starting
from 0.

 Syntax:

array_name=(value1 value2 value3 ... valueN)


Example
#!/bin/bash

# Declaring an array
fruits=("Apple" "Banana" "Orange" "Grapes")

# Accessing array elements


echo "First fruit: ${fruits[0]}"
echo "Second fruit: ${fruits[1]}"

# Length of the array


echo "Number of fruits: ${#fruits[@]}"
Thanks

Presentation by
Twinkle

You might also like