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

Re: CR/LF



On Sat, Dec 10, 2022 at 11:16:12PM -0500, Jim Popovitch wrote:
> On Sat, 2022-12-10 at 22:10 -0500, Greg Wooledge wrote:
> > On Sat, Dec 10, 2022 at 10:07:48PM -0500, Jim Popovitch wrote:

> > > > > Why does this produce a CR/LF

> > There is still no CR.  At all.  Ever.  This is not Microsoft Windows.
> 
> Why would you assume Windows is involved?  This is about running cmds
> from Debian 11 to Debian 11.

Then there is no CR/LF.  There is only LF.

> > So... what are you actually trying to do?
> 
> Run cmds on a remote system, that is captured locally in a variable,
> where said cmds may or may not produce output.

OK.  You have a few choices:

1) Throw away the notion that you can store the output in a variable, and
   store it in a file instead.  This is the simplest and safest thing to
   do.  If the command produces binary data (including NUL bytes), it's
   not possible to store it directly in a shell variable.  But it can
   always be stored in a file.

   Redirection to a file also dodges all the insane issues of data
   modification that you get with the other choices.

2) Use a command substitution.  This has two issues:

   a) It cannot handle binary data -- only text.
   b) All trailing newlines will be stripped by the command substitution.

   If you're certain the output will be text, but you need to preserve
   the correct number of newlines in the output, then the standard
   workaround is to append a fixed character to the stream, and remove
   it afterward:

   myvar=$(ssh whatever; printf x)
   myvar=${myvar%x}

   That preserves the output stream in its original form.

3) Pipe the command through something like base64, and use a command
   substitution to store the base64 encoded data stream in the shell
   variable.

   Then use base64 -d (or whatever inverts your choice of encoding) when
   you need to use the data.

> Taking $() out of the equation doesn't change the result. The following
> will add a CR/LF: 

THERE IS NO CR!

>   TEST=`ssh -o LogLevel=QUIET -t user@server "echo -n ''"`; echo ${TEST}

COMMAND SUBSTITUTION REMOVES ALL TRAILING NEWLINES.

IT DOES NOT MATTER WHETHER YOU USE THE MODERN $() OR THE ANCIENT AND
DEPRECATED BACKTICKS.

YOU ARE **STILL** FAILING TO QUOTE CORRECTLY!

YOU ARE **STILL** USING echo WHICH ADDS A NEWLINE AND THEN WONDERING WHY
A NEWLINE IS ADDED.

.... I'm done.  Continuing the self-abuse of attempting to help you
is going to be pointless, so please read the answers you've already
been given.  I won't give you any more.


Reply to: