How to remove CR in error message in sftp stderr

1

I am using Linux 2.6.32-573.26.1

I use a_script to run batch sftp to get file from another Linux server

Here is the way to run a_script

a_script > a_script.log.wri 2>&1

Here is a_script:

echo script start at `date`
sftp -b myCommandFile user@server 
echo script end at `date`

Inside myCommandFile, there are these commands:

lls an_non_exist_file

Because cannot find an_non_exist_file
The error message is:

ls: cannot access an_non_exist_file: No such file or directory  
Shell exited with status 2^M  

So, a_script.log.wri will contain everything, including the line

Shell exited with status 2^M

It is this line to spoil everything.

Because there is a Carriage-return ^M char in the a_script.log.wri , when I attached this file into a email and send to gmail , the gmail cannot open it.

Although it is a file with extension wri , the gamil just does not open it.

I am using Nexus 5x to read the email inside gmail. The Android does not open the attachment file a_script.log.wri

I try to create a file dummy.wri inside Linux with 5 lines and only the 3rd line having a ending carriage-return char. Other 4 lines are ended with LF.

Then, I attach this dummy.wri file in a email and sent to the gmail account.

Well, really it cannot open dummy.wri

If there is no ending carriage-return, everything is fine. I can open it inside the gmail inside Android of Nexus 5x.

So, my question is , how to make sftp NOT to add the carriage-return char when displaying the error message ?

Also, instead of using lls, using ls an_non_exist_file will get similar situation.

Alvin SIU

Alvin SIU

Posted 2016-07-06T05:03:04.993

Reputation: 111

Have you tried a_script 2>&1 | sed 's/\r$//' > a_script.log.wri ? – Eugen Rieck – 2016-07-06T06:57:21.537

Yes, I use similar method as a workaround. My question is: since everything is running in unix environment, why the sftp like to use CRLF as line delimiter ? Also, there seems to be no way to tell sftp to use LF as line delimiter. – Alvin SIU – 2016-07-07T01:56:28.000

No answers