[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to read \000?
From: |
Jeffrey Walton |
Subject: |
Re: How to read \000? |
Date: |
Wed, 15 May 2024 20:27:37 -0400 |
On Wed, May 15, 2024 at 6:20 PM Greg Wooledge <greg@wooledge.org> wrote:
> On Wed, May 15, 2024 at 06:01:04PM -0400, Jeffrey Walton wrote:
> > On Wed, May 15, 2024 at 3:48 PM Peng Yu <pengyu.ut@gmail.com> wrote:
> >
> > > No. I need to just read one character.
> > >
> >
> > `tail -c +1` will read just one character.
>
> I think you have the wrong command. Perhaps you meant "head -c 1"?
>
No. head does not have a -c option, see <
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/head.html>.
> Whether head reads one byte, or more than one byte, will be implementation
> dependent. I don't think you can guarantee its behavior across platforms.
>
> On Debian 12, "head -c 1" appears to consume exactly one byte, but
> "head -n 1" consumes a full 8k buffer
-n specifies lines, while -c specifies characters. Since -c is only
available with tail, you have to use tail -c +1
> hobbit:~$ seq 2000 | wc
> 2000 2000 8893
> hobbit:~$ seq 2000 | { head -c 1 >/dev/null; wc; }
> 2000 1999 8892
> hobbit:~$ seq 2000 | { head -n 1 >/dev/null; wc; }
> 141 140 701
>
> On other platforms, "head -c 1" may use a buffered read.
>
> If you want a standard command that reads 1 byte *without* reading a
> whole buffer, on all platforms, I suggest "dd bs=1 count=1".
>
> > I guess the problem is, what do
> > you want to do afterwards.
>
> This is Peng Yu. The one guarantee you have is that you will *not*
> be told what they want to do afterward. Or before. Or during.
>
:)
Jeff
Re: How to read \000?, Grisha Levit, 2024/05/15