adb shell 输入文本不带 & 符号

问题描述 投票:0回答:4

当我输入一些带有 & 字符的文本时,我的完整文本不会输入到设备上。

D:\Apps\Android-SDK\tools>adb shell input text hello&hello
'hello' is not recognized as an internal or external command,
operable program or batch file.

只进入了

hello
。但其他
&
hello
字符未输入。

如何输入&字符?

android adb
4个回答
11
投票

您需要将字符串封装在引号中并转义&符号

"hello\&hello"


6
投票

( ) < > | ; & * \ ~ " '
和空间都需要逃逸。 空格可以替换为
%s

我已经编写了一个android命令行工具来执行此调用

inputer

我的代码中的一个函数在这里(使用
regex
):

//e.g. convert string "hello world\n" to string "hello%sworld\n"
    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;
}

0
投票

终于找到了适合我的字符串 - 你好1”&“你好2


0
投票

我写了一个简单的oneliner来转义输入文本:

"Hello, world.".replace(/[()<>|;&*\\~"'$]/g, function (match) { return ("\\" + match); }).replace(/ /g, "%s");

您可能需要调整它或添加更多转义字符。

© www.soinside.com 2019 - 2024. All rights reserved.