I'm running offlineimap on a bunch of accounts and want to check the exit code of each run and perform some actions accordingly.
I have 6 separate email accounts that I run against with a lot of code duplication. The original command structure is:
$ $(which offlineimap) -c offlineimaprc -o -a yahoo & declare yahoo_pid=$!
wait $yahoo_pid
yahoo_st=$?
if [[ $yahoo_st -ne 0 ]];then <do some stuff>
$ $(which offlineimap) -c offlineimaprc -o -a gmail & declare gmail_pid=$!
wait $gmail_pid
gmail_st=$?
if [[ $gmail_st -ne 0 ]];then <do some stuff>
Now I'd like to remove the duplication and run this from a for in
loop and the wait
command. The ${account-name}_pid (e.g. yahoo_pid) substitution works fine but I get stuck with the wait
command.
$ for app in yahoo gmail
do
$(which offlineimap) -c offlineimaprc -o -a ${app} & declare ${app/%/_pid}=$!
wait ${app}_pid
done
[1] 73443
-bash: wait: `$yahoo_pid': not a pid or valid job spec
[2] 73444
-bash: wait: `$gmail_pid': not a pid or valid job spec
$yahoo_pid
value is assigned in the statement just before thewait
command but I can't seem to access it. – Tony Barganski May 24 '22 at 20:30