passing adb output to variable in bash script - android

im trying to create bash script to install split APKs manually with adb shell
that requires to get session id using the command bellow
command
SESSION='pm install-create -S 42211368'
this will output something like : Success: created install session [547376362]
547376362 will be the session ID
I want to pass 547376362 into SESSION Variable
sh < pm install-write -S 24628703 ${SESSION} 0 /sdcard/YTAPKM/base.apk
so result shall be "sh < pm install-write -S 24628703 547376362 0 /sdcard/YTAPKM/base.apk"

grep is sufficient for this.
SESSION=$(pm install-create -S 42211368 | grep -oE '[0-9]+')
sh < pm install-write -S 24628703 ${SESSION} 0 /sdcard/YTAPKM/base.apk
To explain what's happening a bit:
grep -E uses "extended" regular expressions (easier to work with)
grep -o outputs only the matching part, the integer in this case
SESSION=$(some_cmd) stores the stdout from some_cmd to the variable SESSION, and allows for pipes and such too

Related

Get application name from its package name via adb [duplicate]

I'm developing an application that uses ADB Shell to interface with android devices, and I need some way of printing out the application name or label of an application, given maybe their package name.
In short, I need a way of getting app names (i.e. "Angry Birds v1.0.0") for user installed applications through adb shell.
Any light on the matter? Any help is appreciated on this.
adb shell pm list packages will give you a list of all installed package names.
You can then use dumpsys | grep -A18 "Package \[my.package\]" to grab the package information such as version identifiers etc
just enter the following command on command prompt after launching the app:
adb shell dumpsys window windows | find "mCurrentFocus"
if executing the command on linux terminal replace find by grep
If you know the app id of the package (like org.mozilla.firefox), it is easy.
First to get the path of actual package file of the appId,
$ adb shell pm list packages -f com.google.android.apps.inbox
package:/data/app/com.google.android.apps.inbox-1/base.apk=com.google.android.apps.inbox
Now you can do some grep|sed magic to extract the path : /data/app/com.google.android.apps.inbox-1/base.apk
After that aapt tool comes in handy :
$ adb shell aapt dump badging /data/app/com.google.android.apps.inbox-1/base.apk
...
application-label:'Inbox'
application-label-hi:'Inbox'
application-label-ru:'Inbox'
...
Again some grep magic to get the Label.
A shell script to accomplish this:
#!/bin/bash
# Remove whitespace
function remWS {
if [ -z "${1}" ]; then
cat | tr -d '[:space:]'
else
echo "${1}" | tr -d '[:space:]'
fi
}
for pkg in $(adb shell pm list packages -3 | cut -d':' -f2); do
apk_loc="$(adb shell pm path $(remWS $pkg) | cut -d':' -f2 | remWS)"
apk_name="$(adb shell aapt dump badging $apk_loc | pcregrep -o1 $'application-label:\'(.+)\'' | remWS)"
apk_info="$(adb shell aapt dump badging $apk_loc | pcregrep -o1 '\b(package: .+)')"
echo "$apk_name v$(echo $apk_info | pcregrep -io1 -e $'\\bversionName=\'(.+?)\'')"
done
Inorder to find an app's name (application label), you need to do the following:
(as shown in other answers)
Find the APK path of the app whose name you want to find.
Using aapt command, find the app label.
But devices don't ship with the aapt binary out-of-the-box.
So you will need to install it first. You can download it from here:
https://github.com/Calsign/APDE/tree/master/APDE/src/main/assets/aapt-binaries
Check this guide for complete steps:
How to find an app name using package name through ADB Android?
(Disclaimer: I am the author of that blog post)
This is what I just came up with. It gives a few errors but works well enough for my needs, matching package names to labels.
It pulls copies of all packages into subdirectories of $PWD, so keep that in mind if storage is a concern.
#!/bin/bash
TOOLS=~/Downloads/adt-bundle-linux-x86_64-20130717/sdk/build-tools/19.1.0
AAPT=$TOOLS/aapt
PMLIST=adb_shell_pm_list_packages_-f.txt
TEMP=$(echo $(adb shell mktemp -d -p /data/local/tmp) | sed 's/\r//')
mkdir -p packages
[ -f $PMLIST ] || eval $(echo $(basename $PMLIST) | tr '_' ' ') > $PMLIST
while read line; do
package=${line##*:}
apk=${package%%=*}
name=${package#*=}
copy=packages$apk
mkdir -p $(dirname $copy)
if [ ! -s $copy ]; then # copy it because `adb pull` doesn't see /mnt/expand/
adb shell cp -f $apk $TEMP/copy.apk
adb pull $TEMP/copy.apk $copy
fi
label=$($AAPT dump badging $copy || echo ERROR in $copy >&2 | \
sed -n 's/^application-label:\(.\)\(.*\)\1$/\2/p')
echo $name:$label
done < <(sed 's/\r//' $PMLIST)
adb shell rm -rf $TEMP
So I extremely grateful to jcomeau_ictx for providing the info on how to extract application-label info from apk and the idea to pull apk from phone directly!
However I had to make several alteration to script it self:
while read line; do done are breaking as a result of commands within while loop interacting with stdin/stdout and as a result while loop runs only once and then stops, as it is discussed in While loop stops reading after the first line in Bash - the comment from cmo I used solution provided and switched while loop to use unused file descriptor number 9.
All that the script really need is a package name and adb shell pm list packages -f is really excessive so I changed it to expect a file with packages list only and provided example on how one can get one from adb.
jcomeau_ictx script variant do not take in to account that some packages may have multiple apk associated with them which breaks the script.
And the least and last, I made every variable to start with underscore, it's just something that makes it easier to read script.
So here another variant of the same script:
#!/bin/bash
_TOOLS=/opt/android-sdk-update-manager/build-tools/29.0.3
_AAPT=${_TOOLS}/aapt
#adb shell pm list packages --user 0 | sed -e 's|^package:||' | sort >./packages_list.txt
_PMLIST=packages_list.txt
rm ./packages_list_with_names.txt
_TEMP=$(echo $(adb shell mktemp -d -p /data/local/tmp) | sed 's/\r//')
mkdir -p packages
[ -f ${_PMLIST} ] || eval $(echo $(basename ${_PMLIST}) | tr '_' ' ') > ${_PMLIST}
while read -u 9 _line; do
_package=${_line##*:}
_apkpath=$(adb shell pm path ${_package} | sed -e 's|^package:||' | head -n 1)
_apkfilename=$(basename "${_apkpath}")
adb shell cp -f ${_apkpath} ${_TEMP}/copy.apk
adb pull ${_TEMP}/copy.apk ./packages
_name=$(${_AAPT} dump badging ./packages/copy.apk | sed -n 's|^application-label:\(.\)\(.*\)\1$|\2|p' )
#'
echo "${_package} - ${_name}" >>./packages_list_with_names.txt
done 9< ${_PMLIST}
adb shell rm -rf $TEMP

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.

"adb pull" throws "does not exist"

When I perform command:
for el in $(adb shell ls /mnt/sdcard/ | grep mem_); do adb pull /mnt/sdcard/${el} android_mem; done
I get:
' does not existmnt/sdcard/mem_AI
' does not existmnt/sdcard/mem_Alarms
' does not existmnt/sdcard/mem_Android
' does not existmnt/sdcard/mem_Autodesk
' does not existmnt/sdcard/mem_Cardboard
' does not existmnt/sdcard/mem_DCIM
...
But if I perform this, for example, adb pull /mnt/sdcard/mem_DCIM android_mem I get that 0 KB/s (20 bytes in 0.080s), ie ok. Why is this happens??
The problem is that adb shell ls /mnt/sdcard/ | grep mem_ is returning a \r at the end, so it can't pull the file properly.
So you need to remove it with sed -r 's/[\r]+//g', for example:
for el in $(adb shell ls /mnt/sdcard/ | grep mem_ | sed -r 's/[\r]+//g'); do adb pull /mnt/sdcard/${el} android_mem; done

How do I fix column count in adb shell?

When bash (or other shell) fails to guess the correct column count on your terminal, it gets inconvenient to edit long lines.
For usual Linux systems, there is script termsize that automatically fixes the terminal size. But typical Android does not have Python, so termsize can't work.
How do I fix the terminal width in adb shell?
This snippet can assist you for setting up column count:
echo "\033[18t"; { sleep 1; echo -n "COLUMNS="; } & grep -m 1 -o '[0-9]*t' | { sleep 2; grep -m 1 -o '[0-9]*'; }
Usage:
Open adb shell;
Insert the snippet into command line;
Execute it (press Enter) and immediately press Enter again.
Wait for 2 seconds: it should print COLUMNS= after 1 second and the columns count after another 1 second;
Copy COLUMNS=NNN into console and press Enter yet again.
Example session:
$ adb shell
9]*t' | { sleep 2; grep -m 1 -o '[0-9]*'; } <
[1] 17212
^[[8;38;149t
COLUMNS=149
[1] + Done { sleep 1 ; echo -n "COLUMNS=" ; }
shell#hwH60:/ $ COLUMNS=149
shell#hwH60:/ $

How to get the PID from PS command output in Android shell

Can anyone tell how can I get the PID from the output of PS command in Android shell.
For example from the output:
u0_a51 20240 38 132944 22300 ffffffff 40037ebc S com.example.poc_service
pid value 20240 is to be got. I tried
ps -ef | grep com.example.poc_service
but to no avail. Also pgrep is not being recognized.
If you have shell access in Android you can also use pidof:
# pidof com.example.poc_service
20240
However, be careful as there may be multiple processes matching...
Its pretty nasty but it works:
for pid in `ls /proc`; do
cmd=`cat $pid/cmdline 2> /dev/null`;
if [ "X$cmd" == "Xcom.example.poc_service" ]; then
echo $pid;
fi
done
or as one line:
for pid in `ls /proc`; do cmd=`cat $pid/cmdline 2> /dev/null`; if [ "X$cmd" == "X/system/bin/mm-qcamera-daemon" ]; then echo $pid; fi done
Neither grep, egrep, fgrep, rgrep is available in Android.
If you are working on Unix, Linux, Mac or Cygwin, you can pipe the output of adb shell command to get the result you want.
$ adb shell ps |grep settings
system 23846 71 111996 22676 ffffffff 00000000 S com.android.settings
$ adb shell ps |grep settings |awk '{print $2}'
23846

Categories

Resources