1

I have bash script like this:

rm ~/sqoop/"$TABLE"/*
rmdir ~/sqoop/"$TABLE"
return $?

After execution this script it returns a value to next process but even though the script run successfully and executing all stages in script completed successfully it is returning 1.

Because this - the next process is not taking place.

If I remove the return statement the next process going smoothly. this problem came after the Hadoop cluster up gradation which having Ubuntu 14 and new cluster have Ubuntu 16.

Can you please help us to understand what is issue and how the next process is running successfully if remove the return command here and what is impact in production if remove the Return statement?

  • what is the next step? what exactly does the next step check? – Yaron Apr 23 '18 at 7:59
  • The next is HQL(hive query in Hadoop) and it has some business steps it is running if i comment the return value it is working good but not running if comment the return. i have't changes anything on business logic/shell script the only thing we had done is upgrade – gs rao Apr 23 '18 at 8:06
3

The return statement returns an error if the statement is not executed from a function.

In shell functions, use return. In scripts, use exit.

A short script like

#!/bin/bash

return 0

will produce the error message

line 1: return: can only `return' from a function or sourced script

and it will set $? to 1.

If you don't exit the script with an explicit exit (or if you exit with exit but without specifying a exit code), the return code of the script as a whole will be the same as the last executed command.

Having exit "$?" is the same as plain exit, and if it's at the end of the script, this can be left out completely.


In general, I also suggest that you use $HOME rather than tilde in scripts. This is because $HOME works like any other variable, whereas tilde is expanded in a separate expansion step, which means that it does not behave as a variable and that it is not expanded in quoted strings. $HOME is also more descriptive and since it's a script, you can spend few extra keystrokes to make the code more readable.

  • I have quick query for above your response..i will try above with exist 0 but i have one query why this issue happens now only previsouly i use same query ? it is working if i comment return statement is this impact any thing? – gs rao Apr 23 '18 at 8:37
  • @gsrao If you comment out the return statement, the return code of the script will be that of the last executed command, so in your case here, you can definitely leave it out. I will add this to my answer. – Kusalananda Apr 23 '18 at 9:01
  • Oh..great ..you are saving my life, so my case if i comment the return by default script will return last comment successful flag(rmdir ~/sqoop/"$TABLE") right? ..here even though your answered work me if two simple queries which are eating my brain 1) my case what is use of "return $?" like you said it will work like same if i comment the "return $" 2) what may be possible causing the issue earlier to now? – gs rao Apr 23 '18 at 9:17
  • @gsrao Yes, if you comment out the return (which shouldn't be there to start with since it's not in a function, as far as I can see), the exit status of the script will be that of rmdir, if that's the last command in the script. As for your last question, I don't quite understand what you mean by "the issue earlier to now". – Kusalananda Apr 23 '18 at 9:22
  • My last question..i am using same script what i have use before linux/cluster up gradation it was working until cluster up gradation, why now only this issue happens? what might be the cause this issue? – gs rao Apr 23 '18 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.