Ввод текста в оболочке adb с пробелом
Как отправить текст с пробелами типа "некоторый текст", используя adb shell input text
?
Нашел следующее решение
adb shell input text "some%stext"
работает нормально. Но есть ли простой способ заменить пространство на%s?
Пример:
$ adb shell input text "some text"
Error: Invalid arguments for command: text
Usage: input [<source>] <command> [<arg>...]
The sources are:
keyboard
mouse
joystick
touchnavigation
touchpad
trackball
dpad
stylus
gamepad
touchscreen
The commands and default sources are:
text <string> (Default: touchscreen)
keyevent [--longpress] <key code number or name> ... (Default: keyboard)
tap <x> <y> (Default: touchscreen)
swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
press (Default: trackball)
roll <dx> <dy> (Default: trackball)
6 ответов
Решение
С установленным gnu-linux sed (или другими) (большинство машин linux поставляются с ним предустановленным) - вы можете использовать sed для замены пробелов на%s.
adb shell input text $(echo "some text with spaces" | sed 's/ /\%s/g')
знак% должен быть экранирован \.
Вы можете сделать это с \<space>
, как это:
adb shell input text "some\ text"
Вы можете отправить свой текст как:
Text = "some text with spaces"
Замените заготовки на%s:
adb shell input text some%stext%swith%sspaces
Заключите текст в одинарные кавычки:
adb shell input text 'some text'
Лучший способ заменить специальные символы на .
val message = text.replace("|", "\\|")
.replace("\"", "\\\"")
.replace("\'", "\\\'")
.replace("<", "\\<")
.replace(">", "\\>")
.replace(";", "\\;")
.replace("?", "\\?")
.replace("`", "\\`")
.replace("&", "\\&")
.replace("*", "\\*")
.replace("(", "\\(")
.replace(")", "\\)")
.replace("~", "\\~")
.replace(" ", "\\ ")