You are not logged in.

#1 2021-01-12 17:36:30

EdeWolf
Member
Registered: 2016-01-06
Posts: 85

[Solved] Differnent behaviour of head and tail (serial port whoes)

Not sure, wether this is the proper forum, as it is a mixture of hardware, coreutils and bash. Please feel free to move, if I am wrong here.


Back on topic. I am in need of reading the last line of the output of a serial device within a simple shell batch and am having trouble understanding, why head -n1 /dev/ttyS0 is working, but tail -f /dev/ttyS0 is not. 

As I would just like to read the last line, I am rather looking for a tail -n 1 /dev/ttyS0.

But no chance here either, the output of tail is dead silent, no matter how may lines are being output by the serial device. The serial port outputs both \r and  \n per line, I've tried filtering either using stty, it makes no difference.
Redirecting to a file and tail that, does work again. As in cat /dev/ttyS0 > /tmp/file &; tail -f /tmp/file. No idea what is going in here.

So, please, what is the difference between those two? Why does tail /dev/ttyS0 not output any single character, while head does its job as expected?

Any ideas?

Last edited by EdeWolf (2021-01-12 19:34:51)

Offline

#2 2021-01-12 17:49:12

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,700

Re: [Solved] Differnent behaviour of head and tail (serial port whoes)

My guess would be that tail is waiting for an EOF.  Head can stop reading as soon as it gets the data it needs.  Tail has no way to know a line is the last line until it reaches the end of the file (as it were)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#3 2021-01-12 18:27:12

EdeWolf
Member
Registered: 2016-01-06
Posts: 85

Re: [Solved] Differnent behaviour of head and tail (serial port whoes)

Thanks very much. Sounds somwhat plausible, however, this makes me wonder, why then is tail working on log files, that, I assume, have no EOF either, since they are open and permanently appended to?

Offline

#4 2021-01-12 18:35:05

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,700

Re: [Solved] Differnent behaviour of head and tail (serial port whoes)

They do.  Well, not really -- the EOF is not in the file per se, but when reading the file, when attempting to read past the end, the OS will return an EOF.  They can grow at any time as they are updated, but there is always a end at any given time.   A device node will block when there are no more characters until the 'file' is closed.   Try using cat instead of tail,  you should see the same behavior of not closing, but you will see characters as they arrive.  Same for a command such as od -x  -- you will see the hex representations of characters as they arrive (in blocks of 16 bytes)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#5 2021-01-12 19:34:30

EdeWolf
Member
Registered: 2016-01-06
Posts: 85

Re: [Solved] Differnent behaviour of head and tail (serial port whoes)

Thanks again. Makes sense. cat and od are working, and I've tried cat /dev/ttyS0 | tail -f before, but of course to no avail. I've also tried to redirect ttyS0 to a filedescriptor or a fifo, but tail never spoke a word. But if it is the OS, then it all is comprehendable.

Offline

Board footer

Powered by FluxBB