Bash doesn't end the loop despite pipeline is broken -
the simple script:
while [ 1 ]; cat pattern_file done | socat - /dev/ttys0
it makes stream, looped pattern included in file , sends serial port via socat. script allows read data serial port. unfortunately when socat ends (eg. killed) loop hangs forever without error message.
want avoid:
- pts
- more scripts one
- reopening serial port every pattern_file
the problem running that, under bash
(other may shells differ), pipeline terminates after all commands in pipeline finished.
one solution use:
while [ 1 ]; cat pattern_file || break done | socat - /dev/ttys0
if socat
terminates, cat
command fail when runs. however, mere failure of command in loop not cause loop terminate. adding break
command, can assure that, if cat
fails, loop terminate.
another solution avoid pipelines altogether , use process substitution:
socat - /dev/ttys0 < <(while true; cat pattern_file; done)
documentation
the problematic pipeline behavior documented in man bash
:
the shell waits commands in pipeline terminate before returning value.
Comments
Post a Comment