如何从文件中打印特定 IP 地址的某些文本,并对多个文本和 IP 地址重复该过程?

问题描述 投票:0回答:1

我正在自动化定制多个盒子/设备的过程。以下 bash 脚本从名为 device_ips.txt 的文件中读取 IP 地址 -> 连接到一个设备 -> 运行 adb 命令 -> 断开设备连接 -> 连接到下一个 IP 地址并重复该过程。

#!/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 文件包含以下格式的 IP 地址:

192.168.0.123
192.168.0.132
192.168.0.146

它可以很好地从文件中提取下一个 IP 地址并逐一自定义设备。我面临的问题是登录过程(位于#LOGIN下方)。该代码应该按照与 IP 地址相同的方式逐一获取电子邮件,但它会在所有设备上不断重复第一封电子邮件 ([email protected]),并且不会移至第二封电子邮件或第三封电子邮件。

emails.txt 包含电子邮件 ID:

[email protected]
[email protected]
[email protected]

我尝试将 #LOGIN 下面的第三行从

while IFS= read -r email; do
更改为
for email in "${email[@]}"; do
,它将所有电子邮件一起打印在文本框中,例如:
[email protected][email protected][email protected]
并在所有设备上重复相同的操作。

我想要的是在 IP 地址

[email protected]
上输入
192.168.0.123
,然后在
[email protected]
上输入
192.168.0.132
,依此类推。

bash automation adb ui-automation
1个回答
0
投票

将两个文件读入数组,然后使用

for
循环迭代数组索引,以便您可以从与当前正在处理的 IP 地址相同的索引中检索电子邮件地址。比如:

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

运行此代码将产生如下输出:

device ip: 192.168.0.123
email: [email protected]
---
device ip: 192.168.0.132
email: [email protected]
---
device ip: 192.168.0.146
email: [email protected]
---
© www.soinside.com 2019 - 2024. All rights reserved.