shell script in android gives [: not found

前端 未结 8 1321
一个人的身影
一个人的身影 2021-01-20 23:31

I have this script which works on my linux machine

#!/bin/sh
c=1
if [ $c == 1 ]
then
  echo c is 1
else
  echo c is 0
fi

But when I use this in

相关标签:
8条回答
  • 2021-01-21 00:07

    Android does not provide a full UNIX environment, it is not a UNIX operating system. It has some similarities, much like how Windows also has some similarities to UNIX. Some Android devices and ROMs try to provide more of a UNIX-like environment that others, but you cannot rely on most of the standard shell scripting tools being installed if you are thinking about cross-device compatibility.

    So for example, if you look at your GNU/Linux system, you can see that test and [ are actually programs. Try this: ls -l /usr/bin/[. Most Android installs do not include test or [. That means that if you want to try to do actual programming with Android's minimal shell environment, you have to use lots of odd tricks. You can install busybox to get a full UNIX shell environment, or you can even build busybox into your app. I do that when I need to include shell scripts in an app (for example, Lil' Debi and Commotion MeshTether).

    Here's an example of writing a killall in Android's /system/bin/sh environment: http://en.androidwiki.com/wiki/Android_Shell_tips_and_tricks You can also use the various parameter expansions to create some logic, you can see an example of that in the Barnacle Wifi Tether scripts.

    0 讨论(0)
  • 2021-01-21 00:13

    I run into this issue also and found a solution (on another site)

    if [[ $b -gt 0]]
    then
        echo 'Hooray it works'
    else
        echo 'still works'
    fi
    
    0 讨论(0)
  • 2021-01-21 00:14

    How about checking that the .sh file doesn't contain a carriage return before line feed.

    Windows \r\n -> CR LF

    Unix \n -> LF

    0 讨论(0)
  • 2021-01-21 00:16

    Use bash:

    #!/system/bin/bash

    or

    #!/system/xbin/bash

    You can check where your sh binary is pointing to on your Linux machine:

    ls -l /bin/sh
    

    Edit

    BTW, use:

    c=1
    if [ $c -eq 1 ]
    then
      echo c is 1
    else
      echo c is 0
    fi
    
    0 讨论(0)
  • 2021-01-21 00:19

    Think you using the wrong arithmetic operator and there is a syntax error of a missing ";": try

    [ $c -eq 1 ];
    

    Also your location for Bash (sh) might be wrong at the top of your file:

    #!/system/bin/sh
    
    0 讨论(0)
  • 2021-01-21 00:20

    generally [ is an alias for test,

    in Linux machine test is at

    /usr/bin/test
    

    and

    if [ $c == 1 ]
    

    is evaluated as

    if test "$c" = 1
    

    BUT here in android there is no test

    so if with [] will not work in any case...

    i will cross compile test for android and check it....!!!

    0 讨论(0)
提交回复
热议问题