I have a need to group commands using curly braces in my shell script so that I can direct their output to separate log files like so ...
>cat how-to-exit-script-from-within-curly-braces.sh
{
printf "%d\n" 1
printf "%d\n" 2
} | tee a.log
{
printf "%d\n" 3
printf "%d\n" 4
} | tee b.log
>./how-to-exit-script-from-within-curly-braces.sh
1
2
3
4
>cat a.log
1
2
>cat b.log
3
4
>
Although I have added the curly braced to facilitate logging, I would still like the script to exit when the exit command is invoked inside the curly braces.
It does not do so of course. It only exits the curly braces and then continues on executing the remainder of the script like so ...
>cat how-to-exit-script-from-within-curly-braces.sh
{
printf "%d\n" 1
exit
printf "%d\n" 2
} | tee a.log
{
printf "%d\n" 3
printf "%d\n" 4
} | tee b.log
>./how-to-exit-script-from-within-curly-braces.sh
1
3
4
>cat a.log
1
>cat b.log
3
4
>
making the exit code non-zero and adding "set -e" to the script does not appear to work ...
>cat how-to-exit-script-from-within-curly-braces.sh
set -e
{
printf "%d\n" 1
exit 1
printf "%d\n" 2
} | tee a.log
{
printf "%d\n" 3
printf "%d\n" 4
} | tee b.log
>./how-to-exit-script-from-within-curly-braces.sh
1
3
4
>cat a.log
1
>cat b.log
3
4
>
Is there a way to force the exit of the script from within the curly braces?