Bash scripts if/else statements: task validation issue

Hi @Tej-Singh-Rana
I believe all conditions are met, but I am still getting a failure for this task during validation. can someone from the team review it?


That’s one very wrong workflow in the script.
From a brief look at the script:
If the database does not exist, you create it and then do a MySQL dump.
What happened with the rest of the tasks you have to do in the newly created database?

Hello Mr @jetchko.jekov I’m facing same thing right here, can you please go through my screenshots and tell me where am wrong and how can I correct the mistakes.



@jetchko.jekov

@david-makanya
The workflow looks correct (if your checks in IF statements are correct).
I don’t know how exactly the final check works but from a fast look you are printing 'Database is not empty" while the task description says “database is not empty” (capital vs regular letter ‘D’)
Again, I am speculating because of the “… print messages are incorrect …” part of the check output.

Disclaimer: I am only reading the bash script, I haven’t done this task myself or at least I don’t remember doing it. I stopped using KKE a couple of years back. Currently, I am playing a bit with 2.0.

Hello @vijayyuvi did you manage to get answer for this task?

thank you alot Mr @jetchko.jekov it has worked. The problem was only spellings.

@jetchko.jekov Thanks for the insights, I fixed the workflow and was able to complete the task.

Hello @david-makanya !
Could you please tell how you fixed spelling errors. I have similar I guess:((

could someone fix the quote errors messages in this exercise??

#!/bin/bash

dbexists=$(mysql -e 'show databases;' | grep kodekloud_db01)
if [[ $dbexists == 'kodekloud_db01' ]] 
then 
  echo 'Database already exists'
  mysql -e 'CREATE USER IF NOT EXISTS `kodekloud_roy`@`%` IDENTIFIED BY "asdfgdsd";'
  mysql -e 'GRANT ALL ON kodekloud_db01.* TO `kodekloud_roy`@`%` WITH GRANT OPTION;'
  count=$(mysql -D kodekloud_db01 -e 'show tables;' | wc -l)
  if [[ $count == '0' ]] 
  then 
    mysql -D kodekloud_db01 -e "source /opt/db_backups/db.sql;"
    echo 'imported database dump into kodekloud_db01 database'
  else 
    echo 'database is not empty'
  fi
else 
  mysql -e 'create database kodekloud_db01;'
  echo 'Database kodekloud_db01 has been created'
  mysql -e 'CREATE USER IF NOT EXISTS kodekloud_roy`@`%` IDENTIFIED BY "asdfgdsd";'
  mysql -e 'GRANT ALL ON kodekloud_db01.* TO `kodekloud_roy`@`%` WITH GRANT OPTION;'
fi

mysqldump kodekloud_db01 > /opt/db_backups/kodekloud_db01.sql

I have this script, and the error shows

 - seems like '/opt/scripts/database.sh' script execution failed or print messages are incorrect

it is done.
it was my mistake into the steps
but the quotes must be fixed

perfect !this worked.

my script:

#!/bin/bash

DB_NAME=“kodekloud_db01”
DB_USER=“kodekloud_roy”
DB_USER_PASS=“asdfgdsd”

if mysql -u root -e “use $DB_NAME” 2>/dev/null; then
echo “Database already exists”
else
mysql -u root -e “create database $DB_NAME”
echo “Database $DB_NAME has been created”

mysql -u root -e “CREATE USER ‘$DB_USER’@‘%’ IDENTIFIED BY ‘$DB_PASS’;”
mysql -u root -e “GRANT ALL PRIVILEGES ON $DB_NAME.* TO ‘$DB_USER’@‘%’;”
fi

if mysql -u root $DB_NAME -e “SHOW TABLES” | grep -q “.”; then
echo “database is not empty”
else
mysql -u root $DB_NAME < /opt/db_backups/db.sql
echo “imported database dump into $DB_NAME database.”
fi

mysqldump -u root $DB_NAME > /opt/db_backups/$DB_NAME.sql

btw, there are some formatting typos in the description

Please use code blocks when pasting code - it looks nicer and preserves the formatting.