Shell script in android gives [: not found

I have this script that runs 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 it in android as follows:

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

An error message appears:

 [: not found 

EDIT

Is there any other logic to check the value of $c , is it 1 or 0?

Does the Android shell have a problem with [] if there is any other way to check the value of c?

+3
source share
8 answers

usually [ is an alias for test ,

Linux is in test mode

 /usr/bin/test 

and

 if [ $c == 1 ] 

estimated as

 if test "$c" = 1 

but here in android there is no test

so if with [] will not work anyway ...

I will go on to compile a test for Android and test it .... !!!

+4
source

andriod shell sh is actually a reference to busybox, and it is called as

 busybox sh 

you need to configure [applets manually

 busybox ln -s /your_original_sh_path/busybox [ 

if you don't know where busybox is loaded try listing / system / bin / sh that you give

 ls /system/bin/sh busybox which busybox 
+5
source

Android does not provide a complete UNIX environment; it is not a UNIX operating system. It has some things in common, similar to how Windows has some similarities with UNIX. Some Android devices and ROMs try to provide more of a UNIX-like environment than others, but you cannot rely on most of the standard shell scripting tools that are installed when you think about compatibility between devices.

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

Here is an example of writing killall in the Android /system/bin/sh environment: http://en.androidwiki.com/wiki/Android_Shell_tips_and_tricks You can also use various parameter extensions to create some logic, you can see an example of this in Bifacle Wifi Tether scripts .

+2
source

Use bash:

#!/system/bin/bash

or

#!/system/xbin/bash

You can check where your sh binary is located 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 
+1
source

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

 [ $c -eq 1 ]; 

Also your location for Bash (sh) may be incorrect at the top of the file:

 #!/system/bin/sh 
+1
source

How to verify that a .sh file does not contain a carriage return before a line feed.

Windows \ r \ n → CR LF

Unix \ n → LF

+1
source

use / system / bin / cmp for equality test. if you need a numerical test, replace $ (($ c == 1)) with $ c

 #!/system/bin/sh echo $c >/tmp/a echo 1 >/tmp/b if cmp /tmp/a /tmp/b echo c is 1 else echo c is 0 fi 
0
source

I ran into this problem and found a solution (on another site)

 if [[ $b -gt 0]] then echo 'Hooray it works' else echo 'still works' fi 
0
source

Source: https://habr.com/ru/post/1442359/


All Articles