A Bash function is essentially a set of commands that can be called numerous times.
The purpose of a function is to help you make your bash scripts more readable and to avoid writing the same code repeatedly.
function function_name() {
your_commands
}
Example:
#!/bin/bash
function hello() {
echo "Hello World Function!"
}
hello
Website Hosting Script
#!/bin/bash
# Install nginx if not already installed
if ! command -v nginx &> /dev/null
then
echo "nginx not found. Installing..."
sudo apt-get update
sudo apt-get install nginx -y
fi
# Copy your website files to the nginx document root
sudo cp -r /path/to/your/website/* /var/www/html/
# Start nginx if not already running
if ! pgrep -x "nginx" > /dev/null
then
sudo service nginx start
echo "nginx started"
else
echo "nginx is already running"
fi
# Give Executable permission
chmod +x host_website.sh
# Run Script
./host_website.sh