views:

61

answers:

2

Hi all dear friends

The following test script has a problem. When I add the line (sleep 5 ) & in the script then the "while read" loop does not read all lines from the file, but only prints the first line.

But when I remove the ( sleep 5 ) & from the script, then the script prints all lines as defined in the file.

Why the ( sleep 5 ) & causes this?

And how to solve the problem? I want to create a new process (for which the sleep is just an example) in the while loop.

THX

$ more test

#!/bin/ksh 
while read -r line ; do 
echo $line
( sleep 5 )&   
RESULT=$!
sleep 1
kill $RESULT
done < file


$ more file
123 aaa
234 bbb
556 ccc
A: 

The code you provides runs and prints each line in the file.

Since you are not waiting for the

( sleep 5 ) &

child process, it has no effect on process execution. Is this the EXACT code you wrote?

jim mcnamara
A: 

This seems to be a bug in a specific version of ksh. I'm getting the same effect from ksh 93s+, but not from bash, ash, pdksh, zsh or ksh 93t+.

Gilles