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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:12:09+00:00 2026-06-09T19:12:09+00:00

Below is my shell script from which I am trying to invoke few hive

  • 0

Below is my shell script from which I am trying to invoke few hive SQL queries which is working fine.

#!/bin/bash

DATE_YEST_FORMAT1=`perl -e 'use POSIX qw(strftime); print strftime "%Y-%m-%d",localtime(time()- 3600*504);'`
echo $DATE_YEST_FORMAT1

hive -e "
        SELECT t1 [0] AS buyer_id
            ,t1 [1] AS item_id
            ,created_time
        FROM (
            SELECT split(ckey, '\\\\|') AS t1
                ,created_time
            FROM (
                SELECT CONCAT (
                        buyer_id
                        ,'|'
                        ,item_id
                        ) AS ckey
                    ,created_time
                FROM dw_checkout_trans
                WHERE to_date(from_unixtime(cast(UNIX_TIMESTAMP(created_time) AS BIGINT))) = '$DATE_YEST_FORMAT1' distribute BY ckey sort BY ckey
                    ,created_time DESC
                ) a
            WHERE rank(ckey) < 1
            ) X
        ORDER BY buyer_id
            ,created_time DESC;"

sleep 120

QUERY1=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT SUM(total_items_purchased), SUM(total_items_missingormismatch) from lip_data_quality where dt='$DATE_YEST_FORMAT2';"`

Problem Statement:-

If you see my first hive -e block after the echo $DATE_YEST_FORMAT1. Sometimes that query gets failed due to certain reasons. So currently what happens is that, if the first Hive SQL query gets failed, then it goes to second Hive SQL query after sleeping for 120 seconds. And that is the thing I don’t want. So Is there any way if the first query gets failed dues to any reasons, it should get stopped automatically at that point. And it should start running automatically from the starting again after few minutes(should be configurable)

Update:-

As suggested by Stephen.

I tried something like this-

#!/bin/bash

hive -e " blaah blaah;"

RET_VAL=$?
echo $RET_VAL
if [ $RET_VAL -ne 0]; then
echo "HiveQL failed due to certain reason" | mailx -s "LIP Query Failed" -r rj@host.com rj@host.com
exit(1)

I got something like this below as an error and I didn’t got any email too. Anything wrong with my syntax and approach?

syntax error at line 152: `exit' unexpected

Note:-

Zero is success here if the Hive Query is executed successfully.

Another Update after putting the space:-
After making changes like below

#!/bin/bash

hive -e " blaah blaah;"

RET_VAL=$?
echo $RET_VAL
if [ $RET_VAL -ne 0 ]; then
echo "HiveQL failed due to certain reason for LIP" | mailx -s "LIP Query Failed" -r rj@host.com rj@host.com
fi
exit

hive -e 'Another SQL Query;'

I got something like below-

RET_VAL=0
+ echo 0
0
+ [ 0 -ne 0 ]
+ exit

Status code was zero as my first query was successful but my program exited after that and it didn’t went to execute my second query? Why? I am missing something here for sure again.

  • 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-09T19:12:11+00:00Added an answer on June 9, 2026 at 7:12 pm

    Unless I’m misunderstanding the situation, it’s very simple:

    #!/bin/bash
    
    DATE_YEST_FORMAT1=`perl -e 'use POSIX qw(strftime); print strftime "%Y-%m-%d",localtime(time()- 3600*504);'`
    echo $DATE_YEST_FORMAT1
    
    QUERY0="
            SELECT t1 [0] AS buyer_id
                ,t1 [1] AS item_id
                ,created_time
            FROM (
                SELECT split(ckey, '\\\\|') AS t1
                    ,created_time
                FROM (
                    SELECT CONCAT (
                            buyer_id
                            ,'|'
                            ,item_id
                            ) AS ckey
                        ,created_time
                    FROM dw_checkout_trans
                    WHERE to_date(from_unixtime(cast(UNIX_TIMESTAMP(created_time) AS BIGINT))) = '$DATE_YEST_FORMAT1' distribute BY ckey sort BY ckey
                        ,created_time DESC
                    ) a
                WHERE rank(ckey) < 1
                ) X
            ORDER BY buyer_id
                ,created_time DESC;"
    
    if hive -e "$QUERY0"
    then
        sleep 120
        QUERY1=`hive -e "
        set mapred.job.queue.name=hdmi-technology;
        SELECT SUM(total_items_purchased), SUM(total_items_missingormismatch) from lip_data_quality where dt='$DATE_YEST_FORMAT2';"`
        # ...and whatever you do with $QUERY1...
    fi
    

    The string $QUERY0 is for convenience, not necessity. The key point is that you can test whether a command succeeded (returned status 0) with the if statement. The test command (better known as [) is just a command that returns 0 when the tested condition is met, and 1 (non-zero) when it is not met.

    So, the if statement runs the first hive query; if it passes (exit status 0), then (and only then) does it move on to the actions in the then clause.

    I’ve resisted the temptation to reformat your SQL; suffice to say, it is not the layout I would use in my own code.

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

Sidebar

Related Questions

Below is my shell script that is working fine. #!/bin/bash DATE_YEST_FORMAT2=`perl -e 'use POSIX
Below is my Bash Shell Script from which I am executing my two Hive
Below is my shell script from which I am running my Hive query. In
I have a shell script script here as below: #!/bin/bash CPUSELECTION=1 386SX off \
This is my below shell script which I am using to query the hive
i am testing with the shell script below: #!/bin/ksh -x instance=`echo $1 | cut
So I'm trying to execute a shell script which produces a lot of output(in
I am trying to call my linux executable from shell script. Before calling this
I'm trying to install MySQL on Ubuntu Natty from a shell script. However, I
I'm attempting to emulate the functionality of the bash shell script below using a

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.