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

  • Home
  • SEARCH
  • 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 8707933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:59:17+00:00 2026-06-13T03:59:17+00:00

The following command works fine when used on a terminal // Terminal mysql -h

  • 0

The following command works fine when used on a terminal

// Terminal

mysql -h localhost -u root my_database -e "select count(*) from page;"

But when used in a groovy script (via groovyconsole) it fails to execute. Instead the mysql usage options are printed, as if some unknown command was passed to mysql.

// Groovy Console

def p1 = 'mysql -h localhost -u root my_database -e "select count(*) from page;"'.execute()
p1.text

Any one know what’s up with that?

  • 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-13T03:59:18+00:00Added an answer on June 13, 2026 at 3:59 am

    Not sure why it’s falling over, my guess is it is the way mysql is handling the select call in quotes…

    As a workaround, this works:

    [ 'mysql', 
      '-h', 'localhost',
      '-u', 'root',
      'my_database',
      '-e', 'select count(*) from page' ].execute().text
    

    A nicer way of handling the output is to consume it (rather than use text) as this should alleviate any blocking issues should your buffers fill up….

    Something like:

    String output = new StringWriter().with { writer ->
      [ 
        'mysql', 
        '-h', 'localhost',
        '-u', 'root',
        'my_database',
        '-e', 'select count(*) from page'
      ].execute().with { proc ->
        consumeProcessOutput( writer, writer )
        waitFor()
      }
      writer.toString() 
    }
    
    println output
    

    Edit:

    Of course, you can always just use JDBC:

    @GrabConfig(systemClassLoader=true)
    @Grab('mysql:mysql-connector-java:5.1.21')
    import groovy.sql.Sql
    
    def rowcount = Sql.newInstance( 'jdbc:mysql://localhost/my_database', 'root', '', 'com.mysql.jdbc.Driver' ).with { sql ->
      def count = sql.firstRow( 'select count(*) from page' )[ 0 ]
      sql.close()
      count
    }
    
    println rowcount
    

    Explanation

    If you write a shell script like this (and save it as /tmp/run.sh):

    #!/bin/bash
    
    for var in "$@"
    do
      echo "$var"
    done
    

    Then, when you run:

    println( '/tmp/run.sh "select count(*) from page"'.execute().text )
    

    groovy calls the simple form of Runtime.getRuntime.exec(), and it prints out:

    "select
    count(*)
    from
    page;"
    

    As you can see, the parameters get all messed up, as it separates the select bit into words.

    When you instead call:

    println( [ '/tmp/run.sh', '"select count(*) from page"' ].execute().text )
    

    It prints out:

    "select count(*) from page"
    

    As groovy calls the String[] form of Runtime.exec so java doesn’t have to guess what the different parameters are, so keeps the select all in one parameter.

    Hope this explains it 🙂

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

Sidebar

Related Questions

When I use the following code in command prompt, it works fine. But when
If column Y contains only positive values, the following awk command works fine: $
The following works fine in iOS 4.3 but throws a EXC_BAD_ACCESS in iOS5. if(mainTable
I have the following awk command which works perfectly: awk 'BEGIN { ORS =
I added following command to Sessions -> Startup program but it didn't work. I'm
Following command will let me know the names of databases. $ mysqlshow But how
I am running the following command from my home directory: python -m CGIHTTPServer This
I have used mercurial's bfiles extension for some time and it works fine. The
I have the following query: SELECT * FROM versions WHERE path LIKE '%/dir1%' that
The following code works fine #!/bin/bash while true; do echo $(pwd | awk '{print

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.