1

I have the following bash script:

MYSQL="which mysql"
SQL="CREATE DATABASE IF NOT EXISTS TEST;"

read -p -s "Enter mySQL password:" DBPASS

$MYSQL -root -p$DBPASS -e "$SQL"
if [ "$?" -eq 0 ]; 
then
   echo "Failed."
else
   echo "Done."
fi

Even if I type wrong password the script says "Done.", as it can´t detect the mysql command error...

I need to be able to correctly show the "Error." message when someone types a wrong password.

Help appreciated.

6
  • Perhaps using expect - expect.sourceforge.net - would be a better bet Commented Oct 31, 2015 at 11:08
  • 1
    Replace MYSQL="which mysql" by MYSQL="$(which mysql)". Commented Oct 31, 2015 at 11:15
  • $(which mysql)did not solved the problem... Commented Oct 31, 2015 at 11:18
  • It is the first of two bugs. Commented Oct 31, 2015 at 11:19
  • if "which mysql" found mysql so it is not necessary to do it. mysql also search in the $PATH and if you want to use you put it in backticks MYSQL=which mysql and the MySQL username must after -u. so -uroot Commented Oct 31, 2015 at 12:14

1 Answer 1

5

$? == 0 means the command run successfully.

try this:

MYSQL="which mysql"
SQL="CREATE DATABASE IF NOT EXISTS TEST;"

read -p -s "Enter mySQL password:" DBPASS

$MYSQL -root -p$DBPASS -e "$SQL"
if [ "$?" -eq 0 ]; 
then
   echo "Done."
else
   echo "Failed."
fi
Sign up to request clarification or add additional context in comments.

1 Comment

use: export phppath=$(which php) echo $phppath and add a newline before the echo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.