Thread: Backticks

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

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

    Backticks


    I have a command I need to run from a script, that requires backticks, but I'm not sure how to do that if I need to use backticks around the command itself too.

    So I need to run this mysql query (which REQUIRES the backticks around the word Database):

    Code:
    show databases WHERE `Database` LIKE "central\_%"
    Code:
    for database in `mysql -Bse 'show databases WHERE `Database` LIKE "central\_%" OR `Database` LIKE "facebook\_%"`
    do
      Something
    done
    Backticks within backticks don't work too well...

    Any suggestions?
    Thomas Tremain
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,406
    Rep Power
    1871
    > So I need to run this mysql query (which REQUIRES the backticks around the word Database):
    But backticks are shell magic.

    If your shell is bash, then this seems like it should work.
    Code:
    for database in $(mysql -Bse 'show databases WHERE $(Database) LIKE "central\_%" OR $(Database) LIKE "facebook\_%")
    do
      Something
    done
    But $(Database) gets evaluated twice, which might be a waste of effort.

    So perhaps
    Code:
    mydb=$(Database)
    for database in $(mysql -Bse 'show databases WHERE $mydb LIKE "central\_%" OR $mydb LIKE "facebook\_%")
    do
      Something
    done
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    I appreciate your efforts there, but that does not meet the requirements.

    The backticks around the word Database are NOT to be evaluated by bash, but need to be passed intact as part of the mysql query.
    Thomas Tremain
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,406
    Rep Power
    1871
    I see.

    Would something like this work?
    Code:
    function doit {
        mysql -Bse 'show databases WHERE `Database` LIKE "central\_%" OR `Database` LIKE "facebook\_%"'
    }
    
    for database in `doit` ; do 
        echo $database
    done

    Comments on this post

    • ttremain agrees : Thanks!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    Yes, that does work...

    So does this workaround:
    Code:
    for database in `mysql -Bse 'show databases'`
    do
      if [[ $database =~ ^(facebook|central)\_ ]]
      then
        do something 
      fi
    done
    The workaround doesn't solve the requirements, but does change them.
    Thomas Tremain

IMN logo majestic logo threadwatch logo seochat tools logo