Shell script how to create a right declaration in Android - android

i don't know what the right title for this question but i have a problem like this:
i have a file inside a folder, the name is test.txt and have a value 6
i open the terminal and use su command to get root access (just in case), then i try this script
tek="/a/test.txt"
if [ "$tek" -le "8" ]; then
echo " 6 <= 8 "
else
echo "nice"
fi
and there is an error sush: /a/test.txt: unexpected '/' tmn
i try another way like this tek="a/test.txt" and i get an error sush: a/test.txt: zero divisor tmn
can someone tell me what is wrong with my script?
i do this all in Android device
if there is someone know what is the right title for this question please change it, i'll be so grateful

To read the first line, you have to call $(head -n 1 a/test.txt), not just reference the file path, so try this instead:
#!/bin/sh
tek=$(head -n 1 a/test.txt)
if [ "$tek" -le "8" ]; then
echo "6 <= 8"
else
echo "nice"
fi

Related

Concatenate variables as commands in bash

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 -

linux shell - can't compare strings with: adb shell getprop ro.product.brand

This is really odd...
I can't get this test to result in true in my linux shell and I can't figure out why.
#!/bin/bash
a=$(adb shell getprop ro.product.brand)
adb shell getprop ro.product.brand
if [ "$a" == "Huawei" ]; then
echo "Success"
else
echo "Failed"
fi
The script just outputs:
Huawei
Failed
Whereas this script:
b=$(whoami)
whoami
if [ "$b" == "amo" ]; then
echo "Success"
else
echo "Failed"
fi
...outputs:
amo
Success
Can anyone help me understand that?
I already tried cutting away spaces or line breaks in $a by piping to cut or sed but I get the same result...
I suggest this as a way to remove leading/trailing whitespace :
# Trims $1
# If $2 supplied, assigns result to variable named $2
# If $2 not present, echoes the value to stdout
trim()
{
if
[[ $1 =~ ^[[:blank:]]*(.*[^[:blank:]])[[:blank:]]*$ ]]
then
local result="${BASH_REMATCH[1]}"
else
local result="$1"
fi
if
(( $# > 1 ))
then
printf -v "$2" %s "$result"
else
printf %s "$result"
fi
}
This function uses no external program, so has low overhead.
Maybe a quick explanation of the regular expression...
^[[:blank:]]*(.*[^[:blank:]])[[:blank:]]*$
It matches all leading and trailing whitespace (no surprise there)
In the middle, it matches any string of characters that ends with a non-blank and saves that as a sub-expression for access with BASH_REMATCH
If there were no "non-blank" character specified to end the middle portion, the greedy .* would eat everything up until the end of the string, including trailing blanks.
The .* is, on the other hand, certain to begin with a non-blank, because the greedy initial [[:blank:]]* will only stop when encountering a non-blank.
Depending on your need, you may also use [[:space:]] instead of [[:blank:]] (difference explained here : https://en.wikipedia.org/wiki/Regular_expression#Character_classes). Basically, [[:blank:]] matches tabs and spaces, and [[:space:]] also matches newlines, carriage returns, and a few more.

how to check if a file exist using adb shell

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

Android shell script read

I'm trying to get a shell script to take values. What I currently have is:
#!/system/bin/sh
echo "Enter the page numbers"
read page_no_first;
read page_no_final;
#
echo $page_no_first
echo $page_no_final
The echos are simply there for debug, and the problem is that they display as blanks.
The terminal results are as such:
scriptname
Enter the page numbers
1
1
5
5
Verbatim. That is the echo commands simply produce two empty lines.
I've found a sort of work around, and another related problem:
#!/system/bin/sh
echo "Enter the page numbers"
read page_no_first -s
read page_no_final -s
#
echo $page_no_first
echo $page_no_final
This should give me the read without it repeating the input. Instead, what it gives me is:
scriptname
Enter the page numbers
1
1
: is readonlyriptname[3]: read: -s
3
3
: is readonlyriptname[3]: read: -s
1
3
Ironically, this successfully writes the numbers to the variables, however, it a) does not give me a silent read, and b) gives me some funny error. Googling it doesn't help, since it's too vague.
Any help?
This work as expected (at least as I think it should):
#! /system/bin/sh
echo "Enter the page numbers"
echo -n "First: "
read page_no_first
echo -n "Last: "
read page_no_last
echo "First: $page_no_first Last: $page_no_last"
on android 4.2

shell script in android gives [: not found

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

Categories

Resources