Code block usage { } in bash
I was wondering why code block was used in this example below:
possibly_hanging_job & { sleep ${TIMEOUT}; eva开发者_StackOverflow中文版l 'kill -9 $!' &> /dev/null; }
This could have been written like this ( without using code block as well) ..right ?
possibly_hanging_job &
sleep ${TIMEOUT}
eval 'kill -9 $!' &> /dev/null
Putting the last two commands in braces makes it clear that “These are not just two additional commands that we happen to be running after the long-running process that might hang; they are, instead, integral to getting it shut down correctly before we proceed with the rest of the shell script.” If the author had instead written:
command &
a
b
c
it would not be completely clear that a
and b
are just part of getting command
to end correctly. By writing it like this:
command & { a; b; }
c
the author makes it clearer that a
and b
exist for the sake of getting command
completely ended and cleaned up before the actual next step, c
, occurs.
Actually I even wonder why there's an eval
. As far as I see it should also work without that.
Regarding your actual question:
I guess the code block is there to emphasize that the sleep
belongs to kill
. But it's not necessary. It should also work like this:
possibly_hanging_job & sleep ${TIMEOUT}; kill -9 $! &> /dev/null
精彩评论