Search code examples
linuxbashunixbackground-process

what is difference between round brackets() and curly braces {} in terms of creating background processes in linux


I tried the following using ( )& but it doesn't assign my IP addresses and it looks like wait statement doesn't wait. Same code works without ( )&. What could be some other possible way to execute this ( )& block of code in parallel? What will happen if I use curly braces instead?

To make it simple

iface[i] are eth0,eth1,eth2,eth3 and so on

nic_count are number of eth port.

initially all ip_addr[i]=0 are assigned.

i=0 
while ((i < Snic_count )); do
    if [[ ${#ip_addr[$i]} == 0 ]]; then
    (
           dhclient ${iface[$i]};
           sleep 10
           ip_addr[$i]=`ip 4 -a show ${iface[$i]} | awk '/inet/ {gsub("/..$","",$2); print $2}'`
           if [[ ${#ip_addr[$i]} == 0 ]]; then
                 echo -e "Unable to assign an IP address for ${iface[$i]}"
           fi
     )&    
    fi
    1+=1
done 
wait

Solution

  • OK so I finally figure it out. New child process's data is destroyed as soon as they return to parent. So solution to that is I write each of the ip address to an external file like...(echo ip_addr[$i] > ip_num$i) Anyways if anyone can come up with an idea to avoid too many file creations would be appreciated.

    while ((i < Snic_count )); do
        if [[ ${#ip_addr[$i]} == 0 ]]; then
        (
               dhclient ${iface[$i]};
               sleep 10
               ip_addr[$i]=`ip 4 -a show ${iface[$i]} | awk '/inet/ {gsub("/..$","",$2); print $2}'`
               if [[ ${#ip_addr[$i]} == 0 ]]; then
                     echo -e "Unable to assign an IP address for ${iface[$i]}"
               fi
               #save each output ip to a separate file.
               echo ip_addr[$i] > ip_num$i
         )&    
        fi
        1+=1
    done 
    wait