简体   繁体   English

ADB - 使用 adb shell 从设备获取字符串有意外和奇怪的行为

[英]ADB - Getting string from the device using adb shell has unexpected and weird behavior

I am having the weirdest problem when I try to concat 2 String in a shell script.当我尝试在 shell 脚本中连接 2 String 时,我遇到了最奇怪的问题。

First, let me tell you what I want to do.首先,让我告诉你我想做什么。 I want to create a folder in the external storage of an Android device, using adb .我想使用adb在 Android 设备的外部存储中创建一个文件夹。 In order to do that, I first need to get the external storage folder of the device.为此,我首先需要获取设备的外部存储文件夹。

For that, I used this:为此,我使用了这个:

folder=$(adb shell 'echo $EXTERNAL_STORAGE')

And it works, since if I try to run echo $folder the result is /sdcard , which is the correct external storage folder.它有效,因为如果我尝试运行echo $folder结果是/sdcard ,这是正确的外部存储文件夹。

But I want to create a new folder inside it, called testResults .但我想在其中创建一个名为testResults的新文件夹。 So, to get the full path I tried this:因此,为了获得完整路径,我尝试了以下操作:

newDir=$folder/testResults

The problem is that the newDir variable was allways having the value /testResults , ignoring the value of folder .问题是newDir变量始终具有值/testResults ,而忽略了folder的值。

At first, I thought that it might be a subshell problem, ie the value of folder was not available when I tried to concat it with the new String "for some reason".起初,我认为这可能是一个子shell问题,即当我“出于某种原因”尝试将其与新字符串连接时, folder的值不可用。 But then, it got even weirder...但后来,事情变得更奇怪了……

If I try to concat folder with a smaller string than folder ('pop', for instance), like this:如果我尝试CONCAT folder与比小串folder (“流行”,例如),就像这样:

newDir=$folder/pop

This is the result when I run echo $newDir :这是我运行echo $newDir时的结果:

/popard

CONCLUSION: For some reason, the value of folder is being "ignored", and when it tries to concat with other string s it will actually replace the first N characters of folder (with N=length(s) ).结论:由于某种原因, folder的值被“忽略”,当它尝试与其他字符串s它实际上会替换folder的前N字符(使用N=length(s) )。

I thought that maybe the /s at the beginnig of folder was the reason behind so much confusion, so I tried this:我想也许folder开头的/s是造成如此多混乱的原因,所以我尝试了这个:

folder=$(adb shell 'echo sdcard')
newDir=$folder/pop
echo $newDir

And the result is this:结果是这样的:

/poprd

CONSLUSION: Something in the adb shell tool is making the strings behave anomalously.结论: adb shell 工具中的某些东西使字符串表现异常。

In fact, the problem seems to be related with adb, because if I try this:事实上,问题似乎与adb有关,因为如果我试试这个:

folder=$(echo "/sdcard")  #or even folder="/sdcard"
newDir=$folder/testResults
echo $newDir

The result is correct: /sdcard/testResults结果正确: /sdcard/testResults

But this gets even weirder... I tried using files to help me, like this:但这变得更奇怪了……我尝试使用文件来帮助我,如下所示:

adb shell 'echo $EXTERNAL_STORAGE' > result.txt
folder=$(cat result.txt)
newDir=$folder/pop
echo $newDir

The result here is /popard again!这里的结果又是/popard If I open the file, and insert something in the line that has the string /sdcard , to make it (for example) /sdcardish , and then do this:如果我打开文件,并在包含字符串/sdcard的行中插入一些内容,使其(例如) /sdcardish ,然后执行以下操作:

folder=$(cat result.txt)
newDir=$folder/pop
echo $newDir

The result is: /popardish If I remove that line in the file and then add another one, it works fine.结果是: /popardish 如果我删除文件中的那一行,然后添加另一行,它工作正常。

I seriously don't know what is happening... Does anybody know what can possibly be happening?我真的不知道发生了什么......有人知道可能会发生什么吗?

Thanks in advance!提前致谢!

I'd guess there is a newline at the end of the folder value.我猜folder值的末尾有一个换行符。 Try this:尝试这个:

folder=$(adb shell 'echo -n $EXTERNAL_STORAGE')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM