Scripting Types
- We create scripts for two major usecases
- a person executing it (should inform user about the activites)
- automated execution (should not ask for user interaction)
- For DevOps/Cloud related activities we need automated execution
More on Scripting
- According to last classes, we have a script to install jenkins
#!/bin/bash
apt update
apt install openjdk-17-jdk -y
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
apt-get update
apt-get install jenkins -y
- Lets try to look at this script in a different way by drawing a flowchart

- Lets try to draw a flowchart which will install jenkins on ubuntu and redhat systems

- Below is the script to install jenkins on redhat as well as ubuntu systems
#!/bin/bash
distribution=$(. /etc/os-release && echo $ID)
echo $distribution
# for ubuntu
if [[ $distribution == "ubuntu" ]]
then
apt update
apt install openjdk-17-jdk -y
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
apt-get update
apt-get install jenkins -y
elif [[ $distribution == "rhel" ]]
then
sudo yum install wget -y
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum upgrade
# Add required dependencies for the jenkins package
sudo yum install java-11-openjdk
sudo yum install jenkins
sudo systemctl daemon-reload
else
echo "This script runs only on ubuntu and redhat"
fi
Expression
- An expression is combination of operands and operators which return some value
Operators in bash
Conditional statements
- We have 4 conditional statements in shell
- if
- if else
- if elif else
- switch
- Conditional statements require boolean operators
References
Installations
Exercise
- Write a shell script to do the following
- if the os is redhat
- install jenkins with jdk 11
- install awscli
- install azure cli
- if the os is ubuntu
- install jenkins with jdk 17
- install awscli
- install azurecli