#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    396
    Rep Power
    189

    Without backticks?


    Is there a way to do this without using backticks?

    Code:
    show databases WHERE `Database` LIKE "central\_%"
    I need to include this in a linux command line like:

    Code:
    for database in `mysql -Bse 'show databases WHERE `Database` LIKE "central\_%" OR `Database` LIKE "facebook\_%"`
    do
      Something
    done
    However backticks within backticks don't work too well...
    Thomas Tremain
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,027
    Rep Power
    4210
    Originally Posted by ttremain
    Is there a way to do this without using backticks?
    yes, there is

    don't use backticks

    r937.com | rudy.ca
    please visit Simply SQL and buy my book
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    396
    Rep Power
    189
    Originally Posted by r937
    yes, there is

    don't use backticks

    I've tried the same query without them and it doesn't work.
    Thomas Tremain
  6. #4
  7. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,027
    Rep Power
    4210
    well, i guess i should have tested that myself first

    sorry

    yes, you need the backticks around the `Database` column name produced by the SHOW command

    as for the backticks around the linux command, i'm sorry, i don't do linux, but i'd be ~very~ suprised if the backticks were legal there
    r937.com | rudy.ca
    please visit Simply SQL and buy my book
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    396
    Rep Power
    189
    Sinec there isn't a way to do it by modifying the mySQL command itself, I've just posted the same question in the Linux forum, to see if there is a way on that part of the puzzle.

    I'm hoping there is some fancy workaround that I have not considered (or had no idea could be done)

    Thanks
    Thomas Tremain
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    Have you tried quoting them?

    Code:
    show databases WHERE \`Database\` LIKE "central\_%"
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    396
    Rep Power
    189
    Originally Posted by SimonJM
    Have you tried quoting them?

    Code:
    show databases WHERE \`Database\` LIKE "central\_%"
    Yes, I tried that.

    I found my solution (actually two solutions) by modifying the Bash script itself, instead of altering the mySQL query.

    There is an additional thread, with solutions, in the Linux forum here:
    http://forums.devshed.com/linux-help-33/backticks-934843.html

    Thank you!
    Thomas Tremain
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    It worked for me ...

    Code:
    ehb@phalanx:~/Desktop$ mysql -uroot -pXXXXXXX -Be "show databases where \`database\` like 'm%';"
    Database
    mysql
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    396
    Rep Power
    189
    Originally Posted by SimonJM
    It worked for me ...
    It does, until you wrap THAT with the required Linux code that ALSO includes backticks.

    Remember, the actual problem consisted of having to wrap that in

    Code:
    for database in `mysql -uroot -pXXXXXXX -Be "show databases where \`database\` like 'm%';"`
    Thomas Tremain
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Backticks are deprecated. Use $( command ) instead.
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    I seem to recall having used nested backticks before, but it was a mess, and it was for nested commands, not delimiting a name - though I do not think that should matter.
    As b49P23TIvg says, however, you should really replace the command quotes of `...` with $(...). Apart from backticks being deprecated I, personally, find the $(...) syntax far easier to read.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    396
    Rep Power
    189
    I hope people don't find this too confusing...

    This original thread was posted in the mySQL forum, then a second thread in the linux forum to tackle it from another direction.

    It looks like this thread was later moved to the linux forum when the direction changed...

    I hope that clears some things up a bit.

    One of the last solutions was this:

    Code:
    function doit {
        mysql -Bse 'show databases WHERE `Database` LIKE "central\_%" OR `Database` LIKE "facebook\_%"'
    }
    
    for database in `doit` ; do 
        echo $database
    done
    I also can do it this way by changing the requirements just a bit:

    Code:
    for database in `mysql -Bse 'show databases'`
    do
      if [[ $database =~ ^(facebook|central)\_ ]]
      then
        do something 
      fi
    done


    I assume the later can be modified using the $(...) syntax???

    Comments on this post

    • b49P23TIvg agrees : Functions, what a thought. Great.
    Thomas Tremain
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    It sure can ...
    Code:
    ehb@phalanx:~$ for db in $(mysql -uroot -pXXXXXX -Bse "show databases where \`database\` like '%';")
    do
      echo "Database is: " $db
    done
    Produces output like:
    Code:
    Database is:  information_schema
    Database is:  Test
    Database is:  VampireHalls
    Database is:  mysql
    Last edited by SimonJM; November 29th, 2012 at 08:07 AM.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo