How can I evaluate the result of an adb shell command?

655

adb is adding a carriage-return (aka 0x0d, Ctrl-M, \r, etc) before the line-feed. Probably for ease of use with Windows software that expects lines to end with CR-LF rather than just LF.

You can see this yourself with hexdump aka hd, e.g.:

$ printf "$R" | hd
00000000  30 0d                                             |0.|
00000002

Because you only need to return a single value (the exit code). you could use printf instead of echo and redirect all of ls's output to /dev/null on the Android device to avoid printing any newlines (then adb doesn't add a CR):

R="$(adb shell 'ls /mnt/ > /dev/null 2>&1 ; printf $?')"

If your android device doesn't have printf, or if you need to return one or more lines of output from a the android shell, you can use tr -d '\r' or dos2unix or sed 's/\r$//' or similar to strip the CR.

dos2unix and sed are better choices than tr here because they will only strip CRs that are immediately followed by LF, leaving alone any CRs that might be in elsewhere in a line:

$ R="$(adb shell 'ls /mnt/ > /dev/null 2>&1 ; echo $?' | dos2unix)"
$ printf "$R" | hd
00000000  30                                                |0|
00000001
Share:
655

Related videos on Youtube

Shahzad Tariq
Author by

Shahzad Tariq

Updated on September 18, 2022

Comments

  • Shahzad Tariq
    Shahzad Tariq almost 2 years

    Help me i want to add link wizard in the backend module in front of the textfield without using TCA in typo3-7.6.9.

    enter image description here

    • Jost
      Jost almost 8 years
      You can't really do that without using the TCA - even within flexforms, it's basically a TCA definition.
    • rob-ot
      rob-ot almost 8 years
      as Jost pointed out: you need to define the type of field inn TCA. you can then later on adjust some settings with typoscript in pageTS
    • Kami Yang
      Kami Yang almost 8 years
      So this means, that you will never be able to use it inside of an Backend Module which is made with fluid files?
  • ka3ak
    ka3ak almost 7 years
    'printf not found'
  • Alessio
    Alessio almost 7 years
    what kind of android device is that? printf is even on my ancient HTC Desire HD which I just tested this with (running CM7, not original HTC Android). I'll update with solution for android without printf.
  • Alessio
    Alessio almost 7 years
    btw, hexdump aka hd is a good tool for diagnosing all sorts of weird 'huh, WTF?' problems like this. It makes it easy to see exactly what input or output you are getting (with hex bytes and ascii representation in hd's default output format)
  • Alessio
    Alessio almost 7 years
    my HTC phone is from 2010. but its running CyanogenMod 7, not stock HTC android. Doesn't matter, tr will work whether the device has printf or not.
  • ka3ak
    ka3ak almost 7 years
    Please use 2> so that there is still one line in case of an error (path doesn't exist). Or it may be still better to use 'tail -1'.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 7 years
    The culprit is adb, not echo. Adb adds CR, which is very annoying, for example, when you want to do something like adb shell tar -cf - ….
  • eMPee584
    eMPee584 over 6 years
    I hate android™. So many bad design decisions. So many hours wasted.