简体   繁体   English

Shell脚本可在bash中工作,但不能在“ adb shell”中工作

[英]Shell script works in bash but not on 'adb shell'

Below shell script works perfectly fine in bash shell. 下面的shell脚本在bash shell中运行良好。 But produces an error in android shell. 但是在android shell中产生一个错误。 May be it has got to do something with the shell that android is using. 可能与android正在使用的外壳有关。 But how can I resolve it? 但是我该如何解决呢? If not a perfect solution, an hack would also do for me. 如果不是完美的解决方案,那么黑客也可以为我服务。

Shell-script : Shell脚本:

func()
{
    local x
    x=${1:3:1}
    echo "x - $x"
    if [ "${x}" = " " -o -z "${x}" ]; then
      a="M"
    else
      a="S"
    fi
    echo "A - ${a} X - {x}"
}

func "abc=efg"

O/p In android shell, O / p在android shell中,

root:/ # sample.sh                                                  
x - =
/system/bin/sample.sh[14]: [: =: missing second argument
A - S X - {x}

O/p on bash shell(Linux): bash shell上的O / p(Linux):

PC:~$ ./sample.sh 
x - =
A - S X - {x}

When the value of $x is just = , it looks like: $x值为just = ,它看起来像:

if [ = = " " -o -z = ]; then

and it's apparently getting confused by the = that should be a value rather than an operator. 并且显然被=应该混淆,应该是一个值而不是一个运算符。

Change it to: 更改为:

if [ "x${x}" = "x " -o "x${x}" = "x" ]; then

The adb shell was a direct descendant of BSD ash, of which dash is also. adb外壳是BSD灰的直接后代,连字符也是。 The adb shell in more recent android versions is mksh standard shell of non-emulator builds on Android 4.0 and newer . 新的android版本中的adb shell 是mksh 非仿真器的标准shell,基于Android 4.0及更高版本构建

From the error text it is probable that the shell is some mksh version. 从错误文本来看,shell可能是某些mksh版本。

As the mksh in android is under heavy changes and improvements, I recommend you to take dash as the target shell. 由于android中的mksh正在进行重大更改和改进,因此建议您将破折号作为目标shell。 It is available in most linux distributions and is the default system shell for debian and derivatives. 它在大多数linux发行版中都可用,并且是debian和衍生产品的默认系统外壳。 It has less features but it is very difficult that something that work in dash will not work in the mksh of adb. 它具有较少的功能,但是很难使在ADB的mksh中不能使用破折号的东西起作用。

{1:3:1} {1:3:1}

In particular, this expression: 特别是,此表达式:

x=${1:3:1}

has no meaning in dash (it does work, however, in mksh). 在破折号中没有任何意义(但是在mksh中有效)。 It could be replaced with this: 可以替换为:

x=${1#???}; x=${x%"${x#?}"}

A bit longer, but gets the job done in any shell. 更长一点,但是可以在任何 shell中完成工作。

[ [

Also, there is a problem with the […] test, it seems to have too many "parts" to be reliable. 另外, […]测试存在问题,似乎有太多“零件”,其可靠性不足。 Dividing the test into two (or more if needed) is the usual remedy: 通常的解决方法是将测试分为两个(如果需要,可以进行多个测试):

if [ "${x}" = " " ] || [ -z "${x}" ]; then

The || || is also an or (with the same preference as && , the and). 还是or(与&&和the相同)。
Which also works in any shell. 这也适用于任何外壳。

With a final change of (a missing $ for variable x): 最后更改为(变量x缺少$):

 echo "A - ${a} X - ${x}"

We get the whole script as: 我们将整个脚本获取为:

func()
{
    local x
    x=${1#???}; x=${x%${x#?}}
    echo "x - $x"
    if [ "${x}" = " " ] || [ -z "${x}" ]; then
      a="M"
    else
      a="S"
    fi
    echo "A - ${a} X - ${x}"
}

func "abc=efg"

And running it in dash, bash or mksh, gives: 并在破折号,bash或mksh中运行它可以得到:

x - =
A - S X - =

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM