how to check if a file exist using adb shell - android

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

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 -

Get File Permissions and Parent Directory

How can I get the $DIR and $OPERM to return the correct values?
I am attempting to grab all sqlite3 databases and while the all list if I echo $i;, I need to work on:
the parent folder (in order to do what I need with $i)
store the original file permissions for later use on said file.
So far, all I've managed to do is get this to echo the words sed and stat
for i in $($BB find /system -iname "*.db")
do \
ORPERM=(stat -c "%a" $i);
DIR=(sed 's|/[^/]*$||' $i);
echo $DIR;
echo $OPERM;
done;
p.s. $BB = busybox
You missed the $ sign:
ORPERM=$( stat -c "%a" $i );
DIR=$( dirname $i );

String equality in Bash script [duplicate]

This question already has answers here:
How to compare strings in Bash
(12 answers)
Closed 8 years ago.
I want to simply test if an android device is rooted from a computer (for a root/backup/flash script).
So i have to test some locations for the su binary. Here is my code :
#!/bin/sh
#####################
## ROOT CHECK
SU_LOCATIONS="/system/bin/su /system/xbin/su"
check() {
echo "Checking for SU binary…"
for file in $SU_LOCATIONS
do
suFileResult=`$ADB shell "ls $file"`
echo "suFileResult = $suFileResult"
echo "file = $file"
if [ "$suFileResult" == "$file" ];
then echo "Su trouvé à $suFileResult"
fi
done
echo "$suFileResult" > tmpbak/suFil
}
The problem is that even if file == suFileResult, "if" return false. If i remove the spaces around ==, "if" will always return TRUE…
What am i doing wrong ? If you give an other way to test the file (and where), it would be perfect.
Thanks for your answers !
PS : the string could contain spaces here, such as :
/system/bin/su: No such file or directory
EDIT After answer :
By the way, i figured how to remove this annoying \r character : add
| tr -d '\r'
will remove this character in ALL the string (not only on the end). So in my case :
suFileResult=$(adb shell "[[ -e $file ]]; echo \$?;" | tr -d '\r')
By some weird reason adb returns strings with DOS line-ending (\r\n).
$ suFileResult="$(adb shell "ls $file")"
$ set | grep suFileResult
suFileResult=$'/system/bin/su\r'
So the proper condidtion statement may look like:
[[ $suFileResult == ${file}$'\r' ]]
Also, I should note that using ls for checking file existence is a quite odd approach, even though adb does not return an error code, you could print it to STDOUT:
suFileResult=$(adb shell "[[ -e $file ]]; echo \$?")
if [[ $suFileResult == $'0\r' ]]; then
echo "Su trouvé à $file"
fi

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

Check if directory exists using ADB and push a file if it does

I need to be able to test and see if a directory exists on the SD card of an Android device and then push a few files to that directory if it does exist.
So far, I have this:
adb %argument% shell if [ -e /sdcard/ ]; then echo "it does exist"; else echo "it does not exist"; fi;
But how can I let my batch script know that the directory exists so that it can continue to push the file to that directory?
Here is what I have done in batch script:
set cmd="adb shell ls | find /c "theFile" "
FOR /F %%K IN (' !cmd! ') DO SET TEST=%%K
if !TEST! GTR 0 (
echo the file exists
) else (
echo the file does not exist
)
There could be multiple files that fit the fileName, so I chose to have it test greater than 0.
To test for exact match and using bash in Linux (reference):
FILENAME_RESULT=$(adb shell ls / | tr -d '\015'|grep '^fileName$')
if [ -z "$FILENAME_RESULT" ];
then
echo "No fileName found."
else
echo "fileName found."
fi
I think you should list directory dir or ls and next analyze out using grep. If grep found directory script do something.
1) Just use adb shell ls /filepath > fileifpresent
2) grep locally if "No such file or directory" present, then NO
Else Directory Present
Here's how I'll do it checking for the exit status of the command
MyFile="Random.txt"
WorkingPath="/data/local/tmp/RandomFolder"
IsDir=`adb shell ls $WorkingPath &> /dev/null ; echo "$?"`
if [ $IsDir == 0 ] ; then
echo "Exist! Copying File To Remote Folder"
adb push $MyFile $WorkingPath
else
echo "Folder Don't Exist! Creating Folder To Start Copying File"
adb shell mkdir $WorkingPath
adb push $MyFile $WorkingPath
fi

Categories

Resources