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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:33:55+00:00 2026-05-26T02:33:55+00:00

I have a simple database query I wrapped in my bash script. In case

  • 0

I have a simple database query I wrapped in my bash script.

In case my query retrieves one row, I want to do several things. If my query retrieves zero rows, I want to terminate my script gracefully.

What is the best way to check if my script retrieves zero rows? Can you please provide some pointers?

Thank you.

  • 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-05-26T02:33:55+00:00Added an answer on May 26, 2026 at 2:33 am

    Here’s a working bash script to get number of rows from the DB. Just pass the variables and replace FROM part of the sql with yours.

    #!/bin/bash
    NUMROWS=`$ORACLE_HOME/bin/sqlplus -s $DBUSER/$DBPASSWORDd@//$DBHOST:$DBPORT/$ORACLE_SID   << EOF
    WHENEVER SQLERROR EXIT FAILURE ROLLBACK
    SET HEADING OFF
    SET FEEDBACK OFF
    SET PAGES 0
    select count(*) from dual;
    exit success
    EOF`
    echo $NUMROWS
    

    HINT:
    Replace the last line echo $NUMROWS with exit $NUMROWS and your script will exit gracefully if there are no records. Wrap it in a bash if statement if you want to do other things, as below.

    if [ $NUMROWS -lt 1 ]; then
      #exit gracefully
      exit 0; 
    fi
    
    # do something
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple query using JDBC of an Oracle database which is proving
I've been pulling my hair out. I have a very simple postgre database, one
I'm writing a semi-simple database wrapper class and want to have a fetching method
i have a simple database tree with parentid and i want to read the
I have a quite simple database query: Query q = new Query(person); q.addFilter(name, Query.FilterOperator.EQUAL,
I have a simple database connection: $rowdata = mysql_query(SELECT * FROM table_names); while($row =
I'm trying to run the following PHP script to do a simple database query:
I have one object being the result of a database query, looking something like
I have a simple database table called Entries: class CreateEntries < ActiveRecord::Migration def self.up
I have a simple database with two tables. Users and Configurations. A user has

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.