Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I have a function:

myfunction() {
  [ -d somedirectory ] && [ "some other condition" ] || { echo "error" >&2 ; exit 1; }
  global_var=somevalue
}

And I call it from within another function:

some_other_function() {
  myfunction
  # Do something with "$global_var"
}

This works as expected: If the conditions in myfunction fail, the error exit kills the whole script to prevent additional code from executing.

In refactoring a very large script that shares function definitions with other scripts (in a sourced file), I want to get rid of some of the global variables by returning them like so:

myfunction() {
  [ -d somedirectory ] && [ "some other condition" ] || { echo "error" >&2 ; exit 1; }
  local somevar=somevalue
  # do stuff with "$somevar", then...
  # return value of somevar to calling function.
  printf %s "$somevar"
}

some_other_function() {
  local anothervar="$(myfunction)"
  # Do something with "$another_var"
}

However, the error exit here fails to work as intended. Instead of killing the script, it only kills that function, which is executed in a subshell because of command substitution.

Is there a way to modularize this large script to allow text values to be returned from functions (rather than using global variables) and still allow functions to error exit from the entire script?

share|improve this question
2  
If you want to exit for every error that happens in your script, you can set -e. – pfnuesel Jan 21 at 21:49
    
In some_other_function you have local anothervar=$(myfunction). The $(myfunction) runs in a separate instance of the shell. The exit in myfunction exits that subshell, not the "parent" shell. – Andy Dalton Jan 21 at 21:50
    
@AndyDalton, thanks, but I know that...I said it fails to work as intended, not that it fails to work as expected (and I mentioned the subshell issue). I want to know how to make it work as intended. – Wildcard Jan 21 at 21:53

You have to send a signal to the main shell:

# start of the main script:
MAIN_SHELL_PID=$$

[...]

myfunction() {
    ... || { echo "error" >&2 ; kill -HUP "$MAIN_SHELL_PID"; }
}
share|improve this answer
1  
You don't need the variable MAIN_SHELL_PID. Using $$ will work just as well. – Gilles Jan 22 at 22:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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