Skip to content

Shell Scripting

If Condition

#!/bin/bash

<Command>

if [ <condition> ]

then

    <Commands to Execute>
    <print Messages with echo>
fi
#!/bin/bash

<Command>

if [ <condition> ]

then

    <Commands to Execute>
    <print Messages with echo>

else
    <Commands to Execute>
    <print Messages with echo>

fi
#!/bin/bash

<Command>

if [ <condition> ]

then

    <Commands to Execute>
    <print Messages with echo>

elif [ <condition> ]
then

    <Commands to Execute>
    <print Messages with echo>

else
    <Commands to Execute>
    <print Messages with echo>

fi
#!/bin/bash

apt --help >> /dev/null

if [ $? -eq 0 ]

then
     echo " This is Ubuntu Operating System"
else
     echo " This is CentOS Operating System"
fi
Reference Link

How to program with Bash

https://opensource.com/article/19/10/programming-bash-logical-operators-shell-expansions

For Loop

#!/bin/bash

for <variable> in <list>
do
   <command>
done
#For loop example for adding users with name alpha beta gamma

#!/bin/bash

for USER in alpha beta gamma 
do
   echo "adding user $USER to system"

   sudo useradd $USER

done

While Loop

#!/bin/bash
while [ <condition> ]
do
   <command>
done
#!/bin/bash
#Here variable "a" is speed

a=0
echo "Starting the Engine"

while [ $a -le 100 ]
do
    sleep 1
    echo "Current Speed $a"

    a=$(($a+10))

done

Shell Script For Setting Up Website

#!/bin/bash
sudo apt update
sudo apt install wget net-tools unzip figlet apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
mkdir -p webfiles
cd webfiles
sudo wget https://www.tooplate.com/zip-templates/2118_chilling_cafe.zip
sudo unzip -o 2118_chilling_cafe.zip
sudo cp -r 2118_chilling_cafe/* /var/www/html/
cd ..
sudo rm -rf webfiles
sudo systemctl restart apache2
figlet done
#!/bin/bash
sudo yum install wget net-tools unzip httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
mkdir -p webfiles
cd webfiles
sudo wget https://www.tooplate.com/zip-templates/2118_chilling_cafe.zip
sudo unzip -o 2118_chilling_cafe.zip
sudo cp -r 2118_chilling_cafe/* /var/www/html/
cd ..
sudo rm -rf webfiles
sudo systemctl restart httpd
#!/bin/bash
#Website setup
#Adding variables :-)
URL=https://www.tooplate.com/zip-templates/2118_chilling_cafe.zip
SRV=httpd
PKG=yum
FILE=2118_chilling_cafe
echo "  Installing the Services & Extractors"
echo
sudo $PKG install $SRV wget unzip -y >> /dev/null
echo "Start & Enabling the Services"
echo
sudo systemctl start $SRV
sudo systemctl enable $SRV
echo "Downloading the zip file from tooplate.com"
echo
mkdir -p webfiles
cd webfiles
echo
sudo wget $URL >> /dev/null
echo "extracting the files "
echo
sudo unzip -o $FILE.zip >> /dev/null
echo "copying the extracted file into html"
echo
sudo cp -r $FILE/* /var/www/html >> /dev/null
echo "Restarting the Services"
sudo systemctl restart $SRV
cd ..
sudo rm -rf webfiles
sudo systemctl status $SRV | grep Active
date
#!/bin/bash
apt --help >> /dev/null
if [ $? -eq 0 ]
then 
   sudo apt update
   sudo apt install wget figlet net-tools unzip apache2 -y
   sudo systemctl start apache2
   sudo systemctl enable apache2
   mkdir -p webfiles
   cd webfiles
   sudo wget https://www.tooplate.com/zip-templates/2118_chilling_cafe.zip
   sudo unzip -o 2118_chilling_cafe.zip
   sudo cp -r 2118_chilling_cafe/* /var/www/html/
   cd ..
   sudo rm -rf webfiles
   sudo systemctl restart apache2
   sudo systemctl status apache2 | grep Active
figlet done
else
    sudo yum install wget net-tools unzip httpd -y
    sudo systemctl start httpd
    sudo systemctl enable httpd
    sudo mkdir -p webfiles
    cd webfiles
    sudo wget https://www.tooplate.com/zip-templates/2118_chilling_cafe.zip
    sudo unzip -o 2118_chilling_cafe.zip
    sudo cp -r 2118_chilling_cafe/* /var/www/html/
    cd ..    
    sudo rm -rf webfiles
    sudo systemctl restart httpd
    sudo systemctl status httpd | grep Active
fi