I'm automating the process of customizing multiple boxes/devices. The following bash script reads IP address from a file called device_ips.txt -> connects to one device -> runs the adb commands -> disconnects the device -> connect to next IP address and repeats the process.
#!/bin/bash
while true; do
ip_file="device_ips.txt"
if [ ! -f "$ip_file" ]; then
echo "Error: $ip_file not found."
exit 1
fi
# Read the IP addresses from the file into an array
mapfile -t device_ips < "$ip_file"
for device_ip in "${device_ips[@]}"; do
device_ip=$(echo "$device_ip" | tr -d '\r') # Remove any Windows-style carriage return characters
if [ -z "$device_ip" ]; then
echo "Error: No IP address found in the file."
exit 1
fi
adb disconnect
adb connect "$device_ip"
connected=$(adb devices | grep "$device_ip" | awk '{print $2}')
if [ "$connected" != "device" ]; then
echo "Error: Connection to $device_ip failed."
continue
fi
# Commands to be executed
adb root
adb remount
adb install...
# rest of adb commands...
#LOGIN
email_file="emails.txt"
mapfile -t email < "$email_file"
while IFS= read -r email; do
email=$(echo "$email" | tr -d '\r')
adb shell input tap 886 337 # Taps on empty textbox where email has to typed
for ((i=0; i<${#email}; i++)); do
adb shell input text "${email:i:1}" # types email
done
adb shell input tap 930 549 # login
done < "$email_file"
wait
done
done
device_ips.txt file contains IP addresses in this format:
192.168.0.123
192.168.0.132
192.168.0.146
It works well in picking up the next IP address from the file and customizing the devices one by one. The issue I'm facing is with the Login process (below #LOGIN). The code is supposed to pick up emails one by one the same way it does for IP addresses but it keeps repeating the first email (id_1@email.com) on all devices and doesn't move to second email or to third email.
emails.txt contains the email IDs:
id_1@email.com
id_2@email.com
id_3@email.com
I tried changing third line below #LOGIN which was while IFS= read -r email; do
to for email in "${email[@]}"; do
and it prints all emails together in textbox like: id_1@email.comid_2@email.comid_3@email.com
and repeats the same thing on all the devices.
What I want is it types id_1@email.com
on IP address 192.168.0.123
then id_2@email.com
on 192.168.0.132
and so on.
Read both files into arrays, and then use a for
loop to iterate over array indexes so that you can retrieve the email address from the same index as the ip address you're currently processing. Something like:
ip_file="device_ips.txt"
email_file="emails.txt"
mapfile -t device_ips < "$ip_file"
mapfile -t email_addresses < "$email_file"
for (( i=0; i<${#device_ips[@]}; i++ )); do
echo "device ip: ${device_ips[i]}"
echo "email: ${email_addresses[i]}"
echo "---"
done
Running this code will produce output like:
device ip: 192.168.0.123
email: id_1@email.com
---
device ip: 192.168.0.132
email: id_2@email.com
---
device ip: 192.168.0.146
email: id_3@email.com
---