Hi,
My first post here:
Was looking if someone can help enhancing my code.
I am limited to sh or ash shell (android / busybox)
I made a script to look for busybox symlinks, backup them and delete them
I have these questions about the below code:
-
busybox tar do not has the options to append to an archive, any workaround for my script to avoid backing up separate files?
-
I want to avoid grep to search for busybox string in the readlink output results. if [[ str1 == *busybox ]] option is not recognized in my case
-
any other advises about that script are welcome
Thanks a lot
#!/system/xbin/busybox sh
# or android shell /bin/sh
bkdir="/sdcard/bb-uninstall" ;
if [ -d "$bkdir" ] ;
then
busybox echo "Backup folder $bkdir already exists. Please remove it to proceed...">>$bkdir/__Error_Backup_Folder_Already_Exists__.txt ;
busybox echo "Backup folder \"$bkdir\" already exists. Please remove it to proceed..." ;
exit 1 ;
else
busybox mkdir $bkdir ;
if [ ! -d "$bkdir" ] ;
then
busybox echo "Backup folder \"$bkdir\" could not be created" ;
busybox echo "Check if sdcard is present, mounted, has free space or you have permissions!!" ;
exit 1 ;
fi ;
fi ;
bbdir="/system/xbin /system/bin" ;
ndeletes=0 ;
for bbpath in $bbdir ;
do
for f in $(busybox find $bbpath -type l) ;
do
gnr=$(busybox readlink $f) ;
if echo "$gnr" |busybox grep -q busybox ;
then
busybox echo -e "$f \t is linked to \t $gnr ---> backup up in sdcard then deleted">>$bkdir/bb-uninstall.log ;
bkfile=`busybox echo "$f" | busybox tr '/' '.'` ;
bkfile=bak$bkfile ;
busybox tar zcf $bkdir/$bkfile.tar.gz -C / ${f:1} ;
if [ ! -f "$bkdir/$bkfile.tar.gz" ] ;
then
busybox echo "Error while creating backup file \"$bkdir/$bkfile.tar.gz\"" ;
busybox echo "File \"$f\" was not deleted" ;
busybox echo "Check free space on \"$bkdir\" or if you have needed permissions" ;
busybox echo "Uninstall will stop. Launch it again after fixing write issue to \"$bkdir\"" ;
exit 1 ;
fi ;
busybox echo "$f linking to $gnr backed-up. Now deleting" ;
busybox rm $f ;
ndeletes=`busybox expr $ndeletes + 1` ;
else busybox echo "$f is not linked to busybox, but to $gnr ---> file not deleted">>$bkdir/bb-uninstall.log ;
fi ;
done
done
echo >>$bkdir/bb-uninstall.log ;
echo "=========================================">>$bkdir/bb-uninstall.log ;
echo " Busybox Uninstaller v1.0 Completed !! ">>$bkdir/bb-uninstall.log ;
echo "=========================================">>$bkdir/bb-uninstall.log ;
echo " - Details - ">>$bkdir/bb-uninstall.log ;
echo "Number of deleted files: $ndeletes">>$bkdir/bb-uninstall.log ;
echo "Busybox cleaned folders: \"$bbdir\"">>$bkdir/bb-uninstall.log ;
echo "Backup folder: \"$bkdir\"">>$bkdir/bb-uninstall.log ;
echo "Log file: \"$bkdir/bb-uninstall.log\"">>$bkdir/bb-uninstall.log ;
echo >>$bkdir/bb-uninstall.log ;
echo ;
echo "=========================================" ;
echo " Busybox Uninstaller v1.0 Completed !! " ;
echo "=========================================" ;
echo " - Details - " ;
echo "Number of deleted files: $ndeletes" ;
echo "Busybox cleaned folders: \"$bbdir\"" ;
echo "Backup folder: \"$bkdir\"" ;
echo "Log file: \"$bkdir/bb-uninstall.log\"" ;
echo ;