0

I have the following function

function executeMySql() {
  while IFS= read -p "$prompt" -r -s -n 1 char
  do
      if [[ $char == $'\0' ]]; then
          break
      fi
      if [[ $char == $'\177' ]];  then
          prompt=$'\b \b'
          password="${password%?}"
      else
          prompt='*'
          password+="$char"
      fi
  done
  mysql -u root -p$password -e "$1"
  ret=$?
  if [ $ret = "0" ]; then
    # Show success message
    printf "\e[32m\nSUCCESS: $2\n\n"
    tput sgr0
  else
      echo "Wrong password"
  fi
}

and I use it like this

echo -e "Enter your mysql \$root password to create the db"
executeMySql "CREATE DATABASE IF NOT EXISTS Test" "Test db ready"
{
  touch /var/moo.txt
} || {
  # ...
}

But if user enters wrong mysql password he receives Wrong password message and the script continues but what I want is to loop the user password attempts until it's successful and then go on with rest of the script execution.

Any idea how to achieve that?

2 Answers 2

1

The mysql call isn't within a loop, so it will be called only once, whether it succeeds or not.

You can either make sure you return $ret at the end of executeMySql and call it using:

while ! executeMySql ...; do :; done

or you can modify executeMySql so that the mysql call is within the existing loop:-

function executeMySql() {
  while IFS= read -p "$prompt" -r -s -n 1 char
  do
      if [[ $char == $'\177' ]];  then
          prompt=$'\b \b'
          password="${password%?}"
          continue
      elif [[ $char == $'\0' ]]; then
          prompt='*'
          password+="$char"
          continue
      fi
      # Drop through when $char == $'\0'
      mysql -u root -p$password -e "$1"
      ret=$?
      if [ $ret = "0" ]; then
        # Show success message
        printf "\e[32m\nSUCCESS: $2\n\n"
        tput sgr0
        return $ret
      else
          echo "Wrong password"
      fi
  done
}

I have desk-checked the code, but I cannot easily do a code test, so I hope I have modified it correctly, though if not it should be a sufficient guide to what needs doing.

You could alternatively add an enclosing loop ret=1; while $ret != 0 ... within executeMySql, which is possibly clumsier, but needs fewer code changes.

1
  • It's not working but indeed helped me to make it work 10x
    – Jocky Doe
    Sep 25, 2018 at 14:27
0

Here's the working version of the function

function executeMySql() {
  local password
  local prompt
  local char
  while IFS= read -p "$prompt" -r -s -n 1 char
  do
      if [[ $char == $'\0' ]]; then
          break
      elif [[ $char == $'\177' ]];  then
          prompt=$'\b \b'
          password="${password%?}"
      else
          prompt='*'
          password+="$char"
      fi
  done
  mysql -u root -p$password -e "$1"
  ret=$?
  if [ $ret = "0" ]; then
    # Show success message
    echo -e "SUCCESS: $2"
  else
      echo -e "Wrong password try again"
  fi
  return $ret;
}

usage example

echo -e "Enter your mysql \$root password to create the db"
while ! executeMySql "CREATE DATABASE IF NOT EXISTS Test" "Test db ready"; do :; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .