Addon.d bash script for removing apks after nightly reflash on Android 5.1 CM12.1 not working I'm trying to have a bash script in /system/addon.d/ that removes unwanted apks from a nightly Android 5.1 (CM12.1) install for my Galaxy Nexus phone. Each new nightly reflash will reinstall some system apps so that is why I want a script to reverse this action so I do not have to do it manually.

There are some backup scripts that run fine (SU, GApps) but my own script does not work. It can be found below.

I placed the script in /system/addon.d/ with permissions -rwxr-xr-x (same permissions set as other scripts in addon.d). When flashing I do not see the echo and the apps are not removed.

I have tried google and researched other scripts but I cannot figure out what I am doing wrong here. Help is appreciated!

#!/sbin/sh
#
# /system/addon.d/80-autoclean.sh
#

. /tmp/backuptool.functions

list_files() {
cat &lt;&lt;EOF
AudioFX
BackupRestoreConfirmation
BasicDreams
Browser
CMAccount
CMBugReport
Calendar
CyanogenSetupWizard
DeskClock
Eleven
Email
Exchange2
Galaxy4
HTMLViewer
HoloSpiralWallpaper
LatinIME
LiveWallpapers
LiveWallpapersPicker
LockClock
NoiseField
PhaseBeam
PhotoPhase
PhotoTable
Tag
Terminal
ThemeChooser
ThemesProvider
UserDictionaryProvider
VisualizationWallpapers
VpnDialogs
EOF
}

case "$1" in
  backup)
    # Stub
  ;;
  restore)
    # Stub
  ;;
  pre-backup)
    # Stub
  ;;
  post-backup)
    # Stub
  ;;
  pre-restore)
    # Stub
  ;;
  post-restore)
    list_files | while read FILE; do
        rm -rf /system/app/$FILE.apk
        rm -rf /system/priv-app/$FILE.apk
        rm -rf /system/app/$FILE
        rm -rf /system/priv-app/$FILE
        echo "Removed $FILE"
    done
  ;;
esac
This is all made with trial and error and some script copying, I have only vague knowledge of bash and absolutely no idea what I'm risking. All is done with backups and careful reflashes.
#!/sbin/sh
# 
# /system/addon.d/77-MyApps.sh
#
# During a dirty flash, this script will backup files and apps at /system/*
# /system is formatted and reinstalled, then the files are restored.
#

. /tmp/backuptool.functions

list_files() {
cat &lt;&lt;EOF
app/ActionLauncher/base.apk
app/Dropbox/lib/arm/libblasV8.so
app/Dropbox/lib/arm/libDummyArchIndicator.so
app/Dropbox/lib/arm/librsjni.so
app/Dropbox/lib/arm/libRSSupport.so
app/Dropbox/base.apk
app/Eventbrite/base.apk
app/Flud/lib/arm/libclient.so
app/Flud/base.apk
app/GCalendar/base.apk
app/GMail/base.apk
app/GoogleCamera/lib/arm/libgcam.so
app/GoogleCamera/lib/arm/libgcam_swig_jni.so
app/GoogleCamera/lib/arm/libjni_pegutil.so
app/GoogleCamera/lib/arm/libjni_tinyplanet.so
app/GoogleCamera/lib/arm/libjpeg.so
app/GoogleCamera/lib/arm/liblightcycle.so
app/GoogleCamera/lib/arm/libnativehelper_compat_libc++.so
app/GoogleCamera/lib/arm/librefocus.so
app/GoogleCamera/lib/arm/librs.layered_filter_fast_f32.so
app/GoogleCamera/lib/arm/librsjni.so
app/GoogleCamera/lib/arm/libRSSupport.so
app/GoogleCamera/base.apk
app/GPlus/lib/arm/libcrashreporter.so
app/GPlus/lib/arm/libcronet.so
app/GPlus/lib/arm/libframesequence.so
app/GPlus/lib/arm/libphotoeditor_native.so
app/GPlus/lib/arm/libwebp_android.so
app/GPlus/base.apk
app/GPSStatus/base.apk
app/Greenify/base.apk
app/HereMaps/lib/arm/libCertResourcesPkg.so
app/HereMaps/lib/arm/libcrypto_here.so
app/HereMaps/lib/arm/libdatabase_sqlcipher.so
app/HereMaps/lib/arm/gnustl_shared.so
app/HereMaps/lib/arm/libLohitIndicFontPkg.so
app/HereMaps/lib/arm/libMapsEngineResourcePkg.so
app/HereMaps/lib/arm/libMAPSJNI.so
app/HereMaps/lib/arm/libNanumGothicFontPkg.so
app/HereMaps/lib/arm/libNuanceVocalizer.so
app/HereMaps/lib/arm/libposclient.so
app/HereMaps/lib/arm/libPureArabicFontPkg.so
app/HereMaps/lib/arm/libPureChineseFontPkg.so
app/HereMaps/lib/arm/libPureIndicSouthFontPkg.so
app/HereMaps/lib/arm/libPureThaiFontPkg.so
app/HereMaps/lib/arm/libSdkResourcePkg.so
app/HereMaps/lib/arm/libsqlcipher_android.so
app/HereMaps/lib/arm/libssl_here.so
app/HereMaps/lib/arm/libstlport_shared.so
app/HereMaps/base.apk
app/Keep/lib/arm/libsketchology_native.so
app/Keep/base.apk
app/KernelAdiutor/base.apk
app/Orbot/lib/arm/libtun2socks.so
app/Orbot/base.apk
app/Photos/lib/arm/libcrashreporterer.so
app/Photos/lib/arm/libfilterframework_jni.so
app/Photos/lib/arm/libcronet.so
app/Photos/lib/arm/libnative.so
app/Photos/lib/arm/framesequence.so
app/Photos/lib/arm/moviemaker-jni.so
app/Photos/base.apk
app/PlayGames/lib/arm/libgames_rtmp_jni.so
app/PlayGames/base.apk
app/PlayMusic/base.apk
app/Shuttle/base.apk
app/Polycon/base.apk
app/SEftp/base.apk
app/SolidExplorer/lib/arm/lib7z.so
app/SolidExplorer/lib/arm/libgif.so
app/SolidExplorer/base.apk
app/Webview/lib/arm/libwebviewchromium.so
app/Webview/base.apk
app/XDALabs/lib/arm/librealm-jni.so
app/XDALabs/base.apk
app/Xposed/base.apk
app/Youtube/lib/arm/libambisonic_audio_renderer.so
app/Youtube/lib/arm/libcronet.so
app/Youtube/lib/arm/libfilterframework_jni.so
app/Youtube/lib/arm/libluajit.so
app/Youtube/lib/arm/libm2ts_player.so
app/Youtube/lib/arm/libmoxie.so
app/Youtube/lib/arm/libmoxieclient.so
app/Youtube/lib/arm/librsjni.so
app/Youtube/lib/arm/libRSSupport.so
app/Youtube/lib/arm/libvpx.so
app/Youtube/lib/arm/libvpxJNI.so
app/Youtube/lib/arm/libvrtoolkit.so
app/Youtube/lib/arm/libwebp_android.so
app/Youtube/base.apk
EOF
}

case &quot;$1&quot; in
  backup)
    list_files | while read FILE DUMMY; do
      backup_file $S/$FILE
    done
  ;;
  restore)
    list_files | while read FILE REPLACEMENT; do
      R=&quot;&quot;
      [ -n &quot;$REPLACEMENT&quot; ] &amp;&amp; R=&quot;$S/$REPLACEMENT&quot;
      [ -f &quot;$C/$S/$FILE&quot; ] &amp;&amp; restore_file $S/$FILE $R
    done
rm -rf /system/app/Chromium
rm -rf /system/app/Email
rm -rf /system/app/Calendar
rm -rf /system/app/webview
  ;;
  pre-backup)
    # Stub
  ;;
  post-backup)
    # Stub
  ;;
  pre-restore)
    # Stub
  ;;
  post-restore)
    # Stub
  ;;
esac

I have cobbled up this script to back up mine (after plenty of reflashing and restoring) and remove some AOSP alternatives. It seems you must write the code that is to be run after flashing over there (webview is from older AOSP, while Webview is a more streamlined mess). The template is taken from XDA with respect to grinder777. If this works, give yourself a bar of chocolate and send him (her?) a thank. If it doesn't, I have no idea what will, besides what @Android Dev said, but that's not permanent.
EDIT: As YouKnowNothingJohn said, you need at least one backup command. That command could be anything, even a dummy file (at least in &gt;4.4). Thanks Mr. Snow.