【问题标题】:adb shell input text does not take & ampersand characteradb shell 输入文本不带 & & 字符
【发布时间】:2014-09-11 15:27:01
【问题描述】:

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

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


【解决方案1】:

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

【讨论】:

  • 不,它没有进入。请找到以下输出。 D:\Apps\Android-SDK\tools>adb shell input text hello1\&hello2 'hello2' 不是内部或外部命令、可运行程序或批处理文件。
  • 不,还是同样的错误。 D:\Apps\Android-SDK\tools>adb shell 输入文本 "hello12&hello12" /system/bin/sh: hello12: not found
  • @a.t.obulreddy - 已修复。
【解决方案2】:

( ) < > | ; & * \ ~ " ' 和空间都需要转义。 空格可以替换为%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;
}

【讨论】:

    【解决方案3】:

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

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

    您可能需要对其进行调整或添加更多转义字符。

    【讨论】:

      【解决方案4】:

      终于找到了适合我的字符串—— 你好1"&"你好2

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多