shell alias has cache? Why? - android

I write a alias command for get version of android in PC using adb shell, like this:
alias av="echo v:`adb shell getprop ro.build.version.release`;
echo sdk:`adb shell getprop ro.build.version.sdk`;
echo model:`adb shell getprop ro.product.model`;
echo display:`adb shell getprop ro.build.display.id`;"
I connect one device, it right outputs :
v:4.0.4
sdk:15
model:ZTE N983
display:N983V1.0.0B06
But when I change another device. It also output last text. When i open new shell, it output rights again. Why?
Alias has a cache? How to solve it ?
Thanks.

Backticks are evaluated immediately.
Use something like
alias av="echo -n v; adb shell getprop ro.build.version.release; ..."
to make the adb commands be executed at the time of alias execution.

The final result
1 alias av="echo -n v:; adb shell getprop ro.build.version.release; echo -n sdk:;adb shell getprop ro.build.version.sdk; \
echo -n model:;adb shell getprop ro.product.model; echo -n display:;adb shell getprop ro.build.display.id";
2 alias av='echo v: adb shell getprop ro.build.version.release; echo sdk: adb shell getprop ro.build.version.sdk; echo model: adb shell getprop ro.product.model; echo display: adb shell getprop ro.build.display.id'
But The second way cannot begin a new line using \

Related

Get the exact value from android getprop

I wish to get specific value of properties from adb command getprop
To get specific value I use:
adb shell getprop ro.product.display_name
adb shell getprop ro.bootloader
adb shell getprop ro.serialno
adb shell getprop ro.product.model
Result example:
Galaxy Watch5
RFT4JD6GHK
RGHJKABVGTS
SM-R860
But this is not good, cos for each value, I need to do getprop each time. I wish to ask it once per device and then parse the result to get values.
Something like:
adb shell getprop
and then what I get from stdout is long list with all properties and values
...
[ro.build.version.release]: [4.2.2]
[ro.product.display_name]: [Galaxy Watch5]
[ro.bootloader]: [RFT4JD6GHK]
[ro.hardware]: [qcom]
[ro.opengles.version]: [196108]
[ro.product.brand]: [Verizon]
[ro.product.manufacturer]: [samsung]
[ro.serialno]: [RGHJKABVGTS]
[ro.product.model]: [SM-R860]
...
How to parse this big list end get same result as separate commands? I try bellow but I get the whole thing with brackets:
adb shell getprop | grep "ro.product.display_name\|ro.bootloader\|ro.serialno\|ro.product.model"
Result:
[ro.product.display_name]: [Galaxy Watch5]
[ro.bootloader]: [RFT4JD6GHK]
[ro.serialno]: [RGHJKABVGTS]
[ro.product.model]: [SM-R860]
I solve it, but I'm not sure if there is easier solution:
adb shell getprop | grep 'ro.product.display_name\|ro.bootloader\|ro.serialno\|ro.product.model' | cut -d ":" -f 2 | sed 's:^..\(.*\).$:\1:'
Using awk and defining an array with the keys you want (ro.product.display_name used as an example here), you can do
adb shell getprop | awk -F '[][:]' 'BEGIN {keys["ro.product.display_name"]++} $2 in keys {print $5}'

adb.exe: unknown command am adb.exe: unknown command sleep error keeps poping up when am trying to run the shell script on my rooted android device

Hi guys am new to the shell scripting.. please do not mind if acted like a noob.
The following error keeps poping up when am trying to run the shell script on my android device
#!/bin/bash
for i in $(seq 100)
do
echo "Time: $i"
adb -s $* shell am start com.android.camera/.Camera -W -S
adb -s $* shell sleep 3
adb -s $* shell input tap 540 1994
adb -s $* shell input tap 540 1994
adb -s $* shell input tap 540 1994
count=`adb -s $* shell ls /sdcard/DCIM/Camera/ | wc |awk '{print $1}'`
echo Count: $count
if [[ count -gt 500 ]]; then
break
fi
adb -s $* reboot
adb -s $* wait-for-device
adb -s $* shell sleep 40
adb -s $* shell input tap 500 1200
done
Result:
adb.exe: unknown command am
adb.exe: unknown command sleep
you can't run adb commands on your android device. instead the adb binary must installed on host. the script is a linux script and can not run from windows. prepare bootable usb flash drive and run this script from any linux terminal
furthermore some of your commands requires root permissions. therefore sush must invited (and permissions granted on superuser app on touchscreen)
adb shell su -c "am start com.android.camera/.Camera -W -S"
awk is not available on android. to make it more clear that these pipes are actually running on host, quote the android commands (or even better avoid awk at all)
count=$(adb shell "ls -1 /sdcard/DCIM/Camera" | wc | awk '{print $1}')
count=$(adb shell "ls -1 /sdcard/DCIM/Camera | wc -l")
am is actually a shell script pointing to am.jar this requires full booted android. if you try to run in recovery mode you should at least run the framework

How to start daemon on android by adb

Can you help me please, my head is swollen...
I am trying to run daemon on android emu/device by command:
adb -s <device_name> shell su -c /dir/daemon <port_number>
but nothink happens, and no errors!
If I do:
adb -s <device> shell
and then form shell cmdline:
su -c /dir/daemon <port_number>
than its work fine. I try use shell-script and run:
adb -s <device_name> shell sh su -c /dir/script.sh <port_number>
and I even try to generate script from Qt code with port number and use:
adb -s <device_name> shell sh su -c /dir/script.sh
but its not helped... problem that if I enter to adb shell previous to run daemon - its work. Another problem is on different device/emu/iso_image different command format could not work. For example:
adb -s <device_name> shell su -c /dir/daemon <port_number> // work at emu, but not at iso
and vice:
adb -s <device_name> shell su -c "/dir/daemon <port_number>" // work at iso
etc.
Everybody can answer what's the matter?
Sorry for my english
adb shell su -c "/dir > /dev/null 2> /dev/null < /dev/null &"
or
shell su -c /data/local/tmp/start_daemon.sh
#!/system/bin/sh
/data/local/tmp/nohup
/data/local/tmp/daemon <portn_number> &

"adb shell getprop persist.sys.country" returning empty string

I'm trying to get from the device (running android L) the country and the language with the following commands:
adb shell getprop persist.sys.country
adb shell getprop persist.sys.language
But both return a empty string.
When I try another getprop command, like:
adb shell getprop ro.product.model
It returns the correct value.
Anyone knows what is happening?
Found the solution:
Android has changed this prop name in the last versions. You can use now:
adb shell getprop ro.product.locale
or
adb shell getprop persist.sys.locale
There are some other useful commands related to this question:
adb shell getprop ro.csc.country_code //e.g. [Germany]
adb shell getprop ro.csc.countryiso_code //e.g. [DE]
adb shell getprop ro.csc.sales_code //e.g. [VD2]
It gives information about country and operator the Binary is dedicated to (if not modified of course).

How to use su command over adb shell?

I need to make a script that executes a lots of thing on Android device, my device is rooted, when I enter on the shell, I can give the command su, and it works but I need pass this command like:
adb shell "
su;
mv /sdcard/Download/app_test /data/local;
cd /data/local;
./app_test;
exit;
exit;
"
when I put some commands before the su it works, according what I read su creates a new shell that return immediately, but I need give commands on the su shell, how can I do that?
Well, if your phone is rooted you can run commands with the su -c command.
Here is an example of a cat command on the build.prop file to get a phone's product information.
adb shell "su -c 'cat /system/build.prop |grep "product"'"
This invokes root permission and runs the command inside the ' '.
Notice the 5 end quotes, that is required that you close ALL your end quotes or you will get an error.
For clarification the format is like this.
adb shell "su -c '[your command goes here]'"
Make sure you enter the command EXACTLY the way that you normally would when running it in shell.
On my Linux, I see an error with
adb shell "su -c '[your command goes here]'"
su: invalid uid/gid '-c'
The solution is on Linux
adb shell su 0 '[your command goes here]'
The su command does not execute anything, it just raise your privileges.
Try adb shell su -c YOUR_COMMAND.
By default CM10 only allows root access from Apps not ADB. Go to Settings -> Developer options -> Root access, and change option to "Apps and ADB".
1. adb shell su
win cmd
C:\>adb shell id
uid=2000(shell) gid=2000(shell)
C:\>adb shell 'su -c id'
/system/bin/sh: su -c id: inaccessible or not found
C:\>adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
C:\>adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
win msys bash
msys2#bash:~$ adb shell 'su -c id'
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2#bash:~$ adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2#bash:~$ adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
2. adb shell -t
if want run am cmd, -t option maybe required:
C:\>adb shell su -c am stack list
cmd: Failure calling service activity: Failed transaction (2147483646)
C:\>adb shell -t su -c am stack list
Stack id=0 bounds=[0,0][1200,1920] displayId=0 userId=0
...
shell options:
shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
run remote shell command (interactive shell if no command given)
-e: choose escape character, or "none"; default '~'
-n: don't read from stdin
-T: disable pty allocation
-t: allocate a pty if on a tty (-tt: force pty allocation)
-x: disable remote exit codes and stdout/stderr separation
Android Debug Bridge version 1.0.41
Version 30.0.5-6877874
for my use case, i wanted to grab the SHA1 hash from the magisk config file. the below worked for me.
adb shell "su -c "cat /sbin/.magisk/config | grep SHA | awk -F= '{ print $2 }'""
I opt for the following:
adb shell su root <your command>
e.g., to access the external storage (sd card):
adb shell su root ls storage/0/emulated

Categories

Resources