Adb Shell 输入文本过长

3
我有一个应用程序,其中输入文本是通过adb shell传递的,现在问题是每当我键入命令时:

./adb shell input text 'Khay'

它完美地工作,并在应用程序的文本框中显示“Khay”,但当我传递非常长的命令时,例如:

./adb shell input text ' http://stagingapi.something.com/v2/api.php?apikey=2323214\&appid=32432\&imei=324234 ........................................................

这段文本的英译如下:

如果这段文本太长,就会出现错误

错误:服务名称过长。

现在我有两个问题。

  1. 我是否可以通过 adb shell 传递这么长的文本。

  2. 如果选项1不可行,那么我该怎么办才能传递这么长的输入文本。


请确保您的文本字符串中不包含空格。将所有空格替换为“%s”。 - Alex P.
是的,我已经做过了。好吧,我想出了一个临时而简单的解决方案,将输入分为3部分传递,然后将字符串附加到另一部分。 - user3189761
2个回答

6

解决这个问题的方法是将长命令写入本地的shell文件,使用adb push推送文件,然后在设备上使用sh执行该文件。

因此,不要直接执行:

adb shell input text ' http://...some.really.long.command...'

请这样做:
echo "input text ' http://...some.really.long.command...'" > longcommand.sh
adb push longcommand.sh /data/local/tmp
adb shell sh /data/local/tmp/longcommand.sh

这救了我的命。我不想root我的设备,也忘记了可以使用sh file.sh而不是./file.sh。只有一个备注:我认为chmod +x file.sh不是必需的。 - André Chalella
不应该需要,因为您没有直接运行脚本。如果您想单独运行它,则需要将其设置为可执行文件,但在这种情况下,您需要在开头加上shebang语句。 - cde

1
"你的问题不是字符串长度,而是特殊字符的处理。"
Best to run your string through a converter (to add escape codes),
there are quite a few characters that input does not like:
<pre>
 ( ) < > | ; & * \ ~ 
</pre>
and space escaped with %s .
<pre>
    char * convert(char * trans_string)
    {
        char *s0 = replace(trans_string, '\\', "\\\\");
        free(trans_string);
        char *s = replace(s0, '%', "\\\%");
        free(s0);
        char *s1 = replace(s, ' ', "%s");//special case for SPACE
        free(s);
        char *s2 = replace(s1, '\"', "\\\"");
        free(s1);
        char *s3 = replace(s2, '\'', "\\\'");
        free(s2);
        char *s4 = replace(s3, '\(', "\\\(");
        free(s3);
        char *s5 = replace(s4, '\)', "\\\)");
        free(s4);
        char *s6 = replace(s5, '\&', "\\\&");
        free(s5);
        char *s7 = replace(s6, '\<', "\\\<");
        free(s6);
        char *s8 = replace(s7, '\>', "\\\>");
        free(s7);
        char *s9 = replace(s8, '\;', "\\\;");
        free(s8);
        char *s10 = replace(s9, '\*', "\\\*");
        free(s9);
        char *s11 = replace(s10, '\|', "\\\|");
        free(s10);
        char *s12 = replace(s11, '\~', "\\\~");
        //this if un-escaped gives current directory !
        free(s11);
        char *s13 = replace(s12, '\¬', "\\\¬");
        free(s12);
        char *s14 = replace(s13, '\`', "\\\`");
        free(s13);
    //  char *s15 = replace(s14, '\¦', "\\\¦");
    //  free(s14);
        return s14;
}

(code from inputer native binary: interactive converter for input).

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接