bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Examples of concurrent coproc usage?


From: Carl Edquist
Subject: Re: Examples of concurrent coproc usage?
Date: Tue, 16 Apr 2024 10:00:06 -0500 (CDT)

On Tue, 16 Apr 2024, Andreas Schwab wrote:

But you can delimit your records on NULs, and use printf to reproduce them.

Though that will likely add a spurious null at EOF.


On Tue, 16 Apr 2024, Zachary Santer wrote:

Just wouldn't copy over whatever might have followed the final null
byte, if we're not talking about null-terminated data.

You guys are right.  Sorry for glossing over that detail.

Yes if the file does not end in a NUL byte, the last dangling record still needs to be printed. You can handle it either way with, for example:

        while IFS= read -rd '' X; do printf '%s\0' "$X"; X=; done
        [[ $X ]] && printf '%s' "$X"


Might've gotten lucky with all those .so files ending in a null byte for whatever reason.

Yes that is exactly what happened :)

Luckily, on linux anyway, .so files and ELF binaries always seem to end in a null byte.


There's no way to force this to give you the equivalent of sized buffers. 'read -N' obviously has the same problem of trying to store the null character in a variable. So, if you're trying to run this on a huge text file, you're going to end up trying to shove that entire file into a variable.

Right, that is another reason why it's really not a great solution.

Although you can limit the buffer size with, say, 'read -n 4096', and with a bit more handling[1] still get a perfect copy. But that's not my point.

My point is, it's one thing to use it in an emergency, but I don't consider it a real usable replacement for cat/tee/paste in general use.

Shoveling data around should really be done by an appropriate external program. So in my multi-coproc example, the shell is really crippled if the close-on-exec flags prevent external programs from accessing manual copies of other coproc fds.


Carl



[1] eg:

        emergency_maxbuf_cat_monster () (
            maxbuf=${1:-4096}
            fmts=('%s' '%s\0')
            while IFS= read -rd '' -n $maxbuf X; do
                printf "${fmts[${#X} < maxbuf]}" "$X";
                X=;
            done
            [[ ! $X ]] || printf '%s' "$X"
        )




reply via email to

[Prev in Thread] Current Thread [Next in Thread]