3

I am outputting results from an android shell command to a file, with MS-windows cmd via ADB.exe.

It outputs the correct results, but I am getting an extra line between each result. It looks normal in interactive cmd (without extra lines), but when it is saved to a file the additional lines show up.

I am using Notepad++ to view the file output. When viewing all symbols, it is showing a CR(carriage return) at the end of each printed line and a CR LF for each blank line.

Is it possible to output the results to a file without the extra lines, and if so what could be causing this?

Interactively, output direct to terminal

D:\>adb shell "ls -l"

drwxr-xr-x root     root              2009-12-31 19:00 acct
drwxrwx--x system   cache             2020-03-12 07:14 cache
lrwxrwxrwx root     root              1969-12-31 19:00 charger -> /sbin/healthd
dr-x------ root     root              2009-12-31 19:00 config

Redirecting to file

D:\>adb shell "ls -l" > test.log

drwxr-xr-x root     root              2009-12-31 19:00 acct

drwxrwx--x system   cache             2020-03-12 07:14 cache

lrwxrwxrwx root     root              1969-12-31 19:00 charger -> /sbin/healthd

dr-x------ root     root              2009-12-31 19:00 config

2
  • On Unix (UNIX, BSD, Gnu/Linux, MacOS, ...) that should work. I imagine that it is some interaction with Micorsoft's Windows or CMD. MS-Windows still has an ancient line ending, caused by embedding a device driver into every command. Can you edit the question to show how you are viewing the file, and to show any thing else that you missed out. No matter how irrelevant you think it is. It may be useful. Commented Mar 15, 2020 at 9:27
  • @ctrl-alt-delor No, it doesn't work right, even on Linux. If you look at the test.log file, you will see that it contains extra carriage-returns.
    – user313992
    Commented Mar 15, 2020 at 16:17

1 Answer 1

2

Try

adb shell -T "ls -l" > test.log

or, if it complains that error: device only supports allocating a pty:

adb shell "ls -l >/data/local/tmp/list"; adb pull /data/local/tmp/list test.log

Not all the devices support the ssh inspired -t and -T options, even if your adb client program does.

This isn't Windows-specific: even on a Unix system, adb shell "ls -l" > test.log will create a file with undesirable extra carriage returns at the end of the lines.

5
  • Any idea what the problem is? What is causing it? And are they extra carriage-returns or extra line-feeds? Commented Mar 15, 2020 at 16:57
  • @uncle-billy The write file to device and pull method that you answered above was going to be my next idea to try as well. I just tried it and it works perfect, thank you. The other method - adb shell -T "ls -l" - did throw the error you mentioned.
    – PadreD727
    Commented Mar 15, 2020 at 18:28
  • 1
    @ctrl-alt-delor they're extra carriage returns, and they happen because the adb server (on the device/phone) will create a pseudo-tty, and it's the tty driver which translates LF into CR/LF. Just set stty -onlcr; echo first; echo second; echo third to see what happens if the tty driver wasn't doing that.
    – user313992
    Commented Mar 18, 2020 at 1:04
  • @UncleBilly I grok that. What I don't grok, is why CR is mapped to LF. (Why two times LF?) Commented Mar 18, 2020 at 8:02
  • @ctrl-alt-delor the windows version of adb probably does translate the CR to a CR/LF and the LF to another CR/LF when writing to handle pointing to a file, instead of to the console.
    – user313992
    Commented Mar 20, 2020 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .