Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:03:36+00:00 2026-06-10T06:03:36+00:00

I am trying to write a Bash script to run a MySQL query from

  • 0

I am trying to write a Bash script to run a MySQL query from a string constructed in the script. I have been able to construct the query string, and it looks correct when echo’d. And I can cut and paste it into the shell command line and it runs. But, I need to basically cut the query results to just the words, OK or CRITICAL. Nothing else. When I try to save the query result to a bash variable I get:

mysql: unknown option ‘-2’

If I cut out the grep and cut code, I get the mysql --help results. So, it’s not liking my mysql string, but I don’t know why. Help?

BTW- Running CentOS 6.3

alert=`mysql myDB -e "select value from config_table where name=\"ach_alert_time\"\G" | /bin/grep value: | /bin/cut -f2 -d:`
echo $alert  #Brings back 18,50,00

sql="mysql myDB -e 'select if(count(*) > 0,\"CRITICAL\",\"OK\") as STATUS from xyz_batch where timestamp > concat(date(now()),\" \",maketime("$alert"))\G' | /bin/grep OK  | /bin/cut -f2 -d:"
echo $sql # mysql myDB -e 'select if(count(*) > 0,"CRITICAL","OK") as STATUS from xyz_batch where timestamp > concat(date(now())," ",maketime( 18,50,00))\G' | /bin/grep OK | /bin/cut -f2 -d

status=`$sql`

# Whoops!
# mysql: unknown option '-2'

echo $status
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:03:37+00:00Added an answer on June 10, 2026 at 6:03 am

    The problem is that when you run $sql as a command, it under goes word-splitting, filename expansion, and so on, with no respect for any quotation-marks that $sql might contain. And it won’t handle pipes and other shell metacharacters. For example, if you’re in a directory that contains the files foo and bar, then this:

    cmd="echo '     *     ' | grep baz"
    $cmd
    

    will print ' bar foo ' | grep baz — it won’t preserve whitespace, it will expand the * into a list of files, and it will treat | and grep as arguments to echo.

    You can work around this by using the command eval "$sql" instead of just $sql:

    status=`eval "$sql"`
    

    but you may want to re-evaluate your design a bit. Maybe there’s a better way to achieve what you want?


    Edited to add: You say that you’re constructing the query in the script, but do you actually need to store the entire command in a variable? Something like this:

    sql="select if(count(*) > 0,\"CRITICAL\",\"OK\") as STATUS from xyz_batch where timestamp > concat(date(now()),\" \",maketime("$alert"))\G"
    status=`mysql myDB -e "$sql" | /bin/grep OK  | /bin/cut -f2 -d:`
    

    would not have this problem, since then $sql is then just a single argument, so you don’t need Bash to do anything special with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a bash script that looks at two files with
I'm trying to write a BASH script to get my Java program to run(common
I am trying to write a simple Bash script to monitor MySQL replication status.
I'm trying to write a bash script that looks at a directory full of
I am trying to write a bash script. I want to find files by
I'm trying to write a bash script that will process a list of files
I'm trying to write a bash script that wraps whatever the user wants to
I am trying to write a bash script(script.sh) to search and replace some variables
I am trying to write a bash or PHP script that will execute a
Im trying to write a bash script that executes several commands as different users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.