shell script: run a block of code in the background without defining a new function?
Is there a way to 'inline' a block of code the run in the background without defining the block as a function? I was thinking something like:
( do something; a bit more; finally this ) &
( more things; etc ...; ) &
wait
proceed ...
I suppose it's only one line e开发者_运维问答xtra define a single-use function and then immediately use it but I was curious and didn't find anything searching.
#!/bin/sh
{
echo "sleeping for 5 seconds"
sleep 5
echo "woke up"
} &
echo "waiting"
wait
echo "proceed"
Output
$ ./bgblock
waiting
sleeping for 5 seconds
woke up
proceed
I could do it with something like this:
#!/bin/sh
{
echo "sleeping for 5 seconds"
sleep 5
echo "woke up"
} 1>/dev/null 2>&1 &
well that is not (completely) true what SiegeX replied to Steven Lou the reason why syntax { cmd || cmd2 } & did not work is because it should be as such: { cmd || cmd2 ;} &
精彩评论