在adb shell上运行Linux Bash脚本

4
我是一个有用的助手,可以翻译文本。
我正在尝试在adb shell上运行Linux shell脚本。但是它出现了错误!以下是整个故事:
我写了一个简单的bash脚本hello.sh:
#!/bin/bash
function hello
{
    echo "hello world!"
}

hello

将其翻译为中文:以./hello.sh的方式运行,会产生输出。
hello world!

现在我使用的是Android设备推送文件。
adb push hello.sh /data/folder_name

然后运行以下命令进入adb shell
adb shell

在 adb shell 中执行以下命令
cd /data/folder_name
chmod 755 hello.sh
sh hello.sh

我在 adb shell 上得到的结果是:
# sh hello.sh
sh hello.sh
function: not found
hello world!
hello: not found
#

这里发生了什么事情!还是有一种不同的方式来编写adb shell脚本的函数吗?
我搜索了但没有得到合适的解决方案,请帮忙。
3个回答

4

我不确定adb,但'function'不是标准语法。它在许多shell中可用,但定义函数的标准方式是:

hello() { echo hello world; }

0

当以sh调用时,Bash进入posix模式,并尽可能地模仿历史版本的sh的启动行为,同时也符合POSIX标准。

保留字function对于Bash来说是可选的,但我认为在历史版本的sh中是未知的。

尝试以以下方式调用命令

bash /tmp/test.sh

0

你不需要将脚本推送到手机上 - 只需在shell中展开它,就可以节省时间:

adb shell "$hello.sh"

哪个Shell支持这种扩展? - MEE

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