3

I am trying to run .hql file through a shell script like below

#!/bin/bash
cd /path/

hive -f hive_script.hql

but the script hive_script.hql is failing. I want to exit the shell script successfully even if the script fails. Is that possible?

4
  • 2
    add this to the end of your script: exit 0. But why on earth would you want to pretend the script ran successfully? Commented May 31, 2018 at 15:59
  • 1
    Why is this tagged sh though your she-bang is #!/bin/bash? Commented May 31, 2018 at 16:28
  • 1
    hive -f hive_script.hql ||: is another idiomatic way to ignore a nonzero exit status. (: is another name for true). Commented May 31, 2018 at 16:31
  • 1
    BTW, it's good practice to make it cd /path/ || exit; without the || exit, you'll try to run hive in the wrong directory if the cd fails. Commented May 31, 2018 at 16:32

2 Answers 2

4

If you don't put an explicit exit in your script, the exit code of your script would be the exit code of the last command it ran - in your case, it is the hive -f ... command.

You can add exit 0 at the end of your script to make sure it always exits with zero.


Related:

Sign up to request clarification or add additional context in comments.

Comments

2

If you want the script to exit with 0 even when hive -f hive_script.hql would fail, you can just or the command with something that won't ever throw an error

hive -f hive_script.hql || :

This means that if the hive command fails, bash should also run the second command. In this case, that command is :, which is basically pass from python, and will always return a 0 status.

4 Comments

I have tried the below but it failed #!/bin/bash cd /path hive -f hive_script.hql || :
Then it is likely that you are failing before that, on the cd /path/ part of the command. You can also do cd /path/ || : to prevent that from failing.
No it's not failing at that part , the .hql script is failing with "vertex failed error " . I wanted to ignore the error and exit the .sh script with success
@SanthoshChakka are you sure your script is exiting with a nonzero code? It is possible that the error message gets printed and the script still "succeeds". Does echo $? after running the script return a nonzero number?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.