I know about > and >>, but cant capture output for below code.
I have 3 files, i want to open file one by one using CAT.
I would search for a specific string/message using GREP, if Found PASS else FAIL.
Code is working but can not get a output in file.
XXXXXXXXXX/XXX/XXX/postprocessing$ ls
demo.txt tag.txt tc1_adblog.txt tc2_adblog.txt tc3_adblog.txt
XXXXXXXXXX/XXX/XXX/postprocessing$ cat tc1_adblog.txt | grep 'ActivityRecord{4306a670 u0 com.example.android.notepad/.NotesList t9}' && echo "Test case 1: Pass" || echo "Test case 1: FAIL" >> result.txt
I/Timeline( 1002): Timeline: Activity_windows_visible id: ActivityRecord{4306a670 u0 com.example.android.notepad/.NotesList t9} time:12020671
Test case 1: Pass
XXXXXXXXXX/XXX/XXX/postprocessing$ ls
demo.txt tag.txt tc1_adblog.txt tc2_adblog.txt tc3_adblog.txt
>> result.txt is only being applied to echo after ||. You need to group the whole command like this:
s='ActivityRecord{4306a670 u0 com.example.android.notepad/.NotesList t9}'
{ grep -qF "$s" tc1_adblog.txt && echo "Test case 1: Pass" || echo "Test case 1: FAIL"; } >> result.txt
Use grep -F to match fixed strings
Use grep -q to avoid grep's output
Avoid useless cat
&& echo "Pass" >> result.txt || echo "Fail" >> result.txt
(grep "string" && echo "Pass" || echo "Fail") >> result.txt
Related
I'm trying to repurposed my Sony Xperia Z3 Compact into a lcd display project. In order to proceed, I would like to mod my Xperia phone so that the phone will automatically turn on when charger turn ON. I search around and found some script like https://android.stackexchange.com/questions/20021/automatically-power-on-android-when-the-charger-is-connected and some people mentioned that I should mod chargemon file. But when I replace this file, my phone cannot boot into the system.
#!/system/bin/sh
/system/bin/reboot
While checking the file, I notice that there 2 file chargemon and another is chargemon.stock. attached here chargemonfile . Actually this file have been edited during rooting process which I get it from xda forum https://forum.xda-developers.com/z3-compact/general/recovery-root-mm-575-lb-t3418714
#!/system/xbin/busybox sh
BUSYBOX=/system/xbin/busybox
VIB=/sys/class/timed_output/vibrator/enable
R_LED=/sys/class/leds/led:rgb_red/brightness
G_LED=/sys/class/leds/led:rgb_green/brightness
B_LED=/sys/class/leds/led:rgb_blue/brightness
LOG="/cache/mm_twrp_recovery.log"
WORKDIR="/cache/mm_twrp_recovery_keycheck"
MKDIR="${BUSYBOX} mkdir"
CHOWN="${BUSYBOX} chown"
CHMOD="${BUSYBOX} chmod"
TOUCH="${BUSYBOX} touch"
CAT="${BUSYBOX} cat"
SLEEP="${BUSYBOX} sleep"
KILL="${BUSYBOX} kill"
RM="${BUSYBOX} rm"
PS="${BUSYBOX} ps"
GREP="${BUSYBOX} grep"
AWK="${BUSYBOX} awk"
EXPR="${BUSYBOX} expr"
MOUNT="${BUSYBOX} mount"
LS="${BUSYBOX} ls"
HEXDUMP="${BUSYBOX} hexdump"
CP="${BUSYBOX} cp"
${RM} -f ${LOG}
echo "chargemon" >> ${LOG}
BOOTTWRP=0
if [ -e "/cache/recovery/boot" ]; then
echo " /cache/recovery/boot file found" >> ${LOG}
${RM} -f /cache/recovery/boot
echo 255 > ${R_LED}
echo 0 > ${G_LED}
echo 255 > ${B_LED}
BOOTTWRP=1
else
if [ ! -d "${WORKDIR}" ]; then
${MKDIR} ${WORKDIR}
${CHOWN} system.cache ${WORKDIR}
${CHMOD} 770 ${WORKDIR}
fi
if [ ! -e ${WORKDIR}/keycheck ]; then
${RM} ${WORKDIR}/keyevent*
${RM} ${WORKDIR}/keycheck_down
${RM} ${WORKDIR}/ps*
fi
echo 0 > ${R_LED}
echo 255 > ${G_LED}
echo 0 > ${B_LED}
echo 150 > ${VIB}
for EVENTDEV in $(${LS} /dev/input/event* ); do
SUFFIX="$(${EXPR} ${EVENTDEV} : '/dev/input/event\(.*\)')"
${CAT} ${EVENTDEV} > ${WORKDIR}/keyevent${SUFFIX} &
done
${SLEEP} 2
${PS} > ${WORKDIR}/ps.log
${CHMOD} 660 ${WORKDIR}/ps.log
for CATPROC in $(${PS} | ${GREP} /dev/input/event | ${GREP} -v grep | ${AWK} '{print $1}'); do
${KILL} -9 ${CATPROC}
done
${HEXDUMP} ${WORKDIR}/keyevent* | ${GREP} -e '^.* 0001 0072 .... ....$' > ${WORKDIR}/keycheck_down
if [ -s ${WORKDIR}/keycheck_down ]; then
echo " keycheck volume down - ok" >> ${LOG}
echo 255 > ${R_LED}
echo 0 > ${G_LED}
echo 255 > ${B_LED}
BOOTTWRP=1
fi
fi
if [ $BOOTTWRP -eq 1 ]; then
echo " remount rootfs rw" >> ${LOG}
mount -o remount,rw rootfs / 2>> ${LOG}
echo " copy busybox to /sbin" >> ${LOG}
${CP} /system/etc/mm_twrp_recovery/busybox /sbin
${CHOWN} 0.2000 /sbin/busybox
${CHMOD} 755 /sbin/busybox
BUSYBOX=/sbin/busybox
echo " copy boot_twrp_recovery.sh to /sbin" >> ${LOG}
${CP} /system/etc/mm_twrp_recovery/boot_twrp_recovery.sh /sbin
${CHOWN} 0.0 /sbin/boot_twrp_recovery.sh
${CHMOD} 755 /sbin/boot_twrp_recovery.sh
echo " copy recovery.twrp.cpio.lzma to /sbin" >> ${LOG}
${CP} /system/etc/mm_twrp_recovery/recovery.twrp.cpio.lzma /sbin
${CHOWN} 0.0 /sbin/recovery.twrp.cpio.lzma
${CHMOD} 644 /sbin/recovery.twrp.cpio.lzma
echo " unpack recovery.twrp.cpio.lzma" >> ${LOG}
${BUSYBOX} lzma -d /sbin/recovery.twrp.cpio.lzma
echo " exec boot_twrp_recovery.sh (twrp boot)" >> ${LOG}
exec /sbin/boot_twrp_recovery.sh
fi
echo 0 > ${B_LED}
echo 0 > ${R_LED}
echo 0 > ${G_LED}
echo " exec chargemon.stock (regular boot)" >> ${LOG}
exec /system/bin/chargemon.stock
exit 0
So im thinking, I shouldn't replace this file entirely but just add another line of code. Now, if anyone have experience regarding this maybe can share some input or help me modify the existing script.
My main goal is to get my xperia phone turn on when im plugin the charger.
Got it working by comment out this line exec /system/bin/chargemon.stock
Ref: https://z4-forum.com/forum/viewtopic.php?t=56746&start=90
For example, in an android program, I can do it like this:
android.content.pm.Signature[] sigs = pkm.getPackageInfo(
"com.test", PackageManager.GET_SIGNATURES).signatures;
But, how can I do it via adb on PC?
You can do
service call package 2 s16 "com.test" i32 64
and parse the resulting Parcel dump
Alex has a great answer.
WARNING: Ugly code ahead
I was able to get the signature in adb shell as root with the following code:
package=com.test; b=false; while read line; do case $line in *\<package*${package}*) b=true ;; *\<cert*) if $b; then echo $line | sed -e 's|.*key="||' -e 's|".*||'; b=false; fi esac; done < /data/system/packages.xml
indented:
package=com.test
b=false
while read line; do
case $line in
*\<package*${package}*)
b=true ;;
*\<cert*)
if $b; then
echo $line | sed -e 's|.*key="||' -e 's|".*||'
b=false
fi
esac
done < /data/system/packages.xml
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
I have a rather weird issue. I can't figure out why it's happening or how to fix it.
The script:
#!system/bin/sh
#set -x
reader() {
t2=-1
grep -v -E "add device|name:" | while IFS=' ' read -r t1 a b c d _; do
t1=${t1%%-*}
t=`expr $t1 - $t2`
if let 't > 0 && t2 > -1'; then
echo "sleep $t"
fi
printf 'sendevent %s' "${a%?}"
printf '%5d %5d %5d\n' "0x$b" "0x$c" "0x$d"
t2=$t1
done
}
let() {
IFS=, command eval [ '$(($*))' -ne 0 ]
}
countDown() {
echo 'Starting in...'
i=4
while [[ $i -gt 1 ]]; do
i=$(($i-1))
echo "$i"
sleep 1
done
printf '%s\n\n\n' 'Go!'
# echo "$*"
"$#" | reader
}
clear
printf '%s >' 'Catch by [n]umber of events or [t]imeout?'
read type
case $type in
n)
printf '%s >' 'Events to catch?'
read arg
echo "Gonna catch $arg events!"
countDown getevent -t -c "$arg"
;;
t)
printf '%s >' 'Timeout (in seconds)?'
read arg
echo "Gonna catch events for $arg seconds!"
countDown timeout -t $arg getevent -t
esac
The goal of the script:
Catch the user's interactions (e.g. key presses, screen taps, etc) using the getevent command and outputs a script to stdout, that can be used to replicate these events.
Additional info:
getevent's output is in hexadecimal format
sendevent accepts decimal format
Expected output:
Catch by [n]umber or by [t]imeout? n
Events to catch? > 4
Gonna catch 4 events...
Starting in...
3
2
1
Go!
sendevent /dev/input/event5 1 102 1
sendevent /dev/input/event5 0 0 0
sleep 3
sendevent /dev/input/event5 1 102 0
sendevent /dev/input/event5 0 0 0
The problem:
The code runs as expected when "n" is picked. When "t" is picked, the script exits after the specified amount of seconds. However, it does not output anything - the first line of the while loop doesn't even run.
With set -x, here's what's shown (last few lines):
+ printf %s\n\n\n Go!
Go!
+ reader
+ t2=-1
+ grep -v -E add device|name:
+ timeout -t 5 getevent -t
+ IFS = read -r t1 a b c d _
Running this alone shows output to stdout (output that's supposed to be read and modified inside the while loop):
timeout -t 5 getevent -t
Any ideas?
Thanks.
EDIT:
Alright, so I think it's a buffering issue. Basically this gives a similar issue:
getevent > output
The file gets updated every bunch of events, but not instantly (and might never update if not enough events are made). I'm not aware of any work around on Android.
I think there's no way of enabling line-buffering in busybox's grep, but you can do something like this:
$ adb shell timeout -t 10 getevent -t | \
grep --line-buffered -v -E "add device|name:" | \
while read line; do echo "READ: $line"; done
Here was basically that stuff I translated from your code before being mangled by bug workarounds. Replacing the one command I don't have with cat on the timeout branch, it runs correctly with your sample input in Busybox sh, dash, mksh, bash, and ksh93.
Since so many basic things about the shell and applications are broken, it isn't surprising this doesn't work. I would make sure Busybox is up-to-date and see whether the arithmetic and other bugs you keep hitting are reproducible on other systems, and report bugs if this code doesn't work.
#!/bin/sh
reader() {
t2=-1
grep -vE '^(add device|[[:space:]]+name:)' |
while IFS=' ' read -r t1 a b c d _; do
let "(t = (t1 = ${t1%%-*}) - t2) > 0 && t2 > -1" && echo "sleep $t"
printf 'sendevent %s' "${a%[[:digit:]]:}"
printf '%5d %5d %5d\n' "0x$b" "0x$c" "0x$d"
let t2=t1
done
}
let() {
IFS=, command eval test '$(($*))' -ne 0
}
countDown() {
echo 'Starting in...'
i=4
while let 'i-=1 > 0'; do
echo "$i"
sleep 1
done
printf '%s\n\n\n' 'Go!'
echo "$*"
"$#" <&3 | reader
}
isDigit() {
while ! ${1+false}; do
case $1 in
*[^[:digit:]]*|'') return 1
esac
command shift
done 2>/dev/null
}
main() {
printf '%s >' 'Catch by [n]umber of events or [t]imeout?'
read type
case $type in
n)
printf '%s >' 'Events to catch?'
read arg
isDigit "$arg" || return 1
echo "Gonna catch $arg events!"
countDown getevent -t -c "$arg"
;;
t)
printf '%s >' 'Timeout (in seconds)?'
read arg
isDigit "$arg" || return 1
echo "Gonna catch events for $arg seconds!"
countDown busybox timeout -t "$arg" cat -
;;
*)
return 1
esac
}
main "$#" 4<&0 <<\EOF 3<&0 <&4 4<&-
add device 1: /dev/input/event8
name: "bcm_headset"
add device 2: /dev/input/event7
name: "max8986_ponkey"
add device 3: /dev/input/event6
name: "sec_touchscreen"
add device 4: /dev/input/event5
name: "sec_keypad"
add device 5: /dev/input/event4
name: "orientation"
add device 6: /dev/input/event3
name: "accelerometer"
add device 7: /dev/input/event0
name: "proximity_sensor"
add device 8: /dev/input/event2
name: "geomagnetic_raw"
add device 9: /dev/input/event1
name: "geomagnetic"
45534-48646 /dev/input/event6: 0001 008b 00000001
45534-48646 /dev/input/event6: 0000 0000 00000000
45534-48646 /dev/input/event6: 0001 008b 00000000
45534-48646 /dev/input/event6: 0000 0000 00000000
EOF
# vim: set fenc=utf-8 ff=unix ft=sh :
Output:
$ bb --help
BusyBox v1.21.0 (2013-02-20 20:39:21 CST) multi-call binary.
Usage: bb [-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]
Unix shell interpreter
$ bb answers/countdown
Catch by [n]umber of events or [t]imeout? >t
Timeout (in seconds)? >5
Gonna catch events for 5 seconds!
Starting in...
3
2
1
Go!
busybox timeout -t 5 cat -
sendevent /dev/input/event6: 1 139 1
sendevent /dev/input/event6: 0 0 0
sendevent /dev/input/event6: 1 139 0
sendevent /dev/input/event6: 0 0 0
Had the same problem, and remembered a trick that I first used in MS-DOS 3.30, and later in Cygwin(not for timeout, but the more command is a magical bullet for lots of redirect problems):
timeout -t 8 getevent | more > getevent.txt
works well in Total Commander's shell
with su (not sh)
You're welcome :-)
It is really annoying if you adb push/pull large files to the device that there's no way to now how far along it is. Is it possible to run adb push or adb pull and get a progress bar using the 'bar' utility?
The main issue here is I think that adb expects two file names, if the input file could be replaced by stdin you could pipe through the 'bar' utility and get a progress bar. So far I haven't succeeded in doing so, but I'm not really a shell guru which is why I'm asking here :)
Note that I'm on Linux using bash.
It looks like the latest adb has progress support.
Android Debug Bridge version 1.0.32
device commands:
adb push [-p] <local> <remote>
- copy file/dir to device
('-p' to display the transfer progress)
However, the answers above also work for 'adb install' which do not have a progress option. I modified the first answer's script to work this way:
Create "adb-install.sh" somewhere in your PATH and run "adb-install.sh " instead of "adb install -f "
#!/bin/bash
# adb install with progressbar displayed
# usage: <adb-install.sh> <file.apk>
# original code from: http://stackoverflow.com/questions/6595374/adb-push-pull-with-progress-bar
function usage()
{
echo "$0 <apk to install>"
exit 1
}
function progressbar()
{
bar="================================================================================"
barlength=${#bar}
n=$(($1*barlength/100))
printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
# echo -ne "\b$1"
}
export -f progressbar
[[ $# < 1 ]] && usage
SRC=$1
[ ! -f $SRC ] && { \
echo "source file not found"; \
exit 2; \
}
which adb >/dev/null 2>&1 || { \
echo "adb doesn't exist in your path"; \
exit 3; \
}
SIZE=$(ls -l $SRC | awk '{print $5}')
export ADB_TRACE=all
adb install -r $SRC 2>&1 \
| sed -n '/DATA/p' \
| awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
export ADB_TRACE=
echo
echo 'press any key'
read n
Currently I have this little piece of bash:
function adb_push {
# NOTE: 65544 is the max size adb seems to transfer in one go
TOTALSIZE=$(ls -Rl "$1" | awk '{ sum += sprintf("%.0f\n", ($5 / 65544)+0.5) } END { print sum }')
exp=$(($TOTALSIZE * 7)) # 7 bytes for every line we print - not really accurate if there's a lot of small files :(
# start bar in the background
ADB_TRACE=adb adb push "$1" "$2" 2>&1 | unbuffer -p awk '/DATA/ { split($3,a,"="); print a[2] }' | unbuffer -p cut -d":" -s -f1 | unbuffer -p bar -of /dev/null -s $exp
echo # Add a newline after the progressbar.
}
It works somewhat, it shows a progress bar going from 0 to 100 which is nice. However, it won't be correct if you do a lot of small files, and worse, the bytes/s and total bytes shown by 'bar' aren't correct.
I challenge you to improve on my script; it shouldn't be hard! ;)
Here is my solution, it will show a simple progressbar and current numeric progress
[==================================================] 100%
Usage
./progress_adb.sh source destination
progress_adb.sh
#!/bin/bash
function usage()
{
echo "$0 source destination"
exit 1
}
function progressbar()
{
bar="=================================================="
barlength=${#bar}
n=$(($1*barlength/100))
printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
# echo -ne "\b$1"
}
export -f progressbar
[[ $# < 2 ]] && usage
SRC=$1
DST=$2
[ ! -f $SRC ] && { \
echo "source file not found"; \
exit 2; \
}
which adb >/dev/null 2>&1 || { \
echo "adb doesn't exist in your path"; \
exit 3; \
}
SIZE=$(ls -l $SRC | awk '{print $5}')
ADB_TRACE=adb adb push $SRC $DST 2>&1 \
| sed -n '/DATA/p' \
| awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
echo
Testing on Ubuntu 14.04
$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
TODO
directory support
progressbar size change when screen size change
Well I can give you an Idea:
ADB_TRACE=adb adb push <source> <destination>
returns logs for any command, so for example the copy command, which looks like:
writex: fd=3 len=65544: 4441544100000100000000021efd DATA....#....b..
here you can get the total bytes length before, with ls -a, then parse the output of adb with grep or awk, increment an interneral counter and send the current progress to the bar utility.
When you succeeded, please post the script here.