Get File Permissions and Parent Directory - android

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 );

Related

How to handle .tar.md5 files

I was wondering about how to create / extract / verify .tar.md5 files. These files are used when flashing images to android devices, see here for example.
As far as I can tell the checksum is appended to the file like this:
cp file.tar file.tar.md5
md5sum file.tar >> file.tar.md5
Firstly I would like to know how to extract the file. Can I simply use tar -xf on the file.tar.md5?
How can I verify the integrity of the file? I would like to remove the last bytes (containing the checksum) from the file to obtain the original file back. I guess you would have to use a regexp to match the checksum file.tar? Is something like this implemented somewhere already?
First of all tar -xf should work since tar continues while it matches its' packing algorithm. If the file stops matching so would tar.
Also most archive managers such as 7-zip or winrar will open it if you remove the ".md5".
They might print error regarding mismatch with the end of the file, ignore it.
As for verifying the file:
print out the stored md5sum: tail -z -n 1 [File name here].tar.md5
calculate the md5sum of the tar part of the file: head -z -n -1 [File name here].tar.md5 | md5sum
What works for me with Ubuntu 19.10 is:
download single-file 4 GiB zip from sammobile com
unzip to several *.tar.md5
run the below command-line
.
for F in *.tar.md5; do echo -n "$F " &&
EXP=($(tail --lines=1 "$F")) &&
ACT=($(head --lines=-1 "$F" | md5sum)) &&
if [ ${EXP[0]} = ${ACT[0]} ]; then echo -n "md5ok " &&
tar --extract --file "$F" && echo "done"
else echo "FAIL"; fi; done &&
unlz4 --multiple --verbose *.lz4
AP_G965U1UEU3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT_meta.tar.md5 md5ok done
BL_G965U1UEU3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
CP_G965U1UEU3ARL1_CP11407818_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
CSC_OMC_OYM_G965U1OYM3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
HOME_CSC_OMC_OYM_G965U1OYM3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
…
But we should all try to get away from bash

How to pass the return value of a command as a parameter to another command in bash?

I'm writing a shell script in Android.
I need to determine the name of the owner of an app's data directory, assign it to a variable and change the owner of another directory (which happens to be a sub-directory of the first directory) to the owner of the first directory using a variable.
#assign the owner of the com.netflix.mediaclient to $user variable
user=`su -c "ls -l /data/data | grep com.netflix.mediaclient | cut -f2 -d' '"`
#change the owner of the shared_prefs directory to the value in $user variable
su -c 'busybox chown -R '$user:$user' /data/data/com.netflix.mediaclient/shared_prefs'
This and many other variations of the syntax of the second line does not work.
#using double quotes
su -c 'busybox chown -R "$user:$user" /data/data/com.netflix.mediaclient/shared_prefs'
#using no quotes
su -c 'busybox chown -R $user:$user /data/data/com.netflix.mediaclient/shared_prefs'
Use stat to find the owner and put $user:$user inside double quotes
user="$( busybox stat -c '%U' /data/data/com.netflix.mediaclient )"
busybox chown -R "$user:$user" /data/data/com.netflix.mediaclient/shared_prefs
Why '$user:$user' will not work?
Anything put inside the singles quotes are taken literally by bash and variable expansion will not occur.

Shell Script to remove lines of text in a text file

How can I remove a line of text in a file if it exists?
So far I am guessing
#!/sbin/sh
mount -o remount,rw /system;
# Make a backup first
cp /system/build.prop /system/build.prop.bak;
# Append
if [ grep -o 'wifi.supplicant_scan_interval' <<</system.build.prop > 1 ]; then
echo "YO";
fi;
mount -o remount,ro /system;
however, this shows me YO no matter of it is > 1 or < 1 (it does exist in the file), so this part seems wrong, also, I don't know how I could remove the line?
Can you help?
Code Update
#!/sbin/sh
mount -o remount,rw /system;
function check_prop(){
busybox grep $1 /system/build.prop;
return $?;
}
# Make a backup first
cp /system/build.prop /system/build.prop.bak;
echo $(check_prop 'wifi.supplicant_scan_interval');
# Append
if [ $(check_prop 'wifi.supplicant_scan_interval') > 1 ]; then
# Do my stuff here?
echo 'YO';
fi;
mount -o remount,ro /system;
is returning me a blank line, and YO. If I change it to < 1 it does the same thing
sed '/wifi.supplicant_scan_interval/d' inputfile
would remove lines matching wifi.supplicant_scan_interval
eg
$cat input
hello
world
hai
$sed '/world/d' input
hello
hai
if you want to delete line from file use -i option which is does the action inplace
sed -i '/wifi.supplicant_scan_interval/d' inputfile
EDIT
using grep to print all lines except the lines that match the patter.
grep -v 'wifi.supplicant_scan_interval' inputfile
eg
$ grep -v 'world' input
hello
hai
the -v option does the negation.

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

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