I have a bash script with roughly the following structure:
function download {
# download a big file
}
function prepare_stuff {
# prepare some stuff
}
function process_download {
# process the downloaded file
}
download & prepare_stuff & wait
process_download
The first thing it does is to download a file of several hundred megabytes. While the download is in progress, some other things are prepared in the background. When both of these have finished, the download is processed.
download
may finish in three different ways:
- The download failed (e.g. server not reachable)
- The file was downloaded successfully
- The file has not changed on the server since the last download
Case 1 is an error condition (in which case the function should return something different from zero), while 2 and 3 are not (i.e. the return value should be zero).
Now, I want process_download
to skip the actual processing when case 1 or 3 is encountered, so I need to pass some kind of status back from download
. Since download
runs in a subshell, a variable will not work (assignments take place in the subshell and are not passed back to the parent shell).
How can I pass some kind of value from a function in a subshell back to the parent shell?