[ASUS ZenFone 8] bnsmb:
For installing and configuring an Android phone via script it is often required to boot the phone from an TWRP recovery image

Because of this I wrote a Linux shell script to reboot an Android phone from an TWRP recovery image:

boot_phone_from_twrp.sh

The script boot_phone_from_trwp.sh will detect the current status of the attached phone and use the approbiate commands (fastboot and/or adb) to reboot the phone from the TWRP recovery image.

The usage for the script is:

boot_phone_from_twrp.sh v2.2.0.0 - boot a phone from a TWRP image

boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [status] [twrp_image]

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $

Use the parameter -H to get a detailed usage help:

Spoiler content hidden.

When more than one phone is connected via USB use the parameter serial=#sn or the environment variable SERIAL_NUMBER to define the serial number of the phone to use.

To only retrieve the current status of the attached phone execute the script with the parameter checkonly or status.

The script will then only check the status of the phone and return the current status in the return code:

1 - the phone is booted from the TWRP image

2 - the phone is booted from TWRP installed in the boot or recovery partition

3 - the phone is booted into the Android OS

4 - the phone is booted into bootloader

5 - the phone is booted into the fastbootd

6 - the phone is booted into the safe mode of the Android OS

7 - the phone is booted into the LineageOS recovery installed in the boot or recovery partition

If the parameter status is used the script will also print a message with the current status of the phone,e.g:

Spoiler content hidden.

The script boot_phone_from_twrp.sh can also be used as include file using

. ./boot_phone_from_twrp.sh

Using this method these functions are available in the script that includes boot_phone_from_twrp.sh:

retrieve_phone_status

print_phone_status

boot_phone_from_the_TWRP_image

reboot_phone

wait_for_phone_to_be_in_the_bootloader

wait_for_phone_with_a_working_adb_connection

wait_for_the_adb_daemon

Please check the comments at the start of the script for details about using the script in this way; the command

grep "#S#" boot_phone_from_twrp.sh | cut -c4-

prins the hints for using the script as include file

Spoiler content hidden.

Note:

After including the script you should use the command

${ADB} ${ADB_OPTIONS}

to run the adb binary and the command

${FASTBOOT} ${FASTBOOT_OPTIONS}

to run the fastboot binary.

Examples:

To boot the phone from the TWRP image use this code:

. ./boot_phone_from_twrp.sh

retrieve_phone_status
boot_phone_from_the_TWRP_image

To boot the phone into the Android OS use this code:

. ./boot_phone_from_twrp.sh

retrieve_phone_status
reboot_phone

Spoiler content hidden.

The script boot_phone_from_twrp.sh is used as include script in the script init_magisk_db.sh - see this post How to enable root access using Magisk in a script. The script is also used in the script install_os_via_twrp.sh see this post How to install an OS image using the twrp command in a script .

The script boot_phone_from_twrp.sh is also available on my homepage:

http://bnsmb.de/files/public/Android/boot_phone_from_twrp.sh

Spoiler content hidden.

Spoiler content hidden.