Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
for elem in {1..99} ; do echo $elem> mypipe ; done

This piece of bash code should pass 99 ints through the pipe, which previously, in another terminal had this set:

cat <mypipe

Does anyone know why I'm sometimes only getting a random (less than 99) number of ints passed through the pipe, after which it hangs? I have to terminate the script with Ctrl+c, and I get this message:

bash: mypipe: Interrupted system call

I am running Ubuntu 11.10.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

The problem with your code is that > mypipe will open the fifo, write to it, then close it. Once it's closed in either end, you have to reopen it in both ends. So instead of reopening the fifo for every echo, keep it open for the entire loop.

for elem in {1..99}; do echo "$elem"; done > mypipe

Btw, in place of that for loop, you could just use a single printf

printf '%s\n' {1..99} > mypipe

If you have a more complex case. You can assign an fd to it instead.

exec 3> mypipe # opens mypipe for writing on fd 3

echo "stuff" >&3
echo "more stuff" >&3
...

exec 3>&-      # closes fd 3

See http://mywiki.wooledge.org/BashFAQ/085 for more.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.