use file list as menu options for theme chooser - android

Trying to write an android/linux script to list files in specific directory as menu options then perform set of commands using that file as a variable in a script...so far I've got this:
#!/system/bin/sh
l_count=0
cd /sdcard/themes
ls *.zip|while read l_file
do
l_count=`expr $l_count + 1`
echo "${l_count}. ${l_file}"
done
echo
echo "Pick a theme:"
read $choice
but from there I can't figure out how to assign the number assigned from l_count to the l_file

Figured it out...answer was to have the script write out a second script while still inside the do loop. Here's the final script...
#!/system/bin/sh
l_count=0
cd /sdcard/themes
echo "#!/system/bin/sh" > /sdcard/themes/themer_tmp.sh
echo "echo 'Pick a theme:'" >> /sdcard/themes/themer_tmp.sh
echo "read choice" >> /sdcard/themes/themer_tmp.sh
echo case '$choice' in >> /sdcard/themes/themer_tmp.sh
ls *.zip|while read l_file
do
l_count=`expr $l_count + 1`
echo "${l_count}. ${l_file}"
echo "${l_count})" >> /sdcard/themes/themer_tmp.sh
echo echo '"boot-recovery" > /cache/recovery/command' >> /sdcard/themes/themer_tmp.sh
echo echo '"--update_package=/sdcard/themes/'${l_file}'"' '>> /cache/recovery/command' >> /sdcard/themes/themer_tmp.sh
echo echo '"--wipe_cache" >> /cache/recovery/command' >> /sdcard/themes/themer_tmp.sh
echo echo '"reboot"' '>> /cache/recovery/command' >> /sdcard/themes/themer_tmp.sh
echo "reboot recovery" >> /sdcard/themes/themer_tmp.sh
echo ";;" >> /sdcard/themes/themer_tmp.sh
done
echo "esac" >> /sdcard/themes/themer_tmp.sh
sh /sdcard/themes/themer_tmp.sh
Hope this helps anybody else seeking a similar solution

Related

Turn on Xperia automatically when the charger is connected

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

editing android build.prop with a script

I'm trying to create a script to modify the build.prop file 'automatically'.
I want the script to check for entries I define and:
1. If they do not exist add them
2. If they do exist, check the value, and if it does not match what I've define modify it.
I have created a script.
It runs but outputs nothing, not changing the build.prop
I've attached the script. Is it correct?
#!/system/bin/sh
# Definitions
file=/system/build.prop
tmpf=/system.buildprop.bak
line_list="wifi.supplicant_scan_interval=120 ro.sf.lcd_density=480"
# Function to get args as needed for loop
getargs() {
par=$1
line=`echo $par |cut -d"=" -f1`
arg=`echo $par |cut -d"=" -f2`
}
# Loop to make all changes in line_list
for x in $line_lst; do
# Get all needed arguments
getargs $x
# Write this change to a tmp file to check on it
oldarg=`grep $line $file |cut -d"=" -f2`
sed "s/$line=.*/$line=${arg}/g" $file > $tmpf
# Check if the change was made
chknewarg=`grep $line $tmpf |cut -d"=" -f2`
if [ "$chknewarg" = "$arg" ]; then
cp $tmpf $file
if [ -f $tmpf ]; then
rm $tmpf
fi
echo "File edited"
else
if [ -f $tmpf ]; then
rm $tmpf
fi
echo "Expected $arg, got $chknewarg instead"
exit 1
fi
# If it doesn't exist at all append it to the file
chkexists=`grep -c $line $file`
if [ $chkexists -eq 0 ]; then
echo "$x" >> $file
fi
done
exit 0
I've done a bit more playing and fundamentally changes the script.
#/system/bin/sh
mount -o remount,rw /system
FILE=/system/build.prop
TMPFILE=$FILE.tmp
line1=wifi.supplicant_scan_interval
line2=ro.sf.lcd_density
line3=ro.media.enc.jpeg.quality
line1Arg=120
line2Arg=390
line3Arg=100
lineNum=
prop=$line1
arg=$line1Arg
if grep -Fq $prop $FILE ; then
lineNum=`sed -n "/${prop}/=" $FILE`
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE
else
echo "$prop does not exist in build.prop"
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi
prop=$line2
arg=$line2Arg
if grep -Fq $prop $FILE ; then
lineNum=`sed -n "/${prop}/=" $FILE`
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE
else
echo "$prop does not exist in build.prop"
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi
prop=$line3
arg=$line3Arg
if grep -Fq $prop $FILE ; then
lineNum=`sed -n "/${prop}/=" $FILE`
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE
else
echo "$prop does not exist in build.prop"
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi
mount -o remount,r /system
However I get a 'mount: invalid argument' error?
Also, how would a loop over the line and lineArg variables to compete the actions?

shell cant capture output in file using > and >>

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

Shell script search & replace in text file

I have to edit text file in android shell.
so I type this shell script.
but my Galaxy Nexus does not have Sed, Awk neither.
shell#android:/ $ sed -e "s/old_pattern/new_pattern/g" file_name > modify_file
/system/bin/sh: sed: not found
it doesn't worked.
how can i modify old_pattern to new_pattern in text file.
is it possible in Shell Script?
Edited Shell Script
#!/system/bin/sh
ARGS=4
BAD=65
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` TARGET_FILE,OLD_PATTERN,NEW_PATTERN,MODIFY_FILE"
exit $BAD
fi
old_pattern=$2
new_pattern=$3
modify_file=$4
if [ -f "$1" ]
then
target_file=$1
else
echo "\"$3\" Does not exist."
exit $BAD
fi
exit 0
Solution :
shell#android:/ $ while read STRING
>do
>echo "${STRING//old_pattern/new_pattern}" >> modify_file_name
>done < target_file_name
shell#android:/ $
It is possible with pure bash, but it will be very slooow on big files.
while read STRING
do
echo "${STRING//old_pattern/new_pattern}" >> modify_file
done < file_name
ps. Oh, I just mentioned that your shell isn't bash. Seems like it is just sh. That won't work with sh.

adb push/pull with progress bar

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.

Categories

Resources