DevOps 6.0: Bash Scripting ๐โจ
"DevOps 6.0: Bash Scripting - Unleashing the Power of Automation ๐๐ป"
What is bash scripting?
Bash scripting is a method of automating tasks on Unix-like operating systems using the Bash shell. It involves writing sequences of commands in a script to streamline and automate repetitive tasks, making system administration and other routine processes more efficient.
Let's create a Script:
Steps:
Create directory bashscripts.
Copy the below Vagrantfile to it
Vagrant.configure("2") do |config| config.vm.define "scriptbox" do |scriptbox| scriptbox.vm.box = "jacobw/fedora35-arm64" scriptbox.vm.network "private_network", ip: "192.168.56.26" scriptbox.vm.hostname = "scriptbox" scriptbox.vm.provider "vmware_desktop" do |v| v.allowlist_verified = true v.ssh_info_public = true v.gui = true end scriptbox.vm.provision "shell", inline: <<-SHELL mv /etc/yum.repos.d/fedora-updates.repo /tmp/ mv /etc/yum.repos.d/fedora-updates-modular.repo /tmp/ yum clean all yum update systemctl stop firewalld SHELL end config.vm.define "web01" do |web01| web01.vm.box = "jacobw/fedora35-arm64" web01.vm.network "private_network", ip: "192.168.56.27" web01.vm.hostname = "web01" web01.vm.provider "vmware_desktop" do |v| v.allowlist_verified = true v.ssh_info_public = true v.gui = true end web01.vm.provision "shell", inline: <<-SHELL mv /etc/yum.repos.d/fedora-updates.repo /tmp/ mv /etc/yum.repos.d/fedora-updates-modular.repo /tmp/ yum clean all yum update systemctl stop firewalld SHELL end config.vm.define "web02" do |web02| web02.vm.box = "jacobw/fedora35-arm64" web02.vm.network "private_network", ip: "192.168.56.28" web02.vm.hostname = "web02" web02.vm.provider "vmware_desktop" do |v| v.allowlist_verified = true v.ssh_info_public = true v.gui = true end web02.vm.provision "shell", inline: <<-SHELL mv /etc/yum.repos.d/fedora-updates.repo /tmp/ mv /etc/yum.repos.d/fedora-updates-modular.repo /tmp/ yum clean all yum update systemctl stop firewalld SHELL end config.vm.define "web03" do |web03| web03.vm.box = "spox/ubuntu-arm" web03.vm.box_version = "1.0.0" web03.vm.network "private_network", ip: "192.168.56.29" web03.vm.hostname = "web03" web03.vm.provider "vmware_desktop" do |v| v.allowlist_verified = true v.ssh_info_public = true v.gui = true end end end
Do
vagrant up scriptbox
andvagrant ssh
to login.Do
sudo -i
to go to root user.Create a directory using
mkdir /opt/scripts
.cd into it using
cd /opt/scripts
.Do
vim firstscript.sh
. sh is the extension for shell script but it is not necessary.Enter the below script into it -
#! /bin/bash # The above line is called a shebang, indicating that the script should be interpreted using the Bash shell. echo "Welcome to bash script" # Print a welcome message to the console. echo # Print an empty line for better readability in the console. echo "The uptime of the system is: " uptime # Display the system uptime using the 'uptime' command. echo "Memory Utilization" free -m # Display memory utilization using the 'free' command with the '-m' option to show results in megabytes. echo "Disk Utilization" df -h # Display disk utilization using the 'df' command with the '-h' option to show results in a human-readable format.
Do
./firstscript.sh
you will get permission denied because it does not have execute permissionSo give permission using
chmod +x firstscript.sh
.Run
./firstscript.sh
to see the output.
Setup a website using script:
Steps:
Create the script using
vim /opt/scripts/websetup.sh
.Copy the below contents in the script -
#! /bin/bash ######## Install required packages ######## sudo yum install wget unzip httpd -y ######## Start and enable Apache HTTP server ######## sudo systemctl start httpd sudo systemctl enable httpd ######## Create a temporary directory for web files ######## mkdir -p /tmp/webfiles cd /tmp/webfiles ######## Download and extract a web template ######## wget https://www.tooplate.com/zip-templates/2136_kool_form_pack.zip unzip 2136_kool_form_pack ######## Copy the extracted files to the web server's directory ######## sudo cp -r 2136_kool_form_pack/* /var/www/html/ ######## Restart the Apache HTTP server to apply changes ######## sudo systemctl restart httpd ######## Clean up: Remove the temporary directory ######## rm -rf /tmp/webfiles
Execute using
/opt/scripts/websetup.sh
.Get the IP address of the VM and paste it on the browser to see the website live.