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 android as follows:
#!/system/bin/sh
c=1
if [ $c == 1 ]
then
echo c is 1
else
echo c is 0
fi
It gives an error like:
[: not found
EDIT
Is there any other logic to check the value of $c, whether it is 1 or 0 ?
Android shell have problem with [] in if so is there any other way to check the value of c ?
andriod shell sh is actually a link to busybox, and it is invoked as
busybox sh
you need setup [ applets manually
busybox ln -s /your_original_sh_path/busybox [
if you don't know where busybox is put, try list the /system/bin/sh which you give
ls /system/bin/sh
busybox which busybox
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....!!!
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.
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
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
How about checking that the .sh file doesn't contain a carriage return before line feed.
Windows \r\n -> CR LF
Unix \n -> LF
use /system/bin/cmp for equality test.
if you need numerically test, substitute $(($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
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
Related
Using adb shell to run commands on an android device, I get different results when running ls with or without a wildcard ( globbing, i.e * ).
When running ls without a wildcard, the last path is displayed properly. When running ls with a wildcard, the path is displayed with an : in the end of it for some reason. The actual file does not have a : in its path.
My issue is specifically with the last file: /data/data/com.kauf.wrapmyFaceFunphotoeditor/files/DV-com.com.kauf.wrapmyFaceFunphotoeditor-2020-05-17-17-44-30-DEBUG.txt:
it has an : in the end which isn't supposed to be there
Why does using a wildcard in ls add characters to the result path?
Edit, environment details: Windows 10 / Android 7, the code is running on sh. I've ran adb shell to get to this command prompt, and doing it in one line (i.e adb shell su -c ls ...) returns similar results, same for adb shell command ...; also clarified the question.
As described in Why you shouldn't parse the output of ls, ls's behavior is not always well-defined. It's generally safer to use NULs (if you don't have any control or knowledge of filenames) or newlines (if you have reason to be certain that filenames can't contain them) to directly delimit a list of values emitted by the shell. Consider, then:
# output is separated by NULs, which cannot possibly exist in filenames
printf '%s\0' /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*
...or...
# output is separated by newlines; beware of a file named DV-evil<newline>something-else
printf '%s\n' /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*
Note that if you're passing this through extra unescaping layers, it may be necessary to double up your backslashes -- if you see literal 0s or ns separating filenames in your output, that's evidence of same.
Note also that if no matching files exist, a glob will expand to itself, so you can get an output that contains only the literal string /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*; in bash this can be suppressed with shopt -s nullglob, but with /bin/sh (particularly the minimal busybox versions more likely to be available on Android) this may not be available. One way to work around this is with code similar to the following:
# set list of files into $1, $2, etc
set -- /data/data/com.kauf.wrapmyfacefunphotoeditor/files/DV-*
# exit immediately if $1 does not exist
if [ "$#" -le 1 ] && [ ! -e "$1" ]; then
exit
fi
# otherwise, print the list in our desired format
printf '%s\0' "$#"
I want to declare character variables and then write those variables one after the other in order to form a command. Example:
#!/system/bin/sh
tt=e;rr=c;uu=h;yy=o;
zz=i;ll=f;pp=n;cc=t
x=29
$zz$ll [ "$x"-eq 29 ]
$cc$uu$tt$pp
$tt$rr$uu$yy "yes"
$ll$zz
This code should read:
if [ "$x" -eq 29 ]
then
echo "yes"
fi
This works for the "echo" command but won't work for "if".
Always getting errors: if not found, then not found, fi not found.
I've tired surrounding with quotes and braces.
This is being done on android.
It turns out that i can achieve the desired outcome by utilizing the fact that echo will work regardless. so I echo the entire contents of the shell script in question (test.sh) and run commands in another bash instance reading from stdin.
Modified code now is
tt="e";rr="c";uu="h";yy="o";_1="i";ll="f";pp="n";cc="t"
x=29
"$1" "
$_1$ll [ "$x" -${tt}q 29 ]
$cc$uu$tt$pp
$tt$rr$uu$yy \"yes this file ran without error\"
$ll$_1
"
to run this:
/system/test.sh echo | sh -
I'm running stock android (5.1.1) on my nexus 5 and trying to use a script to kill a process (kill_process.sh).
while true; do
PROCESS=$(pgrep -l whatsapp)
if [[ $PROCESS == *"whatsapp"* ]]; then
kill $(pgrep -f whatsapp)
fi
done
I'm able to have scripts running at boot using the app "Universal Init.d", but this particular script does not seem to be working at boot. It does work as long as I ssh into my phone and run it manually. E.g., as soon as I launch whatsapp it's being killed right away.
Does anybody have any ideas what I could do to make it work at boot?
Also, a while loop is probably not the most efficient way to do this...so any ideas are welcome.
Edit1:
changed the if statement to
case $PROCESS in *"whatsapp"*)
kill $(pgrep -f whatsapp)
esac
Edit2: is there a way to daemonize a script?
Edit3: I set up another script containing only one line start-stop-daemon --exec /path_to_main_script/kill_process.sh -S. I only put the new script in the /system/etc/init.d directory and the kill_process.sh script into a different subdirectory in /system/etc/. This way the new script is being run at boot which then calls the kill_process.sh script. But it's not running stable, which is caused by the "Universal Init.d" app: when the app crashes the script stops running. Any ideas?
Disclaimer: I never used the app Universal Init.d nor do I have a lot of Android experience. But I did some tests on my desktop:
me:~$ bash -x /tmp/x
+ a=abcd
+ [[ abcd == *bc* ]]
+ echo 0
0 # correct
me:~$ dash -x /tmp/x
+ a=abcd
+ [[ abcd == *bc* ]]
/tmp/x: 3: /tmp/x: [[: not found
+ echo 127
127 # [[ not supported
me:~$ busybox sh -x /tmp/x
+ a=abcd
+ [[ abcd == *bc* ]]
+ echo 1
1 # wrong!
The script:
a=abcd
[[ $a == *bc* ]]
echo $?
Conclusion: You should check the shell which the app uses. It's likely that the [[ syntax in combination with * fails. I think it's busybox or dash there...
For saving you battery, I recommend at least replacing while true by
while sleep 1; do
But also this is just a hack. You should look for an event-based solution (cannot help you there)
First, sorry if my question is obscure or in an inconvenient format. This is my first post here :D.
My issue is that I have a script, let's say test.sh which reads an input, and validates if it's a positive integer (reg ex used from this post:
BASH: Test whether string is valid as an integer?):
#!/bin/sh
echo -n " enter number <"
read num
if [[ $num =~ ^-?[0-9]+$ ]] #if num contains any symbols/letters
then # anywhere in the string
echo "not a positive int"
exit
else
echo "positive int read"
fi
I am running this script on my android device (Xiaomi Mi3 w) using adb shell and the error:
syntax error: =~ unexpected operator keeps displaying.
First, is my regex even correct?
Second, any hints on how I can overcome this syntax error?
The default shell in Android is mksh. It is not 100% compatible with bash. So do not expect all bash recipes to work without changes.
For the description of features supported by mksh - read its manual page.
This is a GNU bash POSIX regular expression. In Korn Shell, you can use extglob regular expressions to the same effect:
if [[ $num = ?(-)+([0-9]) ]]; then
…
See the section “File name patterns” in the manpage for details.
I had to use ksh expression as shown below to get this to work.
case $num in
+([0-9])*(.)*([0-9]) )
# Variable positive integer
echo "positive integer"
;;
*)
# Not a positive integer
echo "NOPE"
exit
;;
esac
I have a bash script which goes as follows
if [ -f "/sdcard/testfile"]
then
echo "exists" > /sdcard/outfile
else
echo "does not exist" > /sdcard/outfile
fi
I have sufficient permission to run this with /system/bin/sh.
I am calling this script from my application and running this with /system/bin/sh.
But after running I am getting false, even if the file '/sdcard/testfile' is there.
When I am explicitly running in adb shell, I am getting this error
[: not found
Is there any other way to accomplish this task? I cannot just use java.io.File because of permission issue of application; therefore, I am adhering to shell script (command).
I need the output in the application itself. I mean,
if(filesAreAvailable)
executeSomething();
else
executeSomethingElse();
Basically I am programmatically writing this script in the /data/data/myPackageName/files directory and for calling the command:
if [ -f "/sdcard/testfile"]
as
fileWriterScript.write("if [ -f \"/sdcard/testfile\" ]\n")
When using test, you need a space after the opening bracket and before the closing bracket.
From man test:
SYNOPSIS
test expression
[ expression ]
So change:
[ -f "/sdcard/testfile"]
to:
[ -f "/sdcard/testfile" ]
If you need to use this in bash script then you can do it that way:
if [[ `adb shell ls /sdcard/path/to/your.file 2> /dev/null` ]]; then
echo "File exists";
else
echo "File doesn't exist";
fi
you could do a ls and then check the output - when it contains "No such file or directory" - the file is not there. But still IMHO you need the permission
I used this script. It's checking if a file exist on the phone.
#!/bin/bash
RESULT=$(adb shell "[ -f $1 ] || echo 1")
if [ -z "$RESULT" ]; then
echo "File exists!"
else
echo "File not found!"
fi
I made it work using another answer posted in stackoverflow. Reference
https://stackoverflow.com/a/6364244/2031060