Bash scripts if/else statements

Hello,

Can someone help me with this task? It is in the Linux level 4 practice. The question said The Nautilus DevOps team is working on to develop a bash script to automate some tasks. As per the requirements shared with the team database related tasks needed to be automated. Below you can find more details about the same: Write a bash script named /opt/scripts/database.sh on Database Server. The mariadb database server is already installed on this server. Add code in the script to perform some database related operations as per conditions given below: a. Create a new database named kodekloud_db01. If this database already exists on the server then script should print a message Database already exists and if the database does not exist then create the same and script should print Database kodekloud_db01 has been created. Further, create a user named kodekloud_roy and set its password to asdfgdsd, also give full access to this user on newly created database (remember to use wildcard host while creating the user). b. Now check if the database (if it was already there) already contains some data (tables)if so then script should print 'database is not empty otherwise import the database dump /opt/db_backups/db.sql and print imported database dump into kodekloud_db01 database. c. Take a mysql dump which should be named as kodekloud_db01.sql and save it under /opt/db_backups/ directory.

This is the script I used and I belive I successfully completed the task as you will see in the screenshot.

#!/bin/bash

DB_NAME=“kodekloud_db01”
DB_USER=“kodekloud_roy”
DB_PASS=“asdfgdsd”
DUMP_PATH=“/opt/db_backups/db.sql”
BACKUP_PATH=“/opt/db_backups/kodekloud_db01.sql”

Function to check if database exists

check_db_exists() {
DB_EXIST=$(mysql -u root -e “SHOW DATABASES LIKE ‘$DB_NAME’;” | grep “$DB_NAME”)
if [[ “$DB_EXIST” == “$DB_NAME” ]]; then
echo “Database already exists.”
return 1
else
return 0
fi
}

Function to create the database

create_db() {
echo “Creating database $DB_NAME…”
mysql -u root -e “CREATE DATABASE $DB_NAME;” 2>/dev/null
if [[ $? -eq 0 ]]; then
echo “Database $DB_NAME has been created.”
fi
}

Function to check if user exists

check_user_exists() {
USER_EXIST=$(mysql -u root -e “SELECT User FROM mysql.user WHERE User=‘$DB_USER’;” | grep “$DB_USER”)
if [[ “$USER_EXIST” == “$DB_USER” ]]; then
echo “User $DB_USER already exists.”
return 1
else
return 0
fi
}

Function to create the user and grant privileges

create_user_and_grant_privileges() {
echo “Creating user $DB_USER and granting privileges…”
mysql -u root -e “CREATE USER ‘$DB_USER’@‘%’ IDENTIFIED BY ‘$DB_PASS’;” 2>/dev/null
if [[ $? -eq 0 ]]; then
echo “User $DB_USER has been created.”
fi
mysql -u root -e “GRANT ALL PRIVILEGES ON $DB_NAME.* TO ‘$DB_USER’@‘%’; FLUSH PRIVILEGES;”
echo “Granted privileges to $DB_USER on $DB_NAME.”
}

Function to check if the database is empty (contains tables or not)

check_db_empty() {
TABLE_COUNT=$(mysql -u root -e “USE $DB_NAME; SHOW TABLES;” | wc -l)
if [ “$TABLE_COUNT” -gt 0 ]; then
echo “Database is not empty.”
return 1
else
return 0
fi
}

Function to import the dump if the database is empty

import_db_dump() {
if [[ -f “$DUMP_PATH” ]]; then
echo “Importing database dump into $DB_NAME…”
mysql -u root $DB_NAME < “$DUMP_PATH”
echo “Imported database dump into $DB_NAME database.”
else
echo “Database dump file not found.”
fi
}

Function to take a database backup

take_db_backup() {
echo “Taking backup of the database…”
mysqldump -u root $DB_NAME > “$BACKUP_PATH”
echo “Backup saved as $BACKUP_PATH.”
}

Main script execution

check_db_exists
if [[ $? -eq 0 ]]; then
create_db
fi

check_user_exists
if [[ $? -eq 0 ]]; then
create_user_and_grant_privileges
else
echo “Skipping user creation; user already exists.”
fi

check_db_empty
if [[ $? -eq 0 ]]; then
import_db_dump
fi

take_db_backup

The failure message indicates that the grader is trying to execute the script as

/opts/scripts/database.sh

which is subtly different from

bash /opts/scripts/database.sh

Perhaps all it is, is that your script is not executable
It needs

chmod +x database.sh