无法在 bash 脚本中运行 adb 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20765134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:05:23  来源:igfitidea点击:

Can't run adb commands in bash script

androidbashshellubuntu-12.04adb

提问by Vinit Shandilya

I'm trying to launch Android tethering settings from adb shell. The main purpose of doing so is to enable USB tethering mode by running a shell script. I'm using the following set of commands on my Ubuntu Terminal (12.04):

我正在尝试从 adb shell 启动 Android 网络共享设置。这样做的主要目的是通过运行 shell 脚本来启用 USB 网络共享模式。我在我的 Ubuntu 终端 (12.04) 上使用以下命令集:

adb shell
am start -n com.android.settings/.TetherSettings
sleep 7
input tap 162 159
input tap 385 607

This method works fine when the commands are executed one by one, but I'm not able to run them as normal shell script. Please help! Here is the complete script:

当命令一个一个执行时,此方法工作正常,但我无法将它们作为正常的 shell 脚本运行。请帮忙!这是完整的脚本:

#!/bin/sh
adb shell
am start -n com.android.settings/.TetherSettings
sleep 7
input tap 162 159
input tap 385 607

I guess, it can't find the path to adb in my system. I've tried replacing the first line with the actual path to adb tool in SDK directory. That didn't work either. Any work around for this? (Sorry if the question seems silly. I'm really new to bash scripting!)

我猜,它在我的系统中找不到 adb 的路径。我尝试用 SDK 目录中 adb 工具的实际路径替换第一行。那也没有用。有什么解决办法吗?(对不起,如果这个问题看起来很愚蠢。我真的是 bash 脚本的新手!)

EDIT: Updated script:-

编辑:更新脚本:-

#!/bin/sh
cd /home/evinish/Documents/Android/adt-bundle-linux-x86_64-20130219/sdk/platform-tools
adb shell "
am start -n com.android.settings/.TetherSettings
sleep 7
input tap 162 159
input tap 385 607
"

采纳答案by Vinit Shandilya

Thanks everybody! I finally solved the problem. Here is the updated script:

谢谢大家!我终于解决了这个问题。这是更新后的脚本:

#!/bin/sh
cd /home/evinish/Documents/Android/adt-bundle-linux-x86_64-20130219/sdk/platform-tools
./adb devices
./adb shell "
am start -n com.android.settings/.TetherSettings
sleep 15
input tap 162 159
input tap 385 607
"
sleep 10

The only problem was missing "./" before adb.

唯一的问题是在 adb 之前缺少“./”。

回答by Stephen Niedzielski

adb shellopens a shell on your Android device. The subsequent commands are entered in the context of that shell. Add quotes around the remote commands:

adb shell在您的 Android 设备上打开一个外壳。后续命令在该 shell 的上下文中输入。在远程命令周围添加引号:

adb shell "
am start -n com.android.settings/.TetherSettings
sleep 7
input tap 162 159
input tap 385 607
"