2

This example works well:

if [[ "$(mysql -sse 'USE my_bd; SELECT COUNT(*) FROM my_table" -gt "0" ]]; then echo 1; else echo 0; fi 

But I need to add a WHERE clause that needs quotes ' inside mysql -e 'query...'

So I need to change the quotes order like this and enclose the mysql -e "query..." with double quotes

I have tried to escape with \"

if [[ "$(mysql -sse \"USE my_bd; SELECT COUNT(*) FROM my_table WHERE my_column = 'something'\"" -gt "0" ]]; then echo 1; else echo 0; fi 

But that has thrown a bash error

-bash: command substitution: line 21: syntax error near unexpected token `('

I saw other questions about How to escape double quotes in bash? but the answers don't work in my case

3
  • 2
    I don't see the $(mysql getting closed Commented Mar 11, 2018 at 13:06
  • You have reason if I change "$(mysql to $(mysql working good. Bfff I have tried a lot of combinations but not that one. Thank's The bellow answer also work. I need more coffee :) Commented Mar 11, 2018 at 13:11
  • 1
    Since the values on both sides is a number, it works without " ", but if the value is expected to be a string, it won't work Commented Mar 11, 2018 at 13:31

1 Answer 1

2

You can directly use the below command:

if [[ "$(mysql -sse "USE my_bd; SELECT COUNT(*) FROM my_table WHERE my_column = 'something')" -gt "0" ]]; then echo 1; else echo 0; fi 

I have just tested the above command in a test environment and it worked fine. See the below similar example:

 if [[ "$(mysql -sse "use testdb ; SELECT COUNT(*) from student where stu_id = '1001'")" -gt 0 ]]; then echo "1" ; else echo "0"; fi 1 
Sign up to request clarification or add additional context in comments.

3 Comments

You have also reason. I'm fill ridiculuos ;)
Please refer the Command Substitution section in man bash. "When using the $(command) form, all characters between the parentheses make up the command; none are treated specially."
Thank's. Is good to know that. (I have no study on informatics ... but I try to learn as I can )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.