Skip to content

Latest commit

 

History

History
129 lines (81 loc) · 3.2 KB

File metadata and controls

129 lines (81 loc) · 3.2 KB

Course Objectives:

  • Understand the basics of Bash scripting.
  • Learn to write and execute Bash scripts to automate tasks.
  • Gain proficiency in using Bash for system administration and DevOps tasks.
  • Develop skills to create robust and reusable scripts for common DevOps scenarios.

Prerequisites:

  • Basic familiarity with Linux command line.
  • Understanding of fundamental DevOps concepts.

In previous lessons we have already learned about different Linux commands, “pw, ls, cd, vi, rm, cp. 
In this lesson we are going to learn how to use those commands and write scripts.
What is script or scripting in computer context?
It refers to writing programs or sequence of commands (scripts) that automate the execution of tasks. Unlike compiled languages such as C++ or Java scripts 
So don’t worry, in our course we are not going to learn any programing language but very close to it. 

Slide 1

1- Open text editor

vi script_file.sh

2- Add first line for your script. #!/bin/bash

Shebang (#!/bin/bash) specifies the script's interpreter.

Slide 2

Variables

In Bash scripting, variables are used to store data that can be manipulated or referenced throughout the script.

i.e

#!/bin/bash

variable_1=10 # We informed our script variable_1 actually is 10, so where ever we use $variable_1 it means this is equal to value 10.

variable_2=apple # Similar like variable_1 now variable_2 is defined or preset to apple.

Slide 3

1- Let's learn how can we use variables,

We are going to write a script that can read the ip address of our Linux machine,

Prevoiusly we learned how to print network interfaces IP Address with "ifconfig" command

We learned when we execute ifconfig command it prints all details including MAC address etc, but now let's consider we just want to print IP address of the machine and store that IP address into a variable. We store IP address in a variable and later we use variable to define IP address in some configuration.

#!/bin/bash

Assuming the IP address is stored in ifcfg-eth0 file

IP_ADDRESS=$(grep 'IPADDR=' /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F= '{print $2}') echo "IP Address: $IP_ADDRESS"

Slide 4

Introduction to if and else Conditions in Bash Scripting

  • Understand how to use if and else statements for decision-making in Bash scripts.
  • In previous slide we learned about variable , now we learn more use cases where the variables are going to use further.
  • Let's take an example if we are writing a script. In scripts there are some actions requird some pre-reqs like if we have 10 apples then our output should be 10
apple=10
if [ $apple -eq 10 ]
then
 echo $apple apples 
 else
    echo "we have $apples more then what we need"
fi

Slide 5

For loop

  • Learn how to use the for loop to iterate through a list of items in Bash scripts.
#!/bin/bash

for name in Alice Bob Charlie David
do
            echo "Hello, $name!"
    done


 a="a b c"

 for x in $a
 do
         echo $x + 1
 done

Slide 6

  • Efficent Scripting
  • Use functions
  • Why functions are important in scripting ( Reuseability )
#!/bin/bash

# Function definition
function alpha_name() {
    echo "Hello"
}

# Calling the function
alpha_name