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 9019907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:52:57+00:00 2026-06-16T04:52:57+00:00

I was thinking about implementing a state table in MySQL for several Bash scripts

  • 0

I was thinking about implementing a state table in MySQL for several Bash scripts to poll. The idea is that script #n is cleared to run once script #n-1 has finished running and has written its state and status into the state table in a MySQL db. Basically the state table would look like:

+----+-------+--------+
| id | state | status |
+----+-------+--------+
| 44 |     2 |   OK   |
| 45 |     3 |   OK   |
| .. |   ... |  ...   |
| 55 |     2 |   OK   |
+----+-------+--------+

The id column is auto_increment so that can be used to define the last row.

At the moment I’ve come up with:

#!/bin/bash
#...
VARIABLE=$(mysql mydbname --skip-column-names -s --local -e \
         "SELECT count(*) \
          FROM statetable \
          WHERE state=2 AND \
                status='OK' AND \
                id=(SELECT MAX(id) FROM statetable)")
if [ $VARIABLE -eq 1 ] ; then #...

Is there a right way to check if the last row in the table has the wanted state value and status pair (2 and OK in the example above) without a subquery and to pass that information to the Bash script?

ADDED SOLUTION:

#!/bin/bash
function check_state () {
    VARIABLE=$(mysql mydbname --skip-column-names -s --local -e \
             "SELECT (state=2) && (status='OK') \
              FROM statetable \
              ORDER BY id DESC \
              LIMIT 1;")
    return $VARIABLE
}

if ! check_state ; then #...
  • 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-16T04:52:58+00:00Added an answer on June 16, 2026 at 4:52 am

    In MySQL, you can select not only fields, but also expressions and operations on them. Somewhat surprisingly this works for logical tests as well, the kind we use, for example in a where clause. That means that you could use the following SQL to get a boolean (0 / 1) result:

    select (state = 2) && (status = 'OK')
    from statetable
    order by id desc
    limit 1
    

    You can try it out on this SQL Fiddle.

    Sorry that I did not understand the question at first, but as it seems everybody did 🙂

    The query you have written will most likely always return a value of 1, because it does not select the maximum id, it selects the number of rows with a maximum id, and if there is at least one row in the table, there is such a thing as a maximum id 🙂

    Assuming that you need the last ID, a better way (without a subquery) would be to order the data, and just take the first row, something like:

    SELECT id FROM statetable 
    WHERE state=2 
      AND status='OK' 
    ORDER BY ID DESC
    LIMIT 1
    

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

Sidebar

Related Questions

I'm thinking about a good solution for implementing a sign up/login system that works
I am thinking about implementing a program with finite state automaton in an OOP
I'm thinking about implementing Intense Debate in my Wordpress site. I know that ID
I was thinking about implementing an application in Java (with a GWT GUI) that
I am thinking about implementing a wrapper for MPI that imitates OpenMP's way of
I think I know C++ reasonably well and I am thinking about implementing something
Thinking about avoiding code replication, I got a question that catches me every time
Just thinking about the best way to build an Order form that would (from
Was thinking about implementing tearaway tabs on a Java project I'm working on. Wondering
I am currently thinking about implementing an application with NHibernate and I would like

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.