A script to get a random file - Android Q&A, Help & Troubleshooting

Dear developers,
I have an issue in my script switching from Android 9 to 10 (devices from a Umidigi s3 Pro to a Umidigi F2)
I have installed Bosybox App on the first and Busybox Magisk module on the latter
Now the script does not work because the command
list=(`busybox find "$dirs" -type f -name *.$ext`)
returns an empty array
This is the complete script:
Bash:
#!/system/bin/sh
echo
if test "$1" = ""; then
echo "Randomfile script by Uranya <[email protected]> v1.4 01.01.2021"
echo "Usage:"
echo "sh randomfile.sh <sourcedir> <extension> <destdir>"
exit 1
fi
dirs=$1
ext=$2
dird=$3'/'
dest=$dird'random'
delim1=""
delim2=""
last='last.txt'
# create filename's array
IFS=$'\n'
# here we have the ISSUE
list=(`busybox find "$dirs" -type f -name *.$ext`)
# count number of files
num=${#list[@]}
# initialize random generator
RANDOM=$$
# generate random number in range 1-NUM
let "ran=(${RANDOM} % ${num})+ 1"
echo Random from $num files is $ran
sour=${list[ran]}
sourn=${sour#$dirs}
sourn=${sourn:1:${#sourn}}
date=$(date +"%Y.%m.%d %H:%M")
day=$(date +"%d")
hour=$(date +"%H")
minute=$(date +"%M")
message='---------------------------------------\n'$date' - '$num' >>> '$ran'\n'$delim1$sourn$delim2
if ([ "$day" = "01" ] && [[ "$minute" < "29" ]]) || [ ! -f $dird$last ]; then
echo >$dird$last $message
else
sed -i '1i'$message $dird$last
fi
echo $delim1$sourn$delim2
# rename the old file
cp $dest.$ext $dest'_back.'$ext
# copy the file
cat "$sour" >$dest.$ext
echo File copied as $delim1$dest.$ext$delim2
Can you please help me why this happens, and how to fix it?
Thank you very much for your attention!

Uranya said:
[...]
Click to expand...
Click to collapse
Having done some tests I have found this:
opening a root privileged terminal and executing
---
echo `find /storage/7BC3-1805/Music/MP3/Abba -type f -name *.mp3`
---
it returns two strings containing the names of files inside that folder, but putting it in my script continues to return an empty array, so the issue is not in the access to the folder, but in the syntax, I guess

try putting that *.$ext into quotes...

Dear friends, CXZa, after a couple of hours debugging the script, finally, I have found the mistake!
The line to be used is:
list=( `find "$dirs" -type f -name "*.$ext"` )
it is a very subtle difference: the space after and before the parenthesis!
(even the word busybox is useless)
Oddly in the Busybox app (I have had on my S3 Pro) the spaces are not mandatory, whilst in the Busybox Magisk module those spaces ARE mandatory!
I'm using that script for almost 8 years to have an every day different music for my wake up.
I'm using Tasker to call it just before my alarm get off, so the same file contains every day, a different song.
I have done a change also in the array index that did not began by 0...
So, here it is the right script:
Bash:
#!/system/bin/sh
echo
if test "$1" = ""; then
echo "Randomfile script by Uranya <@uranya7x> v1.5 26.03.2021"
echo "Usage:"
echo "sh randomfile.sh <sourcedir> <extension> <destdir>"
exit 1
fi
dirs=$1
ext=$2
dird=$3'/'
dest=$dird'random'
delim1=""
delim2=""
last='last.txt'
# create filename's array
IFS=$'\n'
list=( `find "$dirs" -type f -name "*.$ext"` )
# count number of files
num=${#list[@]}
# generate random number in range 1-NUM
let "ran=(${RANDOM} % ${num})+ 1"
echo Random from $num files is $ran
sour=${list[$ran-1]}
sourn=${sour#$dirs}
sourn=${sourn:1:${#sourn}}
date=$(date +"%Y.%m.%d %H:%M")
day=$(date +"%d")
hour=$(date +"%H")
minute=$(date +"%M")
message='---------------------------------------\n'$date' - '$num' >>> '$ran'\n'$delim1$sourn$delim2
if ([ "$day" = "01" ] && [[ "$minute" < "29" ]]) || [ ! -f $dird$last ]; then
echo >$dird$last $message
else
sed -i '1i'$message $dird$last
fi
echo $delim1$sourn$delim2
# rename the old file
cp $dest.$ext $dest'_back.'$ext
# copy the file
cat "$sour" >$dest.$ext
echo File copied as $delim1$dest.$ext
I hope it will be useful to someone else that loves to be waked up by music...
Peace everywhere!

Related

need help to translate MIUI V4 (ICS)

Hi,
i tried to translate a MIUI for Galaxy Nexus rom into french (can also be an other language)
i wrote an unix script to automate the process.
What i do is quite simple:
i get a specific MIUI V4 version for my device and the same translated version for the nexus S (because the nexus S has a french version)
i decompile each apk from the french rom, an put the french files into to decompile galaxy nexus directories
i recompile each apk
and i copy the compiled resources into the origin galaxy nexus apks
and i build the new ROM
but i get a bootloop any ideas ?
here is my script
if there are any part of this script that isn't clear, i could try to explain them.
of course this script could also be used on other device or other translation with some changes.
Thanks in advance .
Code:
#!/bin/bash
# MIUI-FRENCH TRANSLATE SCRIPT FOR GNEX V1.0 By Alain57
BASE_DIR=$PWD
FRENCH_DIR=$BASE_DIR/french_rom
ORIGINAL_DIR=$BASE_DIR/original_rom
OUTPUT_ROM_DIR=$BASE_DIR/output_rom
OUT_DIR=$BASE_DIR/out
WORK_DIR=$BASE_DIR/workdir
ORIGIN_APK_DIR=$WORK_DIR/origin
TRANSLATION_DIR=$WORK_DIR/translation
DECODED_APK_DIR=$WORK_DIR/decoded
LOG_FILE=$BASE_DIR/log.txt
TEMP_DIR=$BASE_DIR/temp
# clean the log file and create the missing directories if they don't exist (for exemple on first run)
function cleanLog(){
rm -fr $LOG_FILE
ARRAY_DIR=( $FRENCH_DIR $ORIGINAL_DIR $OUTPUT_ROM_DIR $OUT_DIR $WORK_DIR $ORIGIN_APK_DIR $TRANSLATION_DIR $DECODED_APK_DIR $TEMP_DIR )
echo "creating missing directoriesi if needed"
for DIR in ${ARRAY_DIR[@]}; do
if [ ! -d $DIR ]; then
mkdir $DIR
fi
done
}
# configure apktool to use framework-res and framework-miui-res
function setFramework(){
cd $FRENCH_DIR
echo "<- apktool if system/framework/framework-res.apk ->" >> $LOG_FILE
apktool if framework-res.apk >> $LOG_FILE 2>&1
echo "<- apktool if system/framework/framework-miui-res.apk ->" >> $LOG_FILE
apktool if framework-miui-res.apk >> $LOG_FILE 2>&1
}
#decompile all apk file from the english Gnex miui rom
function decompileOriginFiles(){
cd $ORIGINAL_DIR
COUNT_ORIGINAL_ZIP=`ls . | grep '.zip' | wc -l`
if [ "$COUNT_ORIGINAL_ZIP" -eq "1" ]; then
mv *.zip en.zip
echo "unzip the original rom"
unzip -q -o en.zip
echo "copy the apks to the origin dir"
cp -a system/framework/*.apk $ORIGIN_APK_DIR
cp -a system/app/*apk $ORIGIN_APK_DIR
cd $ORIGIN_APK_DIR
for F in *.apk; do
echo "decompiling origianl file $F ..." | tee -a $LOG_FILE
apktool d $F $DECODED_APK_DIR/${F%.apk} >> $LOG_FILE 2>&1
done
else
echo "there need to be ONE zip file in the original directory"
exit
fi
}
# decompile all apk files from the french Nexus S ROM
function decompileTranslatedFiles(){
cd $FRENCH_DIR
COUNT_FRENCH_ZIP=`ls . | grep '.zip' | wc -l`
if [ "$COUNT_FRENCH_ZIP" -eq "1" ]; then
mv *.zip fr.zip
echo "unzip the french rom"
unzip -q -o fr.zip
setFramework
echo "copy the apks to the working dir"
cp -a system/framework/*.apk $WORK_DIR
cp -a system/app/*.apk $WORK_DIR
cd $WORK_DIR
for F in *.apk; do
echo "decompiling french file $F ..." | tee -a $LOG_FILE
apktool d $F $TRANSLATION_DIR/${F%.apk} >> $LOG_FILE 2>&1
done
else
echo "there neeed to be ONE zip file in the french directory"
exit
fi
}
# Delete the given directory, because it is not needed
function deleteDirectoryWithoutTranslation(){
echo " ---> directory useless, removed"
cd $TRANSLATION_APK_DIR
rm -fr $1
}
# delete all useless data from the french apks
function cleanTranslatedFiles(){
cd $TRANSLATION_DIR
for d in *; do
echo "cleaning the directory $d"
cd $TRANSLATION_DIR/$d
if [ -d "res" ]; then
ls . | grep -v res | xargs -o rm -fr
cd "res"
CONTAINS_FRENCH_TRANSLATION=`ls .| grep '\-fr' | wc -l`
if [ "$CONTAINS_FRENCH_TRANSLATION" -eq "0" ]; then
deleteDirectoryWithoutTranslation $d
else
echo "---> transltation here, clean it"
ls . | grep -v '\-fr\|\./\|\.\./' | xargs -o rm -fr
fi
else
echo "no res directory"
deleteDirectoryWithoutTranslation $d
fi
if [ ! -f $ORIGIN_APK_DIR/$d.apk ];then
echo "directory not in original rom $d"
deleteDirectoryWithoutTranslation $d
fi
done
}
# merge the decoded translation in the decoded english files
function mergeTranslations(){
cd $DECODED_APK_DIR
mv $TRANSLATION_DIR/* .
cd $WORK_DIR
rm -f *.apk
}
# compile new apks including french files
function compileApk(){
cd $ORIGIN_APK_DIR
for f in *.apk; do
echo "compiling $f"
echo "apktool b $DECODED_APK_DIR/${f%.apk} $WORK_DIR/$f"
apktool b $DECODED_APK_DIR/${f%.apk} $WORK_DIR/$f >> $LOG_FILE 2>&1
done
}
# delete apk in work directory and put the original apks there
function deleteWorkFileAndCopyOriginalApk(){
cd $WORK_DIR
rm -f *.apk
cp -a $ORIGIN_APK_DIR/*.apk .
}
# put the compiled modified files in the original apk files
function finalMerge(){
cd $DECODED_APK_DIR
for F in *.apk; do
echo "doing final build $F"
cd $TEMP_DIR
rm -fr *
unzip -q $DECODED_DIR/$F
ls . | grep -v 'res\|classes.dex\|resources.arsc\|\./\|\.\./' | xargs -o rm -fr
if [ -d res ]; then
cd res
ls . | grep -v '\-fr\|\./\|\.\./' | xargs -o rm -fr
HAS_FR=`ls . | grep '\-fr' | wc -l`
if [ "$HAS_FR" -eq "0" ]; then
cd ..
rm -fr res
else
cd ..
fi
fi
zip -r $WORK_DIR/$F *
done
}
#replace the english apk files with the new french apk files, change the build.prop value and create an unsigned zip that can be flashed
function createRom(){
cd $OUTPUT_ROM_DIR
mv $ORIGINAL_DIR/system $ORIGINAL_DIR/boot.img $ORIGINAL_DIR/META-INF .
mv $WORK_DIR/*.apk system/app
mv system/app/framework*.apk system/framework
mv system/build.prop build.prop
sed 's/=en/=fr/g' build.prop > build1.prop
rm -f build.prop
sed 's/=US/=FR/g' build1.prop > system/build.prop
rm -f build1.prop
zip -r $BASE_DIR/new_rom.zip *
}
cleanLog
decompileTranslatedFiles
decompileOriginFiles
cleanTranslatedFiles
mergeTranslations
compileApk
deleteWorkFileAndCopyOriginalApk
finalMerge
createRom
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums & Read the Forum Rules
Moving to Q&A

[Ubuntu][How to][4/11/12 Creating a Kernel Build Script

This thread is meant for kernel devs or people who wants to begin building kernels for their first time.
There is a bit of a learning curve to this though, you need to known Linux functions and have android environment setup
This is a UBUNTU script saved as a shell script for any version of UBUNTU
Start off script with executable script definitions
Code:
#!/bin/bash
This next part is important and ultimately optional however it makes coding the script crazy easier
I call these definitions (likely not what they are technically but, it's my interpretation) to enable shortcoding commands which makes the script look cleaner
These can be named whatever you want btw
Kerneldir - is the directory where your kernel files are kept and this is the main place where you build your kernel
Packagedir - is your work environment, this is where your boot.img will be copied to, extra files, updater-script to be later packaged into a zip folder
INITRAMFS_SOURCE - is where the ramdisk should be placed in folder form [Click Here for how to split ramdisk and zimage from boot.img]
Meta - where the updater script is kept so it can be packaged for my kernel to be able to flashed
ARCH - Must stay as this definition as it is needed for compile to become successful
CROSS_COMPILE - Toolchain command, make sure your kernel supports linaro 4.6 and later. Or default to CM's 4.4.3 toolchain. Must stay as this definition as it is needed for compile to become successful
Code:
export KERNELDIR=/home/ayysir/android/kernel/ASDK
export INITRAMFS_DEST=$KERNELDIR/kernel/usr/initramfs
export PACKAGEDIR=/home/ayysir/android/kernel/asdk_out
export INITRAMFS_SOURCE=/home/ayysir/android/kernel
export Meta=/home/ayysir/android/kernel/Ayysir/META-INF
export Drivers=/home/ayysir/android/kernel/Ayysir/Drivers
export App=/home/ayysir/android/kernel/Ayysir/app
export exfat=/home/ayysir/android/kernel/Ayysir/exfat_modules
export ARCH=arm
export CROSS_COMPILE=/home/ayysir/android/kernel/toolchains/android-toolchain-4.7/bin/arm-eabi-
This part are mostly setup commands to clean out workspace deleting previous compile remnants. This is where definitions we made above comes in handy.
The echo commands are like description commands, used for many situations but for our purpose, it tells the user what is currently happening in the script
Assuming you have knowledge about kernel deving so I don't have to hold your hand in describing things
Below commands vary depending on dev as they have their own defconfig setup for their specific device replace my asdk_defconfig with
this is is for configuring what to include the compile build
UPDATE:
OPTIONAL:
Code:
# Colorize and add text parameters
red=$(tput setaf 1) # red
grn=$(tput setaf 2) # green
cya=$(tput setaf 6) # cyan
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgrn=${txtbld}$(tput setaf 2) # green
bldblu=${txtbld}$(tput setaf 4) # blue
bldcya=${txtbld}$(tput setaf 6) # cyan
txtrst=$(tput sgr0) # Reset
Above Codes Colorize and add text parameters.. it makes it look cleaner and more concise on what is happening in the terminal
The "echo -e" command tells terminal to apply the following text.
"${bldcya}" refers back to colorize table, making it bold (bld) and making text color cyan (cya)
"${txtrst}" closes the statement and resets the color back to terminal default. So the color parameters do not spill over into the next lines
Click to expand...
Click to collapse
the echo "" adds a space in between lines, just so it looks clean and does not jumble things
so here are some example on how to implement this: [Below]
Code:
echo -e "${bldcya} Making Kernel ${txtrst}"
make asdk_defconfig
echo""
echo -e "${bldcya} Clean Environment ${txtrst}"
make menuconfig
Code:
echo "Remove old Package Files"
rm -rf $PACKAGEDIR/*
echo "Setup Package Directory"
mkdir -p $PACKAGEDIR/system/lib/modules
echo "Setup App Directory"
mkdir -p $PACKAGEDIR/data/app
echo "Remove old zImage"
rm $PACKAGEDIR/zImage
echo "Remove old ramdisk"
rm $INITRAMFS_SOURCE/ramdisk.img.gz
echo "Clean Environment"
cd $KERNELDIR
make clean
echo "Make the kernel"
make asdk_defconfig
echo "Clean Environment"
make menuconfig
Before we get to this important section you need two files put into your /bin folder, mkbootfs and mkbootimg Click Here to download these files.
Then you need to cp these files to bin folder [put the files in home folder]
Code:
sudo cp ~/mkbootfs /bin
sudo cp ~/mkbootimg /bin
cd /bin
sudo chmod +x mkbootfs
sudo chmod +x mkbootimg
Now we are ready for next section
Make -j[num] command tells system how much things can be compiled at once, it's dependent on your system (number of processors] so i cay default to -j4
You also notice the script coding wrapped around the make command, this is what I setup to create compile log for compile session just incase you need to go back to review errors or debug some things
Code:
script -q ~/Compile.log -c "
make -j12 "
Below command searches for modules in zimage file and copies the modules files to workplace modules folder for packaging
Code:
cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/
Like I stated before you should of have already split the boot.img file and extracted ramdisk to a ramdisk folder. Below command uses mkbootfs to compress that folder into a .gz formatted file.
Below part is a bit tricky, because it is dependent on device to device basis.
first the code below moves ramdisk to workfolder
Then it cd's to the package folder and uses mkbootimg command to combine zimage and ramdisk together to make a boot.img
What is tricky is the command mkbootimg uses to determine space for the boot.img to be compiled in
The best way to determine the page size, base, & ramdiskaddr is either use the previous tools that you used to split a already compile boot.img commands and it spits out a file info run down or use DSIXDA ANDROID KITCHEN and using it's advanced options to determine boot.img info. Again I am not going to hold your hand on this one since you should research before trying this build script.
Note: many boot.img's use cmdline option in creation
Code:
echo "Make boot.img"
cp $INITRAMFS_SOURCE/ramdisk.img.gz $PACKAGEDIR
cd $PACKAGEDIR
mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.img.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img
Code:
echo "Packaging Ramdisk"
cd $INITRAMFS_SOURCE/
mkbootfs ./ASDK-ramdisk | gzip > ramdisk.img.gz
Code:
echo "Compiling"
script -q ~/Compile.log -c "
make -j12 "
echo "Copy modules to Package"
cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/
cp $Drivers/* $PACKAGEDIR/system/lib/
echo "Copy zImage to Package"
cp $KERNELDIR/arch/arm/boot/zImage $PACKAGEDIR/zImage
echo "Packaging Ramdisk"
cd $INITRAMFS_SOURCE/
mkbootfs ./ASDK-ramdisk | gzip > ramdisk.img.gz
echo "Make boot.img"
cp $INITRAMFS_SOURCE/ramdisk.img.gz $PACKAGEDIR
cd $PACKAGEDIR
mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.img.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img
This is where you add files you want packaged into your zip folder
*Remember add new definitions every time you add a new directory to the mix and have them cp all the files to workfolder
Code:
export curdate=`date "+%m-%d-%Y"`
This sets definition to zip to auto-generate dates to date your builds so it takes the work out of renaming the zip file [Your welcome lol]
Code:
zip -r ../ASDK-AOSP-4.2-$curdate.zip
set a name to your build and add $curdate.zip to enable auto dating zip
Code:
echo "Import of META-INF"
cp -R $Meta $PACKAGEDIR
echo "Import Voltage Control"
cp $App/* $PACKAGEDIR/data/app
echo "Importing Exfat fix"
cp $exfat/* $PACKAGEDIR/system/lib/modules
export curdate=`date "+%m-%d-%Y"`
cp ~/Compile.log ~/android/Compile-$curdate.log
rm ~/Compile.log
cd $PACKAGEDIR
rm ramdisk.img.gz
rm zImage
rm ../ASDK-AOSP-4.2*.zip\
rm -R .fr-7q5stU
zip -r ../ASDK-AOSP-4.2-$curdate.zip .
read -p "Press ENTER to Exit"
UPDATE:
Not to confuse anyone, Below is based off my new build script I use. PLEASE DO NOT COPY WORD FOR WORD, CODE BY CODE JUST USE IT AS REFERENCE. IF YOUR NOT SMART ENOUGH TO GET WHAT I AM DOING THEN DONT TRY BUILDING KERNELS LOL
Code:
if [ -e $KERNELDIR/arch/arm/boot/zImage ]; then
echo -e "${bldred} Copy modules to Package ${txtrst}"
cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/
echo ""
echo -e "${bldred} Copy zImage to Package ${txtrst}"
cp $KERNELDIR/arch/arm/boot/zImage $PACKAGEDIR/zImage
echo ""
echo -e "${bldred} Ramdisk Readying.. ${txtrst}"
cp $KERNELDIR/mkbootfs $INITRAMFS_SOURCE
cd $INITRAMFS_SOURCE/
./mkbootfs ASDK-krait | gzip > ramdisk.krait.gz
rm mkbootfs
echo ""
echo -e "${bldred} Making Boot.img.. ${txtrst}"
cp $KERNELDIR/mkbootimg $PACKAGEDIR
cp $INITRAMFS_SOURCE/ramdisk.krait.gz $PACKAGEDIR
cd $PACKAGEDIR
./mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.krait.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img
rm mkbootimg
echo ""
export curdate=`date "+%m-%d-%Y"`
cp ~/Compile.log ~/android/compile_logs/Success-Krait-$curdate.log
rm ~/Compile.log
cd $PACKAGEDIR
rm ramdisk.krait.gz
rm zImage
rm ../../ASDK-AOSP-3.4.x-*.zip
rm ../ramdisk.krait.gz
rm ../zImage
zip -r ../ASDK-AOSP-3.4.x-$curdate-EXP.zip .
mv ../ASDK-AOSP-3.4.x-$curdate-EXP.zip ~/android/kernel
echo "ASDK HOT OFF THE PRESS"
else
echo "KERNEL DID NOT BUILD! no zImage exist"
export curdate=`date "+%m-%d-%Y"`
cp ~/Compile.log ~/android/compile_logs/Failed-Krait-$curdate.log
fi;
exit 0
Now before you get mind blown Let me explain the update above:
"if [ -e $KERNELDIR/arch/arm/boot/zImage ]; then" is known as the "if, then" commands. Most used in C and scripting it tells system to look for a certain file or directory first, "if" present "then" it will proceed to following commands after it.
so if the zimage does exist then, the code after the statement will go on and end up making the zip folder.
"else" commands tells terminal, well if that file in the if statement does not exist then use following commands instead. In this case if $KERNELDIR/arch/arm/boot/zImage does not exist then else do this:
Code:
else
echo "KERNEL DID NOT BUILD! no zImage exist"
export curdate=`date "+%m-%d-%Y"`
cp ~/Compile.log ~/android/compile_logs/Failed-Krait-$curdate.log
This else statement makes a fail log to where you keep your build logs. This allows you to see where in the build did you received an error which caused the zimage to not be produced. Also it wont make a zip folder so no useless zips with no boot.img in there
Click to expand...
Click to collapse
chmod +x your script [make sure you save it as blahblah.sh] and boom your done
CLICK HERE TO SEE MY FULL SCRIPT THAT I USE FOR MY GS3 KERNEL BUILDS [NEEDS REVISING THOUGH]
IF YOU HAVE ANY QUESTIONS LEAVE IT IN THE COMMENTS OR PM ME
IF YOU LIKED MY HOW-TO AND WOULD LIKE TO DONATE TO HELP ME DEV FOR MORE DEVICES [APPRECIATED BUT DONATION NOT REQUIRED] CLICK HERE
Reserved
hm
Another Example for Fun
Well this thread looked like it needed a comment or at least some positive feedback so here it goes.
Thanks so much @ayysir for making this tutorial!!!! I've been using scripts to do lots of things for me for about a year now and it all started when I read this thread. (for some reason the though of bash scripts never really crossed my mind before then)
Here is an example of how creative I've got since the beginning of this. My script now includes some colored logos just for fun as well as final build time, uploading to goo.im, and signing of the final zip. (The logos might not show up right on here but they look great in terminal)
Code:
#!/bin/bash
#Cl3Kener's Hammerhead Script
# Colorize and add text parameters (originally grabbed from Ayysir)
red=$(tput setaf 1) # red
grn=$(tput setaf 2) # green
cya=$(tput setaf 6) # cyan
pnk=$(tput bold ; tput setaf 5) # pink
yel=$(tput bold ; tput setaf 3) # yellow
pur=$(tput setaf 5) # purple
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgrn=${txtbld}$(tput setaf 2) # green
bldyel=${txtbld}$(tput bold ; tput setaf 3) # yellow
bldblu=${txtbld}$(tput setaf 4) # blue
bldpur=${txtbld}$(tput setaf 5) # purple
bldpnk=${txtbld}$(tput bold ; tput setaf 5) # pink
bldcya=${txtbld}$(tput setaf 6) # cyan
txtrst=$(tput sgr0) # Reset
#Place you defconfig name here:
DEFCONFIG=uber_hammerhead_defconfig
# Change these exports as needed:
export INITRAMFS_DEST=~/android/kernel/usr/initramfs
export PACKAGEDIR=~/android/kernel/OUT
export KERNEL_SOURCE=~/android/kernel
export Meta=~/android/kernel/Cl3Kener/META-INF
export Etc=~/android/kernel/Cl3Kener/etc
export Scripts=~/android/kernel/Cl3Kener/kernel
export Bin=~/android/kernel/Cl3Kener/bin
export Lib=~/android/kernel/Cl3Kener/lib
export GPU=~/android/kernel/Cl3Kener/kcontrol_gpu_msm
export sign=~/android/kernel/Cl3Kener/signapk_files
export ARCH=arm
export CROSS_COMPILE=~/android/kernel/TOOLCHAINS/arm-eabi-4.7/bin/arm-eabi-
# Place your goo.im password here instead of XXXXXX so you don't have to input password (or you can use ssh key if you have one):
export SSHPASS=XXXXXXX
# Start Time
res1=$(date +%s.%N)
# Logo (just for fun)
echo "${bldpur} ${txtrst}"
echo "${bldpur} ________________________________________ ${txtrst}"
echo "${bldpur}| |${txtrst}"
echo "${bldpur}| _| _| _|_|_| _|_|_|_| _|_|_| |${txtrst}"
echo "${bldpur}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldpur}| _| _| _|_|_| _|_|_| _|_|_| |${txtrst}"
echo "${bldpur}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldpur}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldpur}| _|_| _|_|_| _|_|_|_| _| _| |${txtrst}"
echo "${bldpur}|________________________________________|${txtrst}"
echo "${bldpur} ${txtrst}"
echo "${bldcya}Remove Old Package Files ${txtrst}"
rm -rf $PACKAGEDIR/*
echo " "
echo "${bldgrn}Setup Init.d Directory ${txtrst}"
mkdir -p $PACKAGEDIR/system/etc
echo " "
echo "${bldyel}Setup Scripts Directory ${txtrst}"
mkdir -p $PACKAGEDIR/modules
echo " "
echo "${bldblu}Remove old zImage ${txtrst}"
rm $KERNEL_SOURCE/arch/arm/boot/zImage
rm $KERNEL_SOURCE/arch/arm/boot/zImage-dtb
echo " "
echo -e "${bldred}Removing annoying gedit backup files ${txtrst}"
cd ~/android/kernel
find ./ -name '*~' | xargs rm
echo " "
echo "${bldgrn}Clean Environment ${txtrst}"
cd $KERNEL_SOURCE
make $DEFCONFIG
rm $KERNEL_SOURCE/.version
rm $KERNEL_SOURCE/.config.old
rm $KERNEL_SOURCE/.config
make clean
echo " "
echo "${bldpnk}Kernel Menu ${txtrst}"
make menuconfig
echo " "
echo "${bldcya}Compiling.... ${txtrst}"
script -q ~/Compile.log -c "
make -j7"
echo " "
if [ -e $KERNEL_SOURCE/arch/arm/boot/zImage-dtb ]; then
# I used Showp1984 msm_mpdecision hotplug and so I use his KControl Module.
echo "${bldyel}Making GPU Module ${txtrst}"
cd $GPU
make clean
make -j7
echo " "
cd $KERNEL_SOURCE
echo "${bldgrn}Copy Modules to OUT ${txtrst}"
cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/modules/
echo "${bldblu}Import Scripts ${txtrst}"
cp -R $Scripts $PACKAGEDIR
echo "${bldcya}Copy bin to OUT ${txtrst}"
cp -R $Bin $PACKAGEDIR/system/bin
echo "${bldred}Copy zImage to OUT ${txtrst}"
cp $KERNEL_SOURCE/arch/arm/boot/zImage-dtb $PACKAGEDIR/kernel/zImage
echo "${bldgrn}Import of META-INF ${txtrst}"
cp -R $Meta $PACKAGEDIR
echo "${bldcya}Import Init.d Tweaks ${txtrst}"
cp -R $Etc/init.d $PACKAGEDIR/system/etc
echo "${bldred}Import Dalvik/Bionic Optimized Libs ${txtrst}"
cp -R $Lib $PACKAGEDIR/system
export curdate=`date "+%m-%d-%Y"`
cp ~/Compile.log ~/android/Logs/Completed-Uber-hammerhead-$curdate.log
rm ~/Compile.log
cd $PACKAGEDIR
rm ../UBER-Hammerhead*.zip\
rm -R .fr-7q5stU
zip -r ../UBER-Hammerhead-Nightly.zip .
echo " "
echo "${bldblu}Signing Zip ${txtrst}"
cd $KERNEL_SOURCE
cp $sign/signapk.jar $KERNEL_SOURCE
cp $sign/testkey.pk8 $KERNEL_SOURCE
cp $sign/testkey.x509.pem $KERNEL_SOURCE
java -jar ~/android/kernel/signapk.jar testkey.x509.pem testkey.pk8 UBER-Hammerhead-Nightly.zip UBER-Hammerhead-Linaro-4.8.3-$curdate.zip
rm UBER-Hammerhead-Nightly.zip
rm signapk.jar
rm testkey.pk8
rm testkey.x509.pem
echo "${bldgrn} ${txtrst}"
echo "${bldgrn} ${txtrst}"
echo "${bldgrn} ________________________________________ ${txtrst}"
echo "${bldgrn}| |${txtrst}"
echo "${bldgrn}| _| _| _|_|_| _|_|_|_| _|_|_| |${txtrst}"
echo "${bldgrn}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldgrn}| _| _| _|_|_| _|_|_| _|_|_| |${txtrst}"
echo "${bldgrn}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldgrn}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldgrn}| _|_| _|_|_| _|_|_|_| _| _| |${txtrst}"
echo "${bldgrn}|________________________________________|${txtrst}"
echo "${bldgrn} ${txtrst}"
echo "${bldgrn} ${txtrst}"
echo "${bldgrn}Kernel has completed successfully!!! ${txtrst}"
echo " "
echo " "
echo "${bldgrn}Uploading .... ${txtrst}"
echo " "
sshpass -e scp -P 2222 ~/android/kernel/UBER-Hammerhead-Linaro-4.8.3-$curdate.zip [email protected]:/home/Cl3Kener/public_html/HAMMERHEAD/KERNELS/UBER/EXP-LINARO/UBER-Hammerhead-Linaro-4.8.3-$curdate.zip
# Show Elapsed Time
res2=$(date +%s.%N)
echo "${bldgrn}Total elapsed time of ALL BUILDS AND UPLOADS: ${txtrst}${grn}$(echo "($res2 - $res1) / 60"|bc ) minutes ($(echo "$res2 - $res1"|bc ) seconds) ${txtrst}"
else
export curdate=`date "+%m-%d-%Y"`
cp ~/Compile.log ~/android/Logs/Failed-Uber-hammerhead-$curdate.log
rm ~/Compile.log
echo "${bldred} ${txtrst}"
echo "${bldred} ${txtrst}"
echo "${bldred} ________________________________________ ${txtrst}"
echo "${bldred}| |${txtrst}"
echo "${bldred}| _| _| _|_|_| _|_|_|_| _|_|_| |${txtrst}"
echo "${bldred}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldred}| _| _| _|_|_| _|_|_| _|_|_| |${txtrst}"
echo "${bldred}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldred}| _| _| _| _| _| _| _| |${txtrst}"
echo "${bldred}| _|_| _|_|_| _|_|_|_| _| _| |${txtrst}"
echo "${bldred}|________________________________________|${txtrst}"
echo "${bldred} ${txtrst}"
echo "${bldred} ${txtrst}"
echo "${bldred}KERNEL IMAGE DID NOT BUILD PROPERLY! Check Compile log! ${txtrst}"
echo " "
# Show Elapsed Time
res2=$(date +%s.%N)
echo "${bldred}Total elapsed time of ALL BUILDS AND UPLOADS: ${txtrst}${red}$(echo "($res2 - $res1) / 60"|bc ) minutes ($(echo "$res2 - $res1"|bc ) seconds) ${txtrst}"
fi;
echo " "
read -p "Press ENTER to Exit"
Hope this helps some of you get creative!
If you want to see a finished product so you can figure out how everything came together see here: http://goo.im/devs/Cl3Kener/HAMMERHEAD/KERNELS/UBER
Cheers!
Cl3Kener
Awesome guide. probbaly not seen much because of its placement but his is helpful!
This is very helpful. Thanks!
Tapatalk-kal küldve az én Nexus 5-el
I have kali linux,i am using for two years
can i use it for this?

[Q] Building script help

Hi guys...
I'm a translator for MIUI, so i edit apks a lot.
To make things easier, i made this small tool, to help me automate things.. Something like Android Utility and APK Multitool..
But, i'm no programmer, i just got a lot of help from various places, ending up with this tool.
It's far from perfect, needs a lot of improvement and here's where xda comes in
The script is running more or less fine as it is, but it has some (serious?) issues i can't figure out how to fix..
It's a little tricky to explain, but here goes..
First, i press c to clean everything, the operation completes fine.
I press e. to extract apks from a rom zip, operation completes.
Then i install frameworks, operation completes..
BUT, if i now press 2 to decompile all, i get an error:
Code:
/home/dan/buildtool/functions.sh: line 44: no match: *jar
Invalid choice
#?
Thing is, if i quit the tool and restarts it, press2, then it runs fine... ( the *.jar error is expected, it's the "invalid choice" which is interesting..)
So, it has to be something in the menu function, in a loop somewhere, i don't know..
I was hoping someone could run through the script and perhaps catch the error.. I'm really hoping for a simple answer
Other than that, i could use some good inputs about how to improve the script, add functionality and develop it in general...
The script is on github:
https://github.com/1982Strand/buildtool
I don't think you have a looping problem.
Code:
[COLOR=Gray]1067[/COLOR] [[ -z $zip ]] && echo "Invalid choice" && continue
Looks like you explicitly want it to echo "Invalid choice"
I think the problem lies in line #53
Code:
for file in *.apk *jar; do
...you will receive an error if *jar doesn't exist.
You may want to split it up with an if/then...for, for both *.apk and *jar like so:
Code:
if [ -f *.apk ]; then
for file in *.apk; do
...
done
fi
if [ -f *jar ]; then
for file in *jar; do
...
done
fi
[Edit:] Also, you should be aware of this if you aren't already....You can debug your bash scripts with the -vx switch in your shabang statement like so:
Code:
#!/bin/bash -vx
okay, i tried changing line #53 as you said, but now i get another error:
Code:
/home/dan/buildtool/functions.sh: line 53: [: too many arguments
/home/dan/buildtool/functions.sh: line 64: no match: *jar
Invalid choice
#?
And with the -vx set in the shebang:
Code:
/home/dan/buildtool/functions.sh: line 53: [: too many arguments
/home/dan/buildtool/functions.sh: line 64: no match: *.jar
++ [[ -z '' ]]
++ echo 'Invalid choice'
Invalid choice
++ continue
#?
Before i changed the code, it was normal that i threw the error with the *.jars not found, but it's supposed to continue anyway..
As i said, if i quit the tool when the error came up, start the tool again and press 2, it works. (it still gives me the error with the *.jar not found, but that was normal)
I don't understand why it takes me back to the prompt as if i were about to extract the apks??
It seems to me it's because i'm still "in" the "e. extract apks from zip" - function...?? I must admit,i don't fully understand the code in that function, so i'm having a hard time troubleshooting it..
1982Strand said:
okay, i tried changing line #53 as you said, but now i get another error:
Code:
/home/dan/buildtool/functions.sh: line 53: [: too many arguments
/home/dan/buildtool/functions.sh: line 64: no match: *jar
Invalid choice
#?
And with the -vx set in the shebang:
Code:
/home/dan/buildtool/functions.sh: line 53: [: too many arguments
/home/dan/buildtool/functions.sh: line 64: no match: *.jar
++ [[ -z '' ]]
++ echo 'Invalid choice'
Invalid choice
++ continue
#?
Before i changed the code, it was normal that i threw the error with the *.jars not found, but it's supposed to continue anyway..
As i said, if i quit the tool when the error came up, start the tool again and press 2, it works. (it still gives me the error with the *.jar not found, but that was normal)
I don't understand why it takes me back to the prompt as if i were about to extract the apks??
It seems to me it's because i'm still "in" the "e. extract apks from zip" - function...?? I must admit,i don't fully understand the code in that function, so i'm having a hard time troubleshooting it..
Click to expand...
Click to collapse
Sorry, I forgot using an 'if' statement in that way would produce the "too many arguments" error. This is from '*.apk' having more than one match, so this is what I came up with:
Code:
cd $IN
if [ "$(ls -1 | grep '.\+\.apk$' | wc -l)" -gt 0 ]; then #if there are more than 0 results of *.apk...
for file in *.apk ; do
echo "Decompiling $file" 2>&1 | tee -a $LOG/decompile_log.txt
apktool -q d -f $file $DEC/$file
done
cp -f $HJEM/sort.py $DEC
cd $DEC
python sort.py
rm -r sort.py
fi
if [ "$(ls -1 | grep '.\+\jar$' | wc -l)" -gt 0 ]; then #if there are more than 0 results of *jar...
for file in *jar; do
echo "Decompiling $file" 2>&1 | tee -a $LOG/decompile_log.txt
apktool -q d -f $file $DEC/$file
done
fi
After running options 'e' & 'c', then running option 2, there is no error and the script runs as it should (That is, assuming you chose option 2 of 'c' - "Clean all but apks in apk_in folder". Chosing option 1 of 'c' will obviously result in an error because no .apk or jar files will exist to decompile).
The reason you keep getting the "Invalid choice" error is because you're explicitly asking for it.
With the line #1067 mention earlier and others similar to it like the example below:
Code:
[COLOR=Gray]914[/COLOR] [[ -z $file ]] && echo "Invalid choice" && continue
When the variable string for '$file' has a zero length, as would be the case if '*jar' doesn't exist, the script will echo "Invalid choice". My example above ensures that the '$file' variable string will not have a zero length.
My suggestion would be to change "Invalid choice" to something more specific to the function for which it is being used, that way you can get a better idea of the source of your error.
As far as why you would get that error only after choosing options 'e' & 'c' and not after restarting the script, I couldn't say for sure without digging into it a little more, but at least this fixes your original problem.
I hope that helps.
Wow! That did the trick for option 2!!
But then it returns when i continue and get to option 4 "Fix sources":
Code:
[--- Fix MIUI sources ---]
...Fixing framework-miui-res.apk
patching file /home/dan/buildtool/apk_in/decompiled/framework-miui-res.apk/apktool.yml
/home/dan/buildtool/functions.sh: line 132: no match: *.rej
Invalid choice
#?
The point here is, that sometimes the patching fails and patch will generate some files (*.rej and *.orig) that needs to be deleted. But often, like most of the time really, the patching succeeds, so these files are not generated and my simple "remove" commands fail, obviously..
So, i'm guessing here that i get this zero-length issue and my script returns me to this code...
Well, this brings us back to
Code:
[[ -z $file ]] && echo "Invalid choice" && continue
From the "e" option..
This seems to be it.. I'm not entirely sure about what the code exactly means, except the "invalid choice" and continue..
The point with the option "e. Extract apks from zip", is that the user gets a list of zip files contained in the source_rom folder, then choose one.
Then a set of apks (defined in translation_list.txt) must be extracted to apk_in.
The function should simply give the options to choose a zip, x to return to the main menu, or write "invalid choice" if wrong key is entered...
But something seems broken in code, i just can't figure out what and where...
Btw, the code itself is from stackoverflow.com, so i didn't write it myself like that, i just used it for my script as it seemed to do what i needed, but i guess it needs some adjustment still
Okay, in the previous example, you had a 'for' loop that was written like this...
Code:
for file in *.apk *jar; do
this
and that
done
...which is saying, For every file (one at a time) in the current directory that matches the patterns *.apk and *jar, assign that filename to the variable '$file', then do..."this and that" while plugging in the value of '$file' for that particular iteration of the loop to the set of commands represented by "this and that". If for some reason, say, no files match the pattern *jar, then for each iteration of the loop regarding that pattern, $file will be equal to ' ' instead of something like 'filename.jar'. That is a variable string length of 0.
Code:
[[ -z $file ]] && echo "Invalid choice" && continue
...what that is, is a test to see if the string (or in this case filename) represented by '$file' has a zero length as with the example above where there were no jar files to assign to the variable '$file'. Similarly, it would most likely be the case with option 4 when there is no match for '*.rej' & '*.orig'.
In my example:
Code:
if [ "$(ls -1 | grep '.\+\.apk$' | wc -l)" -gt 0 ]; then
...I'm testing to see if the output of the command 'ls -1 | grep '.\+\.apk$' | wc -l' is greater than 0, before continuing with the 'for' loop.
The 'ls -1'command lists the contents of the current directory to one column, instead of the typical two or more. The 'wc -l' counts the number of lines in the resulting output. And grep '.\+\.apk$' is a regular expression that makes sure the resulting output only contains filenames that end in '.apk'. So if there are files that end in .apk, then the output of the command 'ls -1 | grep '.\+\.apk$' | wc -l' would be greater than 0, and the same would hold true for jar files. I'm sure there's a more elegant way of doing it, but it works.
Side note: Regular Expressions are powerful pattern matching tools that you definitely need to learn if you want to get the most our of your shell scripts. Google "regexp" or "Bash regexp" to learn more. It can be very confusing to understand at first, but once you get the hang of it, it is really pretty easy.
Anyway, getting back on track...
After running option 2 to decompile, then running option 4 to fix MIUI sources, everything runs fine...even with, or without '*.rej' & '*.orig'. I'll use the following debug as an example:
Code:
+ echo '...Fixing framework-miui-res.apk'
...Fixing framework-miui-res.apk
+ echo ''
+ patch -i /home/soup/buildtool/src_fix/framework-miui-res/apktool.diff /home/soup/buildtool/apk_in/decompiled/framework-miui-res.apk/apktool.yml
patching file /home/soup/buildtool/apk_in/decompiled/framework-miui-res.apk/apktool.yml
+ cd /home/soup/buildtool/apk_in/decompiled/framework-miui-res.apk/
[COLOR=Red]# notice there are no files that match '*.rej' or '*.orig' [/COLOR]
+ rm -f -r '*.rej'
+ rm -f -r '*.orig'
[COLOR=Red]# and there are no errors as a result of it[/COLOR]
+ echo ''
+ echo '...Fixing MiuiCompass.apk'
...Fixing MiuiCompass.apk
+ echo ''
+ patch -i /home/soup/buildtool/src_fix/MiuiCompass/apktool.diff /home/soup/buildtool/apk_in/decompiled/MiuiCompass.apk/apktool.yml
patching file /home/soup/buildtool/apk_in/decompiled/MiuiCompass.apk/apktool.yml
Hunk #1 FAILED at 4.
1 out of 1 hunk FAILED -- saving rejects to file /home/soup/buildtool/apk_in/decompiled/MiuiCompass.apk/apktool.yml.rej
[COLOR=Red]# here, there is now a match available for 'apktool.yml.rej' but not 'apktool.yml.orig'[/COLOR]
+ cd /home/soup/buildtool/apk_in/decompiled/MiuiCompass.apk/
+ rm -f -r apktool.yml.rej
+ rm -f -r apktool.yml.orig
[COLOR=Red]# still no error[/COLOR]
+ echo ''
...so I wouldn't know what to tell you without being able to recreate it on my end.
It may be helpful to give your variables $file and $zip in those functions a non-zero value after they are run to make sure there isn't any zero length hangover from a previous option, like so...
Code:
pull () {
shopt -s failglob
echo "[--- Choose rom zip to extract from, or x to exit ---]"
echo ""
echo ""
select zip in $SRC/*.zip
do
[[ $REPLY == x ]] && . $HJEM/build
[[ -z $zip ]] && echo "Invalid choice" && continue
echo
for apk in $(<$HJEM/translation_list.txt); do
unzip -j -o -q $zip system/app/$apk -d $IN 2&>1 > /dev/null;
done
unzip -j -o -q $zip system/framework/framework-res.apk -d $IN 2&>1 > /dev/null;
unzip -j -o -q $zip system/framework/framework-miui-res.apk -d $IN 2&>1 > /dev/null;
done
zip=dummy [COLOR=Red]<-- after the script is run, assign the string 'dummy' to $zip[/COLOR]
}
soupmagnet said:
In my example:
Code:
if [ "$(ls -1 | grep '.\+\.apk$' | wc -l)" -gt 0 ]; then
...I'm testing to see if the output of the command 'ls -1 | grep '.\+\.apk$' | wc -l' is greater than 0, before continuing with the 'for' loop.
The 'ls -1'command lists the contents of the current directory to one column, instead of the typical two or more. The 'wc -l' counts the number of lines in the resulting output. And grep '.\+\.apk$' is a regular expression that makes sure the resulting output only contains filenames that end in '.apk'. So if there are files that end in .apk, then the output of the command 'ls -1 | grep '.\+\.apk$' | wc -l' would be greater than 0, and the same would hold true for jar files. I'm sure there's a more elegant way of doing it, but it works.
Side note: Regular Expressions are powerful pattern matching tools that you definitely need to learn if you want to get the most our of your shell scripts. Google "regexp" or "Bash regexp" to learn more. It can be very confusing to understand at first, but once you get the hang of it, it is really pretty easy.
Click to expand...
Click to collapse
Really good stuff! Learning a lot from this, thanks! I'll dig into regular expressions right away
Anyways, i think i got around the missing *.rej and *.orig by approaching the operation with SED instead of PATCH..
First of all, it makes my code shorter and i don't need an external .diff file for the operation to succeed. (Given that i write the SED code correctly of course..)
It may be helpful to give your variables $file and $zip in those functions a non-zero value after they are run to make sure there isn't any zero length hangover from a previous option, like so...
Code:
pull () {
shopt -s failglob
echo "[--- Choose rom zip to extract from, or x to exit ---]"
echo ""
echo ""
select zip in $SRC/*.zip
do
[[ $REPLY == x ]] && . $HJEM/build
[[ -z $zip ]] && echo "Invalid choice" && continue
echo
for apk in $(<$HJEM/translation_list.txt); do
unzip -j -o -q $zip system/app/$apk -d $IN 2&>1 > /dev/null;
done
unzip -j -o -q $zip system/framework/framework-res.apk -d $IN 2&>1 > /dev/null;
unzip -j -o -q $zip system/framework/framework-miui-res.apk -d $IN 2&>1 > /dev/null;
done
zip=dummy [COLOR=Red]<-- after the script is run, assign the string 'dummy' to $zip[/COLOR]
}
Click to expand...
Click to collapse
Added the code
For now, the script runs through all options fine without halting. Great! But i still need to test it more thoroughly.
Now, i will look into refining the script. Especially the "5. mods" and "10. build flashable zip".
Here's how i'd like it to operate:
When option 4 is processed, i'd like to be able to add some modding to the files. I think it's better to do this before recompiling (option 6/12) because if an apk needs to be edited, it's already decompiled.
For the 3way reboot, it needs to modify some jars. I'd like the user to choose which zip from the source_roms folder to work with and extract the version number the zip filename. (The filename will ALWAYS contain a version number..) So that when the jars are processed, the output files will be placed in a folder with the version number (like /out/"version") This is because, when i want to build my flashable zip, i want the user to input which version number to build it for and then it would pull whatever mods are made for this version number. (Because the entire ROM would probably break if those version numbers don't match)
For the crt-off effect, it should do the same, but it has to check wether a jar file is already existing in /out/"version" and modify that one if it is. (Both mods need to modify the same file)
Right now, i have extra options for OFFICIAL roms. The mods are exactly the same, only the file naming in the function are different. I'd like to eliminate those options, by having the user choose what file to process, like i explain in the above..
Guess that gets a little complicated, hope you get what i mean.. It'll take some time to re-write my functions and the code, but eventually, i'll get there!
Ok, so I got it working, with the creation of the folder from the filename. Cool, one step further, I'll continue development tonight or tomorrow
Ok, next problem
In the following function, i want the script to check, if any apks exist in the folder. If yes, present the menu to choose which one to decompile. If no, display an error message and return to the main menu.
But something is up with the LS command, no matter if there are files or not in the folder, it returns the message "no files found"..
Having trouble figuring this one out..
Code:
decompile_single () {
shopt -s failglob
echo "[--- Choose apk number, or x to exit ---]"
echo ""
echo ""
cd $IN
if [ "$(ls -A $IN)" ]; then
echo ""
echo "No files found.."
echo ""
else
select file in *.apk
do
cat /dev/null > $LOG/decompile_log.txt
[[ $REPLY == x ]] && . $HJEM/build
[[ -z $file ]] && echo "Invalid choice for single decompiling" && continue
echo
echo "Decompiling $file" 2>&1 | tee -a $LOG/decompile_log.txt
apktool d -f "$file" $DEC/$file
cp -f $HJEM/sort.py $DEC/$file
python $DEC/$file/sort.py
rm -r $DEC/$file/sort.py
break
done
fi
}
Any ideas?
Okay...
The output of the command "ls -A $IN" exits with 0 if successful, otherwise it exits with 1. A good way to look at 'if' constructs is...(if "0" (goto)-> then.....if "anything else" (goto)-> else). If you're unsure of what the exit status of a command is, you can enter it in the terminal while piping it into the "echo $?" command. "$?" is a bash variable that represents the exit code of the previous command only.
For your example, you can test the exit status of that command like so...
Code:
ls -A ~/buildtool/apk_in | echo $?
Since the exit status is 0, then your output will be "No files found.." But here's where it gets tricky...The exit status of the 'ls' command will always be 0 unless the directory just cannot be accessed, which is why you will always get the same output..."No files found..". You can find out more about the exit status of a command by visiting its man page (man ls).
To get around this, you need to write the command in such a way that will give you an exit status of anything other than 0 if the condition is not met. Since you only want to check for the existence of ".apk" files you could expand on the command using a regular expression and the 'wc' command, like with my previous example...
Code:
if [ "$(ls -A $IN | grep '.\+\.apk$' | wc -l)" -eq 0 ]; then
echo ""
echo 'No ".apk" files found..'
echo ""
else
...
soupmagnet said:
Okay...
The output of the command "ls -A $IN" exits with 0 if successful, otherwise it exits with 1. A good way to look at 'if' constructs is...(if "0" (goto)-> then.....if "anything else" (goto)-> else). If you're unsure of what the exit status of a command is, you can enter it in the terminal while piping it into the "echo $?" command. "$?" is a bash variable that represents the exit code of the previous command only.
For your example, you can test the exit status of that command like so...
Code:
ls -A ~/buildtool/apk_in | echo $?
Since the exit status is 0, then your output will be "No files found.." But here's where it gets tricky...The exit status of the 'ls' command will always be 0 unless the directory just cannot be accessed, which is why you will always get the same output..."No files found..". You can find out more about the exit status of a command by visiting its man page (man ls).
To get around this, you need to write the command in such a way that will give you an exit status of anything other than 0 if the condition is not met. Since you only want to check for the existence of ".apk" files you could expand on the command using a regular expression and the 'wc' command, like with my previous example...
Code:
if [ "$(ls -A $IN | grep '.\+\.apk$' | wc -l)" -eq 0 ]; then
echo ""
echo 'No ".apk" files found..'
echo ""
else
...
Click to expand...
Click to collapse
Yes, thankyou!! Again, learning new stuff..
I just added your earlier code to this function.. Of course, it works like a charm! Facepalm on me! Hehe!
Been reading page after page about regular erxpressions, tests and all kinds of commands this weekend, i kinda stares blind at my code sometimes, haha!
1982Strand said:
Yes, thankyou!! Again, learning new stuff..
I just added your earlier code to this function.. Of course, it works like a charm! Facepalm on me! Hehe!
Been reading page after page about regular erxpressions, tests and all kinds of commands this weekend, i kinda stares blind at my code sometimes, haha!
Click to expand...
Click to collapse
I would say, the things you need to be comfortable with are (in order of importance IMO)...
man pages
exit statuses
debugging
regular expressions
pipes
data manipulation
loops
conditions
everything else
soupmagnet said:
I would say, the things you need to be comfortable with are (in order of importance IMO)...
man pages
exit statuses
debugging
regular expressions
pipes
data manipulation
loops
conditions
everything else
Click to expand...
Click to collapse
Got some more reading ahead of me
Anyways, i think the script is pretty good now, it suits my needs so far and most of the errors are taken care of..
I can always improve the code, so i'll probably continue developing on this.. Also because it's not perfect at all and still got some flaws here and there...

[Q] How to use string array in init.d script?

I want to make app symlinks to sd-ext on booting.
I made a short init.d script, And It correctly worked on JB based rom.
But after I downgraded to GB, It doesn't work anymore.
I founded that pure bone shell doesn't support arrays.
The script uses string array to declare apk list I want to symlink.
So is there a way to use string array or make similar effect?
Here is my script.
Code:
#!/system/bin/sh
if [ -z "$SD_EXT_DIRECTORY" ]; then SD_EXT_DIRECTORY=/sd-ext; fi
SYSBLOCK=`mount | grep system | cut -f 1 -d ' '`
sysapp2sd=("BackupRestoreConfirmation.apk" "BasicDreams.apk" "Bluetooth.apk" "CertInstaller.apk" "Calculator.apk" "CMFileManager.apk" "CMScreenshot.apk" "DSPManager.apk" "DrmProvider.apk" "Email.apk" "Email2.apk" "Exchange.apk" "Exchange2.apk" "Gmail.apk" "GoogleBackupTransport.apk" "InputDevices.apk" "Music.apk" "OneTimeInitializer.apk" "PhotoTable.apk" "PicoTts.apk" "SamsungTTS.apk" "SharedStorageBackup.apk" "SoundRecorder.apk" "Talk.apk" "ThemeChooser.apk" "ThemeManager.apk" "Torch.apk" "VpnDialogs.apk" "WAPPushManager.apk")
app2sd_exception=("com.UCMobile" "com.adobe.flashplayer" "com.android.lmt" "com.fiberthemax.OpQ2keyboard" "com.fiberthemax.OpQ2keyboard.dict.ko" "com.google.android.gms" "com.google.android.googlequicksearchbox" "com.jungsup.thecall" "com.mobint.hololauncher" "com.mobint.hololauncherplus" "com.mobitobi.android.gentlealarm" "com.myhome.mytheme" "com.rs.autorun" "com.teslacoilsw.launcher" "com.vladlee.callconfirm.free" "eu.chainfire.supersu" "kz.mek.DialerOne" "me.dennis.weather.naver" "net.dinglisch.android.taskerm" "net.everythingandroid.smspopup" )
# make sure $SD_EXT_DIRECTORY is actually mounted
if ! awk -vDIR="$SD_EXT_DIRECTORY" '$2 == DIR { exit 1; }' /proc/mounts ; then
mount -rw -o remount SYSBLOCK /system
mkdir -p /sd-ext/symlink/system/app
# /system/app/ -> /sd-ext/symlink/system/app/
FILES=`find /system/app -maxdepth 1 -type f`
for file in $FILES; do
filename=`basename $file`
for pattern in ${sysapp2sd[@]}; do
if [[ $filename == $pattern ]]; then
cp /system/app/$filename /sd-ext/symlink/system/app/
rm -f /system/app/$filename
ln -s /sd-ext/symlink/system/app/$filename /system/app/$filename
fi
done
done
# /data/app/ -> /sd-ext/symlink/data/app/
FILES=`find /data/app -maxdepth 1 -type f`
for file in $FILES; do
filename=`basename $file`
match=false
for pattern in ${app2sd_exception[@]}; do
if [[ $filename == $pattern* ]]; then
match=true
fi
done
if ! $match; then
cp /data/app/$filename /sd-ext/symlink/data/app/
rm -f /data/app/$filename
ln -s /sd-ext/symlink/data/app/$filename /data/app/$filename
fi
done
mount -r -o remount SYSBLOCK /system
fi
You can use echo or cat command to wrap list and grab it with awk.
and add another for loop over for loop for getting the counted numbers to be used on awk...

Guide: Fix WhatsApp Images mess after restoring backup

Hey there
Thought this might help some of you finding yourself in a huge mess because WhatApp restores all its media with the date the restore takes place. I wrote a little shellscript you can run in "adb shell". Just paste it into the shell and run it inside WhatsApps Folders.
In "WhatsApp Images" and it's subfolder "Sent" use this:
Code:
for file in *; do
if [ -f "$file" ]; then
timestamp=$(echo "$file" | sed 's/^IMG-\(.*\)-WA\(...\)\(.\)\..*/\10\2\.0\3/g')
echo $timestamp
echo $file
touch -m -t $timestamp $file
fi
done
For "WhatsApp Animated Gifs" and "WhatsApp Video" use this:
Code:
for file in *; do
if [ -f "$file" ]; then
timestamp=$(echo "$file" | sed 's/^VID-\(.*\)-WA\(...\)\(.\)\..*/\10\2\.0\3/g')
echo $timestamp
echo $file
touch -m -t $timestamp $file
fi
done
Keep the following in mind:
- This code works only if your version of Android allows you to change the modified date of files without root. As far as I know this is the case since Andoid 9
- This code doesn't work if you have files with a WA-Number higher than 0599 as my script doesn't take care to match the number with a proper timeformat.
- This code changes the last modified date of your files like this:
Filename: IMG-20190915-WA0039.jpg
Timestamp: 201909150003.09 -> 20190915 00:03:09
Feedbacks and comments welcome
Greets Air

Categories

Resources