-1

Hi i want to use a simple "healthcheck" for docker in bash like this:

while ["$(docker exec -it ${mysql_name} mysql -u${mysql_user} -p${mysql_user_password} -e "SHOW DATABASES;" | grep ${mysql_name})" != ${mysql_name}]
do
   sleep 1
   echo "waiting for database..."
done

but i only get "mysql/test.sh: line 21: $'| nextcloud_db |\r': command not found". So it seems like bash is trying to output grep and I have no idea :(

"| nextcloud_db |" ist the output of the cmd in while

the single docker cmd + grep works

edit: maybe its better to check if grep is true? (and not this "!= ${mysql_name}"(btw. this will not working i guess, its not " string is not equal to var" its "string contains var"))

Ted Mosby
  • 79
  • 9
  • I also anticipate the same. Try in a single expression maybe ? – sb39 Aug 07 '21 at 06:59
  • 1
    okay until helps, its not perfect but it works for now... i will reply the solution later. OMG and i need a whitespace after [ and before ] – Ted Mosby Aug 07 '21 at 10:53

1 Answers1

0

So for other noobs :). Its important to set a whitespace after [ and ] before

while [ "$(grep ${mysql_name} <<< $(docker exec ${mysql_name} mysql -u${mysql_user} -p${mysql_user_password} -e 'SHOW DATABASES;'))" != ${mysql_name} ]
do
    echo "waiting for database..."
    sleep 5
done

its not perfect, because i get the first line of the docker cmd output (mysql: [Warning] Using a password on the command line interface can be insecure.) but it works for me (I hate that I said that...)

edit: the "" in the while are necessary to work correct?!

Ted Mosby
  • 79
  • 9
  • You could also use grep's exit status: `while grep -q hello <<< hello; do echo found; sleep 1; done` – Cole Tierney Aug 08 '21 at 11:54
  • @Cole Tierney thx, that's better! But the main problem was the brackets and spaces ... but I didn't find the problem and saw no choice but to ask – Ted Mosby Aug 08 '21 at 12:23