├── LICENSE ├── README.md └── scripts ├── backup-apk-files.sh ├── fast-reboot.sh ├── fix-permissions.sh ├── list-multidex-apps.sh ├── reboot-bootloader.sh ├── reboot-recovery.sh ├── reboot.sh ├── remount.sh ├── restart-systemui.sh └── shutdown.sh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jared Rummler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # android-shell-scripts 2 | 3 | Shell scripts for Android 4 | -------------------------------------------------------------------------------- /scripts/backup-apk-files.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION: 4 | # Backup installed APK files 5 | # 6 | # USAGE: 7 | # backup-apk-file.sh [--ignore-system-apps] [--destination=PATH] 8 | # 9 | 10 | backup_system_apps=true 11 | backup_location=$EXTERNAL_STORAGE/apk_files 12 | 13 | for i in "$@" 14 | do 15 | case $i in 16 | --ignore-system-apps) 17 | backup_system_apps=false 18 | shift 19 | ;; 20 | -d|--destination) 21 | backup_location="${i#*=}" 22 | shift 23 | ;; 24 | *) 25 | # unknown option 26 | esac 27 | done 28 | 29 | # create the backup directory if it doesn't exist 30 | if [ ! -d "${backup_location}" ] 31 | then 32 | mkdir -p "${backup_location}" 33 | fi 34 | 35 | # loop through all installed applications 36 | for line in $(pm list packages -f) 37 | do 38 | apk_file=$(echo $line | cut -d: -f2 | cut -d= -f1) 39 | if ! $backup_system_apps 40 | then 41 | if [[ "${apk_file}" == "/system/"* ]] 42 | then 43 | continue # skip system apk 44 | fi 45 | fi 46 | package_name=$(echo $line | cut -d= -f2) 47 | echo $package_name 48 | cp "${apk_file}" "${backup_location}/${package_name}.apk" 49 | done 50 | 51 | -------------------------------------------------------------------------------- /scripts/fast-reboot.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION: 4 | # Perform a quick reboot 5 | # 6 | 7 | setprop ctl.restart zygote 8 | -------------------------------------------------------------------------------- /scripts/fix-permissions.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | # 3 | # Warning: if you want to run this script in cm-recovery change the above to #!/sbin/sh 4 | # 5 | # fix_permissions - fixes permissions on Android data directories after upgrade 6 | # shade@chemlab.org 7 | # 8 | # original concept: http://blog.elsdoerfer.name/2009/05/25/android-fix-package-uid-mismatches/ 9 | # implementation by: Cyanogen 10 | # improved by: ankn, smeat, thenefield, farmatito, rikupw, Kastro 11 | # 12 | # v1.1-v1.31r3 - many improvements and concepts from XDA developers. 13 | # v1.34 through v2.00 - A lot of frustration [by Kastro] 14 | # v2.01 - Completely rewrote the script for SPEED, thanks for the input farmatito 15 | # /data/data depth recursion is tweaked; 16 | # fixed single mode; 17 | # functions created for modularity; 18 | # logging can be disabled via CLI for more speed; 19 | # runtime computation added to end (Runtime: mins secs); 20 | # progress (current # of total) added to screen; 21 | # fixed CLI argument parsing, now you can have more than one option!; 22 | # debug cli option; 23 | # verbosity can be disabled via CLI option for less noise;; 24 | # [by Kastro, (XDA: k4str0), twitter;mattcarver] 25 | # v2.02 - ignore com.htc.resources.apk if it exists and minor code cleanups, 26 | # fix help text, implement simulated run (-s) [farmatito] 27 | # v2.03 - fixed chown group ownership output [Kastro] 28 | # v2.04 - replaced /system/sd with $SD_EXT_DIRECTORY [Firerat] 29 | VERSION="2.04" 30 | 31 | # Defaults 32 | DEBUG=0 # Debug off by default 33 | LOGGING=1 # Logging on by default 34 | VERBOSE=1 # Verbose on by default 35 | 36 | # Messages 37 | UID_MSG="Changing user ownership for:" 38 | GID_MSG="Changing group ownership for:" 39 | PERM_MSG="Changing permissions for:" 40 | 41 | # Programs needed 42 | ECHO="busybox echo" 43 | GREP="busybox grep" 44 | EGREP="busybox egrep" 45 | CAT="busybox cat" 46 | CHOWN="busybox chown" 47 | CHMOD="busybox chmod" 48 | MOUNT="busybox mount" 49 | UMOUNT="busybox umount" 50 | CUT="busybox cut" 51 | FIND="busybox find" 52 | LS="busybox ls" 53 | TR="busybox tr" 54 | TEE="busybox tee" 55 | TEST="busybox test" 56 | SED="busybox sed" 57 | RM="busybox rm" 58 | WC="busybox wc" 59 | EXPR="busybox expr" 60 | DATE="busybox date" 61 | 62 | # Initialise vars 63 | CODEPATH="" 64 | LOCALUID="" 65 | LOCALGID="" 66 | PACKAGE="" 67 | REMOVE=0 68 | NOSYSTEM=0 69 | ONLY_ONE="" 70 | SIMULATE=0 71 | SYSREMOUNT=0 72 | SYSMOUNT=0 73 | DATAMOUNT=0 74 | SYSSDMOUNT=0 75 | FP_STARTTIME=$( $DATE +"%m-%d-%Y %H:%M:%S" ) 76 | FP_STARTEPOCH=$( $DATE +%s ) 77 | if $TEST "$SD_EXT_DIRECTORY" = ""; then 78 | #check for mount point, /system/sd included in tests for backward compatibility 79 | for MP in /sd-ext /system/sd;do 80 | if $TEST -d $MP; then 81 | SD_EXT_DIRECTORY=$MP 82 | break 83 | fi 84 | done 85 | fi 86 | fp_usage() 87 | { 88 | $ECHO "Usage $0 [OPTIONS] [APK_PATH]" 89 | $ECHO " -d turn on debug" 90 | $ECHO " -f fix only package APK_PATH" 91 | $ECHO " -l disable logging for this run (faster)" 92 | $ECHO " -r remove stale data directories" 93 | $ECHO " of uninstalled packages while fixing permissions" 94 | $ECHO " -s simulate only" 95 | $ECHO " -u check only non-system directories" 96 | $ECHO " -v disable verbosity for this run (less output)" 97 | $ECHO " -V print version" 98 | $ECHO " -h this help" 99 | } 100 | 101 | fp_parseargs() 102 | { 103 | # Parse options 104 | while $TEST $# -ne 0; do 105 | case "$1" in 106 | -d) 107 | DEBUG=1 108 | ;; 109 | -f) 110 | if $TEST $# -lt 2; then 111 | $ECHO "$0: missing argument for option $1" 112 | exit 1 113 | else 114 | if $TEST $( $ECHO $2 | $CUT -c1 ) != "-"; then 115 | ONLY_ONE=$2 116 | shift; 117 | else 118 | $ECHO "$0: missing argument for option $1" 119 | exit 1 120 | fi 121 | fi 122 | ;; 123 | -r) 124 | REMOVE=1 125 | ;; 126 | -s) 127 | SIMULATE=1 128 | ;; 129 | -l) 130 | if $TEST $LOGGING -eq 0; then 131 | LOGGING=1 132 | else 133 | LOGGING=0 134 | fi 135 | ;; 136 | -v) 137 | if $TEST $VERBOSE -eq 0; then 138 | VERBOSE=1 139 | else 140 | VERBOSE=0 141 | fi 142 | ;; 143 | -u) 144 | NOSYSTEM=1 145 | ;; 146 | -V) 147 | $ECHO "$0 $VERSION" 148 | exit 0 149 | ;; 150 | -h) 151 | fp_usage 152 | exit 0 153 | ;; 154 | -*) 155 | $ECHO "$0: unknown option $1" 156 | $ECHO 157 | fp_usage 158 | exit 1 159 | ;; 160 | esac 161 | shift; 162 | done 163 | } 164 | 165 | fp_print() 166 | { 167 | MSG=$@ 168 | if $TEST $LOGGING -eq 1; then 169 | $ECHO $MSG | $TEE -a $LOG_FILE 170 | else 171 | $ECHO $MSG 172 | fi 173 | } 174 | 175 | fp_start() 176 | { 177 | if $TEST $SIMULATE -eq 0 ; then 178 | if $TEST $( $GREP -c " /system " "/proc/mounts" ) -ne 0; then 179 | DEVICE=$( $GREP " /system " "/proc/mounts" | $CUT -d ' ' -f1 ) 180 | if $TEST $DEBUG -eq 1; then 181 | fp_print "/system mounted on $DEVICE" 182 | fi 183 | if $TEST $( $GREP " /system " "/proc/mounts" | $GREP -c " ro " ) -ne 0; then 184 | $MOUNT -o remount,rw $DEVICE /system 185 | SYSREMOUNT=1 186 | fi 187 | else 188 | $MOUNT /system > /dev/null 2>&1 189 | SYSMOUNT=1 190 | fi 191 | 192 | if $TEST $( $GREP -c " /data " "/proc/mounts" ) -eq 0; then 193 | $MOUNT /data > /dev/null 2>&1 194 | DATAMOUNT=1 195 | fi 196 | 197 | if $TEST -e /dev/block/mmcblk0p2 && $TEST $( $GREP -c " $SD_EXT_DIRECTORY " "/proc/mounts" ) -eq 0; then 198 | $MOUNT $SD_EXT_DIRECTORY > /dev/null 2>&1 199 | SYSSDMOUNT=1 200 | fi 201 | fi 202 | if $TEST $( $MOUNT | $GREP -c /sdcard ) -eq 0; then 203 | LOG_FILE="/data/fix_permissions.log" 204 | else 205 | LOG_FILE="/sdcard/fix_permissions.log" 206 | fi 207 | if $TEST ! -e "$LOG_FILE"; then 208 | > $LOG_FILE 209 | fi 210 | 211 | fp_print "$0 $VERSION started at $FP_STARTTIME" 212 | } 213 | 214 | fp_chown_uid() 215 | { 216 | FP_OLDUID=$1 217 | FP_UID=$2 218 | FP_FILE=$3 219 | 220 | #if user ownership doesn't equal then change them 221 | if $TEST "$FP_OLDUID" != "$FP_UID"; then 222 | if $TEST $VERBOSE -ne 0; then 223 | fp_print "$UID_MSG $FP_FILE from '$FP_OLDUID' to '$FP_UID'" 224 | fi 225 | if $TEST $SIMULATE -eq 0; then 226 | $CHOWN $FP_UID "$FP_FILE" 227 | fi 228 | fi 229 | } 230 | 231 | fp_chown_gid() 232 | { 233 | FP_OLDGID=$1 234 | FP_GID=$2 235 | FP_FILE=$3 236 | 237 | #if group ownership doesn't equal then change them 238 | if $TEST "$FP_OLDGID" != "$FP_GID"; then 239 | if $TEST $VERBOSE -ne 0; then 240 | fp_print "$GID_MSG $FP_FILE from '$FP_OLDGID' to '$FP_GID'" 241 | fi 242 | if $TEST $SIMULATE -eq 0; then 243 | $CHOWN :$FP_GID "$FP_FILE" 244 | fi 245 | fi 246 | } 247 | 248 | fp_chmod() 249 | { 250 | FP_OLDPER=$1 251 | FP_OLDPER=$( $ECHO $FP_OLDPER | cut -c2-10 ) 252 | FP_PERSTR=$2 253 | FP_PERNUM=$3 254 | FP_FILE=$4 255 | 256 | #if the permissions are not equal 257 | if $TEST "$FP_OLDPER" != "$FP_PERSTR"; then 258 | if $TEST $VERBOSE -ne 0; then 259 | fp_print "$PERM_MSG $FP_FILE from '$FP_OLDPER' to '$FP_PERSTR' ($FP_PERNUM)" 260 | fi 261 | #change the permissions 262 | if $TEST $SIMULATE -eq 0; then 263 | $CHMOD $FP_PERNUM "$FP_FILE" 264 | fi 265 | fi 266 | } 267 | 268 | fp_all() 269 | { 270 | FP_NUMS=$( $CAT /data/system/packages.xml | $EGREP "^ /dev/null 2>&1 453 | fi 454 | 455 | if $TEST $SYSSDMOUNT -eq 1; then 456 | $UMOUNT $SD_EXT_DIRECTORY > /dev/null 2>&1 457 | fi 458 | 459 | if $TEST $SYSMOUNT -eq 1; then 460 | $UMOUNT /system > /dev/null 2>&1 461 | fi 462 | 463 | if $TEST $DATAMOUNT -eq 1; then 464 | $UMOUNT /data > /dev/null 2>&1 465 | fi 466 | 467 | FP_ENDTIME=$( $DATE +"%m-%d-%Y %H:%M:%S" ) 468 | FP_ENDEPOCH=$( $DATE +%s ) 469 | 470 | date_diff $FP_STARTEPOCH $FP_ENDEPOCH 471 | 472 | fp_print "$0 $VERSION ended at $FP_ENDTIME (Runtime:${FP_DDM}m${FP_DDS}s)" 473 | } 474 | 475 | #MAIN SCRIPT 476 | 477 | fp_parseargs $@ 478 | fp_start 479 | if $TEST "$ONLY_ONE" != "" -a "$ONLY_ONE" != "0" ; then 480 | fp_single "$ONLY_ONE" 481 | else 482 | fp_all 483 | fi 484 | fp_end 485 | -------------------------------------------------------------------------------- /scripts/list-multidex-apps.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION 4 | # List apps which are using multidex 5 | # 6 | 7 | for line in $(pm list packages -f) 8 | do 9 | apk=$(echo $line | cut -d: -f2 | cut -d= -f1) 10 | ret=$(unzip -l "${apk}" | grep classes2.dex) 11 | if [ -n "${ret}" ] 12 | then 13 | package_name=$(echo $line | cut -d: -f2 | cut -d= -f2) 14 | echo "$package_name" 15 | fi 16 | done 17 | -------------------------------------------------------------------------------- /scripts/reboot-bootloader.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION: 4 | # Reboot Android into bootloader 5 | # 6 | 7 | reboot bootloader -------------------------------------------------------------------------------- /scripts/reboot-recovery.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION: 4 | # Reboot Android into bootloader 5 | # 6 | 7 | setprop ctl.start pre-recovery 8 | sleep 3 9 | reboot recovery # fallback 10 | -------------------------------------------------------------------------------- /scripts/reboot.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION: 4 | # Reboot Android 5 | # 6 | 7 | setprop sys.powerctl reboot 8 | sleep 3 9 | reboot # fallback 10 | -------------------------------------------------------------------------------- /scripts/remount.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION: 4 | # Remount a file system on Android 5 | # 6 | # PARAMETERS: 7 | # param1: the file system (default is "/system") 8 | # param2: the mount option (default is "rw") 9 | # 10 | 11 | ######################## 12 | # FUNCTIONS 13 | ######################## 14 | 15 | remount() { 16 | local path=${1:-/system} 17 | local option=${2:-rw} 18 | local result_code=1 19 | local oifs=$IFS 20 | IFS=" 21 | " 22 | # read /proc/mounts line by line to get the correct device 23 | while read line 24 | do 25 | local mount_point=$(echo $line | cut -d' ' -f2) 26 | if [ "$path" == "$mount_point" ] 27 | then 28 | local device=$(echo $line | cut -d' ' -f1) 29 | result_code=$(mount -o remount,$option $device $mount_point) 30 | break 31 | fi 32 | done < /proc/mounts 33 | IFS=$oifs # restore internal field seperator 34 | return $result_code 35 | } 36 | 37 | ######################## 38 | # MAIN 39 | ######################## 40 | 41 | remount $@ 42 | -------------------------------------------------------------------------------- /scripts/restart-systemui.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION: 4 | # Restart com.android.systemui 5 | # 6 | 7 | service call activity 42 s16 com.android.systemui 8 | am startservice -n com.android.systemui/.SystemUIService 9 | -------------------------------------------------------------------------------- /scripts/shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/system/bin/sh 2 | ######################## 3 | # DESCRIPTION: 4 | # Shutdown Android 5 | # 6 | 7 | setprop sys.powerctl shutdown 8 | sleep 3 9 | reboot -p # fallback 10 | --------------------------------------------------------------------------------