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 Creating Files with name alpha beta gamma
#!/bin/bash
for FILE in alpha beta gamma
do
echo "Creating file $FILE in system"
sudo touch $FILE
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
Functions¶
Certainly! Here's an example of how you can create a Bash script with a main function to call the check_empty_string
function:
#!/bin/bash
check_empty_string() {
if [ -z "$1" ]; then
echo "Error: Input string is empty!"
else
echo "Input string is not empty."
fi
}
main() {
input="example"
check_empty_string "$input"
}
# Call the main function
main
In this script, the main
function is defined to call the check_empty_string
function with the input
variable. You can replace "example"
with any input string you want to test. When you run the script, it will execute the main
function, which in turn calls the check_empty_string
function.
Sure, here are a few basic reusable Bash functions along with their use cases and examples:
Use Case: A function to greet a person.
greet() {
echo "Hello, $1!"
}
# Example usage
greet "Alice"
greet "Bob"
Use Case: A function to check if a number is even.
is_even() {
if (( $1 % 2 == 0 )); then
echo "Even"
else
echo "Odd"
fi
}
# Example usage
is_even 4 # Output: Even
is_even 7 # Output: Odd
Use Case: A function to display information about a file.
file_info() {
echo "File: $1"
echo "Size: $(du -h "$1" | cut -f1)"
echo "Owner: $(stat -c %U "$1")"
}
# Example usage
file_info "/path/to/somefile.txt"
Use Case: A function to calculate the sum of two numbers.
sum() {
echo "$(($1 + $2))"
}
# Example usage
result=$(sum 5 3)
echo "Sum: $result" # Output: Sum: 8
Use Case: A function to reverse a string.
reverse() {
echo "$1" | rev
}
# Example usage
reversed=$(reverse "hello")
echo "Reversed: $reversed" # Output: Reversed: olleh
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 rm -rf /var/www/html/*
sudo cp -r 2118_chilling_cafe/* /var/www/html/
cd ..
sudo rm -rf webfiles
sudo systemctl restart apache2
figlet done
#!/bin/bash
cd /etc/yum.repos.d/
sudo sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sudo sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
sudo yum update -y
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 rm -rf /var/www/html/*
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://templatemo.com/tm-zip-files-2020/templatemo_520_highway.zip
SRV=httpd
PKG=yum
FILE=templatemo_520_highway
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
echo "Cleaning files in html Directory"
sudo rm -rf /var/www/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 rm -rf /var/www/html/*
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 rm -rf /var/www/html/*
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
Info
Feel free to explore further templates on tooplate.com.