Stopping .bashrc without exiting shell

6,368

Solution 1

In BASH you can use the return built-in:

return [n]

Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body. If used outside a function, but during execution of a script by the . (source) command, it causes the shell to stop executing that script and return either n or the exit status of the last command executed within the script as the exit status of the script. If used outside a function and not during execution of a script by ., the return status is false. Any command associated with the RETURN trap is executed before execution resumes after the function or script.

For other shells it might work differently of course.

If you can possibly rewrite the main script as a loop, you could use the break statement based on the outcome of the loop command.

Solution 2

Check the result of your processing and continue only when nothing has failed.

module_1
if [ $? -ne 0 ]; then
   display_error_function
else
   module_2
   if [ $? -ne 0 ]; then
      display_error_function
   else
      echo OK
   fi
fi

When you have a lot of modules, consider an extra shell variable.
EDIT: With the extra variable you can prevent deep-nesting (just repeat the if-block).

ERRORSFOUND=0
...
if [[ ${ERRORSFOUND} -eq 0 ]]; then
   module_x
   if [[ $? -ne 0 ]]; then
      ERRORSFOUND=1
   fi
fi
Share:
6,368

Related videos on Youtube

nfarrar
Author by

nfarrar

A school of hardknocks-style technical generalist. Mediocre in many technical areas, which fortunately lends itself to being exceptional at Incident Response and providing Technical Leadership. Incident Response Malware Reverse Engineering Digital Forensics Threat Intelligence Development Operations Cloud Operations Python Development

Updated on September 18, 2022

Comments

  • nfarrar
    nfarrar over 1 year

    My .bashrc file is modularized and I've got some error checking in it. In bash scripts, I typically have a function _errexit (or something similar) that I call when I detect an error and want to exit.

    In the case of my bashrc file(s), I don't want to exit (it would close the shell) - but I do want to display an error message and stop processing immediately.

    Any ideas how this might be possible?

  • peterph
    peterph about 10 years
    OP is asking exactly about how not to continue - i.e. what exactly should display_error_function (in your parlance) look like as not to exit the shell completely. I take it he already has something along these lines in his script.
  • celtschk
    celtschk about 10 years
    @peterph: That "not to continue" part is handled in Walter A's answer by putting all the rest of the script in the else part. However if there are many modules, this might give an undesirably deep nesting.
  • Walter A
    Walter A about 10 years
    @celtschk: The second code fragment (with an extra shell variable) was introduced to prevent deep nesting. You can repeat the if-test for a lot of module_x calls. When 1 call fails, you fall, all subsequent tests on ERRORSFOUND will fail.
  • peterph
    peterph about 10 years
    @celtschk my point is, that it doesn't really stop processing the script, unless you do the nesting. And that will make the script cluttered. Unless you put it into a loop in which case one could use the break statement. However, a loop is not always the best form of putting such a script together.