1

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 hive_script.hql script fails. Is it possible?

Charles Duffy
  • 235,655
  • 34
  • 305
  • 356
Santhosh Chakka
  • 125
  • 3
  • 9
  • 2
    add this to the end of your script: `exit 0`. But why on earth would you want to pretend the script ran successfully? – Andrew May 31 '18 at 15:59
  • 1
    Why is this tagged `sh` though your she-bang is `#!/bin/bash`? – codeforester May 31 '18 at 16:28
  • 1
    `hive -f hive_script.hql ||:` is another idiomatic way to ignore a nonzero exit status. (`:` is another name for `true`). – Charles Duffy May 31 '18 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. – Charles Duffy May 31 '18 at 16:32

2 Answers2

3

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:

codeforester
  • 28,846
  • 11
  • 78
  • 104
1

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.

jeremysprofile
  • 6,648
  • 4
  • 25
  • 41
  • I have tried the below but it failed #!/bin/bash cd /path hive -f hive_script.hql || : – Santhosh Chakka May 31 '18 at 16:41
  • 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. – jeremysprofile May 31 '18 at 17:01
  • 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 – Santhosh Chakka May 31 '18 at 17:18
  • @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? – jeremysprofile May 31 '18 at 17:53