How can I evaluate the result of an adb shell command?
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
Related videos on Youtube
Shahzad Tariq
Updated on September 18, 2022Comments
-
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.
-
Jost almost 8 yearsYou can't really do that without using the TCA - even within flexforms, it's basically a TCA definition.
-
rob-ot almost 8 yearsas 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 almost 8 yearsSo this means, that you will never be able to use it inside of an Backend Module which is made with fluid files?
-
-
ka3ak almost 7 years'printf not found'
-
Alessio almost 7 yearswhat 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 almost 7 yearsbtw,
hexdump
akahd
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 inhd
's default output format) -
Alessio almost 7 yearsmy HTC phone is from 2010. but its running CyanogenMod 7, not stock HTC android. Doesn't matter,
tr
will work whether the device hasprintf
or not. -
ka3ak almost 7 yearsPlease 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' almost 7 yearsThe 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 over 6 yearsI hate android™. So many bad design decisions. So many hours wasted.